int P2_Startup(void *arg){ USLOSS_Console(" \n---------Starting Test 9 ----------\n"); sem = P1_SemCreate(0); int childpid1, child_status1, exit_childpid1; int childpid2, child_status2, exit_childpid2; childpid1 = P1_Fork("child_p", child_p, NULL, USLOSS_MIN_STACK * 4, 2); USLOSS_Console("Child PID created %d \n", childpid1); childpid2 = P1_Fork("killer", killer, &childpid1, USLOSS_MIN_STACK * 4, 3); USLOSS_Console("Child PID created %d \n", childpid2); exit_childpid1 = P1_Join(&child_status1); exit_childpid2 = P1_Join(&child_status2); USLOSS_Console("end first %d (should be %d) status %d (should be 444)\nend second %d (should be %d) status %d (should be -4)\n", exit_childpid1,childpid1,child_status1,exit_childpid2,childpid2, child_status2); USLOSS_Console(" ---------Ending Test 9 ----------\n"); return 0; }
static void clockHandler2(int dev, void *arg) { long unit = (long) arg; int clockResult; // check if dispatcher should be called if (readCurStartTime() >= 80000) { timeSlice(); } // inc that a clock interrupt happened clockTicks++; USLOSS_DeviceInput(dev, unit, &clockResult); // every fith interrupt do a conditional send to its mailbox if (clockTicks % 5 == 0) { if (debugflag2 && DEBUG2) { USLOSS_Console("clockHandler2: sending message %s to mbox %d\n", clockResult, clockBox.mboxID); } int sendResult = MboxCondSend(clockBox.mboxID, &clockResult, sizeof(clockResult)); if (debugflag2 && DEBUG2) { USLOSS_Console("clockHandler2: send returned %d\n", sendResult); USLOSS_Halt(1); } } }
static void terminalHandler(int dev, void *arg) { long unit = (long) arg; if (debugflag2 && DEBUG2) { USLOSS_Console("terminalHandler(): dev = %d\n", dev); USLOSS_Console("terminalHandler(): unit = %d\n", unit); } int termResult; // check for valid values if (dev != USLOSS_TERM_DEV || unit < 0 || unit > USLOSS_TERM_UNITS) { USLOSS_Console("termHandler(): Bad values\n"); USLOSS_Halt(1); } // make sure our box still exists if (termBoxes[unit].mboxID == -1) { USLOSS_Console("Term mailbox does not exist, unit\n"); USLOSS_Halt(1); // might need to reutn instead } int result = USLOSS_DeviceInput(USLOSS_TERM_DEV, unit, &termResult); // if (debugflag2 && DEBUG2) { // USLOSS_Console("terminalHandler(): sending now from dev %d to mbox %d value %s\n", dev, termBoxes[unit].mboxID, termResult); // } MboxCondSend(termBoxes[unit].mboxID, &termResult, sizeof(termResult)); if (result != USLOSS_DEV_OK) { USLOSS_Console("termHandler(): USLOSS_DeviceInput is not ok.\n"); USLOSS_Halt(1); } }
int start4(char *arg) { int pid, status, i; char buf[12]; char child_buf[12]; USLOSS_Console("start4(): Spawn eight children. \n"); USLOSS_Console(" 4 write 5 lines to a diff terminal.\n"); USLOSS_Console(" 4 read 5 lines to a diff terminal.\n"); for (i = 0; i < 4; i++) { sprintf(buf, "%d", i); sprintf(child_buf, "Child%d", i); status = Spawn(child_buf, Child1, buf, USLOSS_MIN_STACK,2, &pid); sprintf(child_buf, "Child%d", i+4); assert(status == 0); status = Spawn(child_buf, Child2, buf, USLOSS_MIN_STACK,2, &pid); assert(status == 0); } for (i = 0; i < 8; i++) { Wait(&pid, &status); //assert(status == 0); } USLOSS_Console("start4(): done.\n"); Terminate(1); return 0; } /* start4 */
int waitDevice(int type, int unit, int *status) { mailbox *mbox; switch (type) { case USLOSS_CLOCK_DEV : mbox = &clockBox; break; case USLOSS_DISK_INT : mbox = &diskBoxes[unit]; break; case USLOSS_TERM_INT : mbox = &termBoxes[unit]; break; } if (debugflag2 && DEBUG2) { USLOSS_Console("waitDevice(): receiving from %d\n", mbox->mboxID); } //notify p1.c that there is another process waiting on a device, then receive/block addProcess(); MboxReceive(mbox->mboxID, status, sizeof(long)); releaseProcess(); if (debugflag2 && DEBUG2) { USLOSS_Console("waitDevice(): received %s from mailbox %d\n", status, mbox->mboxID); } if (isZapped()) { return -1; } return 0; }
int P2_Startup(void *notused) { USLOSS_Console("P2_Startup\n"); P1_Fork("P3_Startup", P3_Startup, NULL, 4 * USLOSS_MIN_STACK, 4); USLOSS_Console("P2_Finished\n"); return 0; }
int Child1(char *arg) { int term = atoi(arg); char buf[MAXLINE] = ""; int read_length; int i; USLOSS_Console("Child%d(): start\n", term); for (i = 0; i< 5; i++){ if (TermRead(buf, MAXLINE, term, &read_length) < 0) { USLOSS_Console("ERROR: ReadTeam\n"); return -1; } else { buf[read_length] = '\0'; USLOSS_Console("Child%d(): read %s", term, buf); } } USLOSS_Console("Child%d(): done\n", term); Terminate(0); return 0; } /* Child 1 */
int P4_Startup(void *notused) { // P1_DumpProcesses(); USLOSS_Console("P4_Startup\n"); P1_Fork("P6_Startup", P6_Startup, NULL, 4 * USLOSS_MIN_STACK, 1); USLOSS_Console("P4_Finished\n"); return 0; }
int P2_Startup(void *notused) { int pid; int state; int status = 0; USLOSS_Console("P2_Startup\n"); pid = P1_Fork("Child", Child, NULL, USLOSS_MIN_STACK, 3); if (pid < 0) { USLOSS_Console("Unable to fork child: %d\n", pid); status = 1; } else { /* * Child runs at priority 3, which is lower than ours. Part A is * run-to-completion, so we should continue to run while the * child waits. */ state = P1_GetState(pid); if (state != 1) { // child should be ready USLOSS_Console("Child is in invalid state: %d\n", state); status = 1; } } return status; }
int P3_Startup(void *arg) { USLOSS_Console( "Starting test of creating a simple mailbox with no problems\n"); int id = -1; int result = Sys_MboxCreate(1, 500, &id); assert(result == 0); assert(id >= 0); USLOSS_Console("Created the mailbox\n"); int size = 7; char *input = "hello!"; result = Sys_MboxSend(-5, input, &size); assert(result == -1); size = 7; result = Sys_MboxSend(5000, input, &size); assert(result == -1); size = 501; result = Sys_MboxSend(id, input, &size); assert(result == -1); size = -1; result = Sys_MboxSend(id, input, &size); assert(result == -1); size = 501; result = Sys_MboxSend(-50, input, &size); assert(result == -1); USLOSS_Console("You passed all the tests! Treat yourself to a cookie!\n"); return 7; }
int start4(char *arg) { int result; int status; USLOSS_Console("start4(): Writing data to 3 disk sectors, wrapping "); USLOSS_Console("to next track\n"); USLOSS_Console("\nstart4(): Disk 0:\n"); strcpy(§ors[0 * 512], "This is a test\n"); strcpy(§ors[1 * 512], "Does it work?\n"); strcpy(§ors[2 * 512], "One last chance\n"); result = DiskWrite((char *) sectors, 0, 4, 15, 3, &status); assert(result == 0); result = DiskRead((char *) copy, 0, 4, 15, 3, &status); USLOSS_Console("start4(): Read from disk: %s\n", ©[0*512]); USLOSS_Console("start4(): Read from disk: %s\n", ©[1*512]); USLOSS_Console("start4(): Read from disk: %s\n", ©[2*512]); USLOSS_Console("\nstart4(): Disk 1:\n"); strcpy(§ors[0 * 512], "This is a test\n"); strcpy(§ors[1 * 512], "Does it work?\n"); strcpy(§ors[2 * 512], "One last chance\n"); result = DiskWrite((char *) sectors, 1, 4, 15, 3, &status); assert(result == 0); result = DiskRead((char *) copy, 1, 4, 15, 3, &status); USLOSS_Console("start4(): Read from disk: %s\n", ©[0*512]); USLOSS_Console("start4(): Read from disk: %s\n", ©[1*512]); USLOSS_Console("start4(): Read from disk: %s\n", ©[2*512]); Terminate(24); return 0; }
int Child(void *arg) { int pid; int state; int status = 0; USLOSS_Console("Child\n"); P1_DumpProcesses(); pid = P1_Fork("Grandchild", Grandchild, NULL, USLOSS_MIN_STACK, 2); if (pid < 0) { USLOSS_Console("Unable to fork child: %d\n", pid); status = 1; } else { /* * Grandchild runs at priority 2, which is higher than ours. Part A is * run-to-completion, the grandchild should already have quit before * we get here. */ // USLOSS_Console("Grandchild pid : %d,State=%d\n", pid,P1_GetState(pid)); state = P1_GetState(pid); if (state != 3) { // grandchild should have quit USLOSS_Console("Grandchild is in invalid state: %d\n", state); status = 1; } } return status; }
int start4(char *arg) { int pid, status, i; char buf[12]; char name[] = "ChildS"; USLOSS_Console("start4(): Spawning 5 children to sleep\n"); for (i = 0; i < 5; i++) { sprintf(buf, "%d", i); name[5] = buf[0]; status = Spawn(name, ChildS, buf, USLOSS_MIN_STACK,2, &pid); } USLOSS_Console("start4(): Spawning 2 children to termfuncs\n"); status = Spawn("ChildTR", ChildTR, NULL, USLOSS_MIN_STACK,2, &pid); status = Spawn("ChildTW", ChildTW, NULL, USLOSS_MIN_STACK,2, &pid); USLOSS_Console("start4(): Spawning 4 children to diskfuncs\n"); status = Spawn("ChildDW0", ChildDW0, NULL, USLOSS_MIN_STACK,2, &pid); status = Spawn("ChildDW1", ChildDW1, NULL, USLOSS_MIN_STACK,2, &pid); status = Spawn("ChildDR0", ChildDR0, NULL, USLOSS_MIN_STACK,4, &pid); status = Spawn("ChildDR1", ChildDR1, NULL, USLOSS_MIN_STACK,4, &pid); for (i = 0; i < 11; i++) { Wait(&pid, &status); } USLOSS_Console("start4(): done.\n"); Terminate(1); return 0; } /* start4 */
int ChildTR(char *arg) { char buf[80] = ""; int read_length; int i; USLOSS_Console("ChildTR(): start\n"); for (i=0; i<4; i++) { if (TermRead(buf, 80, i, &read_length) < 0) { USLOSS_Console("ChildTR(): ERROR: ReadTerm\n"); return -1; } else { USLOSS_Console("ChildTR(): terminal %d, read_length = %d\n", i, read_length); buf[read_length] = '\0'; USLOSS_Console("ChildTR(): read from term%d: read %s", i, buf); } } USLOSS_Console("ChildTR(): done\n"); Terminate(0); return 0; } /* ChildTR */
int TermRead (char *buffer, int bufferSize, int unitID, int *numCharsRead){ //check for illegal arguments if (buffer == 0 || bufferSize <= 0 || unitID < 0){ if (debugflaglib4) USLOSS_Console("TermRead(): invalid arguments! returning\n"); return -1; } //build sysarg structure to pass to the syscall vec systemArgs sysArg; CHECKMODE; sysArg.number = SYS_TERMREAD; sysArg.arg1 = buffer; sysArg.arg2 = (void *) ( (long) bufferSize); sysArg.arg3 = (void *) ( (long) unitID); if (debugflaglib4) USLOSS_Console("TermRead(): sysarg built, calling sysvec function\n"); USLOSS_Syscall(&sysArg); int bytesRead = (int ) ((void*) sysArg.arg2); *numCharsRead = bytesRead; //return (int ) ((void*) sysArg.arg2); return 0; }
void P1_DumpProcesses(){// Do CPU Time Part Check_Your_Privilege(); USLOSS_Console("Dumping Process\n"); int i; for(i=0;i<P1_MAXPROC;i++){ if(procTable[i].priority==-1){ continue; } int state=procTable[i].state; char* statePhrase; switch(state){ case 1: statePhrase="Ready"; break; case 2: statePhrase="Killed"; break; case 3: statePhrase="Quit"; break; case 4: statePhrase="Waiting"; break; } int cpu; if(i==currPid){ cpu=P1_ReadTime(); }else{ cpu=procTable[i].cpuTime; } USLOSS_Console("Name:%s\t PID:%-5d\tParent:%d\tPriority:%d\tState:%s\tKids:%d\tCPUTime:%d\n", procTable[i].name,i,procTable[i].parent,procTable[i].priority, statePhrase,procTable[i].numChildren,cpu); } }
// // Read characters from the terminals and puts them in the buffers. // When a buffer is full verify its content against the inputs. // void term_handler(int type, void *arg) { int result; int status; char ch; int unit = (int) arg; int i; // Get the device status for the terminal. result = USLOSS_DeviceInput(USLOSS_TERM_DEV, unit, &status); if (result != USLOSS_DEV_OK) { USLOSS_Console("Something is wrong with terminal %d!!", unit); USLOSS_Halt(1); } // If the status is "USLOSS_DEV_BUSY" then a character has been received. if (USLOSS_TERM_STAT_RECV(status) == USLOSS_DEV_BUSY) { ch = USLOSS_TERM_STAT_CHAR(status); // Get the characte USLOSS_Console("%d: %c\n", unit, ch); i = counts[unit]++; buffers[unit][i] = ch; if (i == NUMCHARS-1) { assert(!strncmp(inputs[unit], buffers[unit], NUMCHARS)); done++; } } }
int P2_MboxRelease(int mbox){ if(permissionCheck()){ return -1; } P1_P(mBoxSemaphore); int i; for(i = 0; i < P2_MAX_MBOX;i++){ if(mailboxes[i].inUse && mailboxes[i].id == mbox){ if(P1_SemFree(mailboxes[i].mutex) != 0){ USLOSS_Console("Processes waiting on mailbox. Halting.\n"); USLOSS_Halt(1); } if(P1_SemFree(mailboxes[i].fulls) != 0){ USLOSS_Console("Processes waiting on mailbox. Halting.\n"); USLOSS_Halt(1); } if(P1_SemFree(mailboxes[i].empties) != 0){ USLOSS_Console("Processes waiting on mailbox. Halting.\n"); USLOSS_Halt(1); } while(mailboxes[i].queue != NULL){ message *m = queuePop(&(mailboxes[i].queue)); free(m->buf); free(m); } mailboxes[i].inUse = 0; P1_V(mBoxSemaphore); return 0; } } P1_V(mBoxSemaphore); return -1; }
int child_p(void *arg){ USLOSS_Console("start child_p\n"); P1_P(sem); //P1_DumpProcesses(); USLOSS_Console("end child_p\n"); return 666; }
int P7_Startup(void *notused) { USLOSS_Console("P7_Startup\n"); P1_Quit(2); USLOSS_Console("P7_Finished\n"); P1_DumpProcesses(); return 0; }
int Child2(char *arg) { USLOSS_Console("\nChild2(): starting, releasing mailbox\n\n"); MboxRelease(mailbox); USLOSS_Console("Child2(): done\n"); return 9; } /* Child2 */
int P4_Startup(void *notused) { int pid; USLOSS_Console("P4_Startup\n"); int result = Sys_Spawn("P5_Startup", P5_Startup, NULL, 4 * USLOSS_MIN_STACK, 4, &pid); Sys_Wait(&pid,&result); printf("P4 pid changed: %d\n", pid); USLOSS_Console("P4_Finished\n"); return 0; }
int P3_Startup(void *notused) { USLOSS_Console("P3_Startup\n"); int result = P1_Fork("P4_Startup", P4_Startup, NULL, 4 * USLOSS_MIN_STACK, 3); USLOSS_Console("P3_Finished\n"); if (result != 3) USLOSS_Console("Fail== RESULT: %d\n", result); return 0; }
int P2_Startup(void *notused) { USLOSS_Console("P2_Startup\n"); int result = P1_Fork("P3_Startup", P3_Startup, NULL, 4 * USLOSS_MIN_STACK, 4); USLOSS_Console("P2_Finished\n"); if (result != 2) USLOSS_Console("Fail\n"); return 0; }
int Child1(char *arg) { int result; USLOSS_Console("\n%s(): starting, blocking on a mailbox receive\n", arg); result = MboxReceive(mailbox, NULL, 0); USLOSS_Console("%s(): result = %d\n", arg, result); return 1; } /* Child1 */
int XXp1(char *arg) { int i, result; char buffer[20]; int msgNum = 0; USLOSS_Console("\nXXp1(): started\n"); for (i = 0; i < 8; i++) { USLOSS_Console("\nXXp1(): conditionally sending message #%d ", msgNum); USLOSS_Console("to mailbox %d\n", mbox_id); sprintf(buffer, "hello there, #%d", msgNum); result = MboxCondSend(mbox_id, buffer, strlen(buffer)+1); USLOSS_Console("XXp1(): after conditional send of message "); USLOSS_Console("#%d, result = %d\n", msgNum, result); msgNum++; } MboxReceive(pause_mbox, NULL, 0); // should block on mail box for (i = 0; i < 8; i++) { USLOSS_Console("\nXXp1(): conditionally sending message #%d ", msgNum); USLOSS_Console("to mailbox %d\n", mbox_id); sprintf(buffer, "good-bye, #%d", i); result = MboxCondSend(mbox_id, buffer, strlen(buffer)+1); USLOSS_Console("XXp1(): after conditional send of message "); USLOSS_Console("#%d, result = %d\n", msgNum, result); msgNum++; } quit(-3); return 0; /* so gcc will not complain about its absence... */ } /* XXp1 */
int ChildDW0(char *arg) { int status; char disk_buf_A[512]; USLOSS_Console("\nChildDW0(): writing to disk 0, track 5, sector 0\n"); sprintf(disk_buf_A, "ChildDW0(): A wonderful message to put on the disk..."); DiskWrite(disk_buf_A, 0, 5, 0, 1, &status); USLOSS_Console("ChildDW0(): DiskWrite0 returned status = %d\n", status); return 0; } /* ChildDW0 */
int ChildDR1(char *arg) { int status; char disk_buf_B[512]; USLOSS_Console("\nChildR1(): reading from disk 1, track 5, sector 0\n"); DiskRead(disk_buf_B, 1, 5, 0, 1, &status); USLOSS_Console("ChildR1(): DiskRead returned status = %d\n", status); USLOSS_Console("ChildR1(): disk_buf_B contains:\n%s\n", disk_buf_B); return 0; } /* ChildDR1 */
int P3_Startup(void *notused) { int pid; USLOSS_Console("P3_Startup\n"); int result = Sys_Spawn("P4_Startup", P4_Startup, NULL, 4 * USLOSS_MIN_STACK, 3, &pid); printf("P3 pid changedi(P4 pid): %d\n", pid); Sys_Wait(&pid, &result); Sys_DumpProcesses(); USLOSS_Console("P3_Finished\n"); return 0; }
int PatientProc2(void *arg) { int handle = *((int *) arg); USLOSS_Console("PatientProc2 created\n"); Sys_SemP(handle); USLOSS_Console("PatientProc2 clears P on semaphore with initial value 2\n"); USLOSS_Console("PatientProc2 DOES NOT V on semaphore with initial value 2\n"); return 5; }