int test_allocFreeNull(void) { int success = 1; int i; initProc(); /* Make sure freeing NULL is invalid. */ freePcb(NULL); success &= getFreeProcessCount() == MAXPROCESS; /* Make sure you cannot allocate more than MAXPROCESS */ for (i = 0; i < MAXPROCESS; i++) { allocPcb(); } success &= allocPcb() == NULL; success &= allocPcb() == NULL; initProc(); /* Make sure an invalid freePcb doesn't alter the state of free * processes. */ allocPcb(); success &= getFreeProcessCount() == MAXPROCESS - 1; freePcb(NULL); success &= getFreeProcessCount() == MAXPROCESS - 1; return success; }
/*TerminatePcb si preoccupa di terminare ricorsivamente tutta la progenie di p, root dell'albero di processi*/ void terminatePcb(pcb_t *p){ semd_t* semd; if(p->p_first_child != NULL) terminatePcb(p->p_first_child); if(p->p_first_child == NULL && p->p_sib != NULL) terminatePcb(p->p_sib); if(p->p_first_child == NULL && p->p_sib == NULL){ lock(MUTEX_SCHEDULER); if(outBlocked(p)){ /*Rimuovo p dalla coda dei processi del suo semaforo*/ lessSoftCounter(); /*Decremento il softBlock counter*/ semd = getSemd(p->p_semkey); /*Se p è bloccato su un semaforo di un device, non incremento il value. Sarà l'interrupt a farlo*/ if(!checkSemaphore(p)) (*semd->s_key)++; } /*Tolgo p dalla ReadyQueue, se presente*/ outProcQ(&ready_queue[getPRID()], p); outChild(p); freePcb(p); unlock(MUTEX_SCHEDULER); } }
int test_allocFreeCount(void) { int success = 1; pcb_t *procs[MAXPROCESS]; int i; initProc(); for (i = 1; i <= MAXPROCESS; ++i) { int j = 0; while (j < i && j < MAXPROCESS) { procs[j++] = allocPcb(); } success &= getFreeProcessCount() == MAXPROCESS - i; while (j > 0) { freePcb(procs[--j]); } success &= getFreeProcessCount() == MAXPROCESS; } return success; }
/** @brief Recursively terminates a process and its progeny. @param process Pointer to the Process Control Block. @return Void. */ HIDDEN void _terminateProcess(pcb_t *process) { /* As long as the process has children, remove the first one and perform a recursive call */ while (!emptyChild(process)) _terminateProcess(removeChild(process)); /* If the process is blocked on a semaphore */ if (process->p_semAdd) { /* If it is the Pseudo-Clock semaphore or a non-device semaphore, and its value is negative */ if ((process->p_semAdd == &PseudoClock || !process->p_isBlocked) && (*process->p_semAdd) < 0) (*process->p_semAdd)++; /* Update the value */ /* Extract the process from the semaphore */ if (!outBlocked(process)) PANIC(); /* Anomaly */ /* In case of a device semaphore, update the Soft Block Count */ if (process->p_isBlocked) SoftBlockCount--; } /* Decrease the number of active processes */ ProcessCount--; /* Insert the process block into the pcbFree list */ freePcb(process); }
/*Initializes the pcbs*/ void initPcbs(){ static pcb_t pcbs[MAXPROC]; int i = 0; freePcb_tp = mkEmptyProcQ(); while( i < MAXPROC){ freePcb(&pcbs[i]); i++; } }
/*Rimuove il PCB puntato da p dalla coda del semaforo su cui e’ bloccato outChildBlocked() termina anche tutti i processi discendenti di p. L'eliminazione avviene visitando l'albero come una DFS*/ void outChildBlocked(pcb_t *p){ semd_t* semd; if(outBlocked(p)){ /*Rimuovo p dalla coda dei processi del suo semaforo*/ lessSoftCounter(); /*Decremento il softBlock counter*/ semd = getSemd(p->p_semkey); /*Se p è bloccato su un semaforo di un device, non incremento il value. Sarà l'interrupt a farlo*/ if(!checkSemaphore(p)) (*semd->s_key)++; } outChild(p); /*Se p ha una progenie, terminatePcb si occupa di terminarla*/ if(p->p_first_child) terminatePcb(p->p_first_child); /*Elimina la progenie di p*/ lock(MUTEX_SCHEDULER); freePcb(p); /*Resetto i campi del pcb puntato da p che viene reinserito nella lista pcbFree*/ unlock(MUTEX_SCHEDULER); }
int main() { int i; initPcbs(); addokbuf("Initialized process control blocks \n"); /* Check allocPcb */ for (i = 0; i < MAXPROC; i++) { if ((procp[i] = allocPcb()) == NULL) adderrbuf("allocPcb(): unexpected NULL "); } if (allocPcb() != NULL) { adderrbuf("allocPcb(): allocated more than MAXPROC entries "); } addokbuf("allocPcb ok \n"); /* return the last 10 entries back to free list */ for (i = 10; i < MAXPROC; i++) freePcb(procp[i]); addokbuf("freed 10 entries \n"); /* create a 10-element process queue */ qa = mkEmptyProcQ(); if (!emptyProcQ(qa)) adderrbuf("emptyProcQ(qa): unexpected FALSE "); addokbuf("Inserting... \n"); for (i = 0; i < 10; i++) { if ((q = allocPcb()) == NULL) adderrbuf("allocPcb(): unexpected NULL while insert "); switch (i) { case 0: firstproc = q; break; case 5: midproc = q; break; case 9: lastproc = q; break; default: break; } insertProcQ(&qa, q); } addokbuf("inserted 10 elements \n"); if (emptyProcQ(qa)) adderrbuf("emptyProcQ(qa): unexpected TRUE" ); /* Check outProcQ and headProcQ */ if (headProcQ(qa) != firstproc) adderrbuf("headProcQ(qa) failed "); q = outProcQ(&qa, firstproc); if ((q == NULL) || (q != firstproc)) adderrbuf("outProcQ(&qa, firstproc) failed on first entry "); freePcb(q); q = outProcQ(&qa, midproc); if (q == NULL || q != midproc) adderrbuf("outProcQ(&qa, midproc) failed on middle entry "); freePcb(q); if (outProcQ(&qa, procp[0]) != NULL) adderrbuf("outProcQ(&qa, procp[0]) failed on nonexistent entry "); addokbuf("outProcQ() ok \n"); /* Check if removeProc and insertProc remove in the correct order */ addokbuf("Removing... \n"); for (i = 0; i < 8; i++) { if ((q = removeProcQ(&qa)) == NULL) adderrbuf("removeProcQ(&qa): unexpected NULL "); freePcb(q); } if (q != lastproc) adderrbuf("removeProcQ(): failed on last entry "); if (removeProcQ(&qa) != NULL) adderrbuf("removeProcQ(&qa): removes too many entries "); if (!emptyProcQ(qa)) adderrbuf("emptyProcQ(qa): unexpected FALSE "); addokbuf("insertProcQ(), removeProcQ() and emptyProcQ() ok \n"); addokbuf("process queues module ok \n"); addokbuf("checking process trees...\n"); if (!emptyChild(procp[2])) adderrbuf("emptyChild: unexpected FALSE "); /* make procp[1] through procp[9] children of procp[0] */ addokbuf("Inserting... \n"); for (i = 1; i < 10; i++) { insertChild(procp[0], procp[i]); } addokbuf("Inserted 9 children \n"); if (emptyChild(procp[0])) adderrbuf("emptyChild(procp[0]): unexpected TRUE "); /* Check outChild */ q = outChild(procp[1]); if (q == NULL || q != procp[1]) adderrbuf("outChild(procp[1]) failed on first child "); q = outChild(procp[4]); if (q == NULL || q != procp[4]) adderrbuf("outChild(procp[4]) failed on middle child "); if (outChild(procp[0]) != NULL) adderrbuf("outChild(procp[0]) failed on nonexistent child "); addokbuf("outChild ok \n"); /* Check removeChild */ addokbuf("Removing... \n"); for (i = 0; i < 7; i++) { if ((q = removeChild(procp[0])) == NULL) adderrbuf("removeChild(procp[0]): unexpected NULL "); } if (removeChild(procp[0]) != NULL) adderrbuf("removeChild(): removes too many children "); if (!emptyChild(procp[0])) adderrbuf("emptyChild(procp[0]): unexpected FALSE "); addokbuf("insertChild(), removeChild() and emptyChild() ok \n"); addokbuf("process tree module ok \n"); for (i = 0; i < 10; i++) freePcb(procp[i]); /* check ASL */ initASL(); addokbuf("Initialized active semaphore list \n"); /* check removeBlocked and insertBlocked */ addokbuf("insertBlocked() test #1 started \n"); for (i = 10; i < MAXPROC; i++) { procp[i] = allocPcb(); if (insertBlocked(&sem[i], procp[i])) adderrbuf("insertBlocked() test#1: unexpected TRUE "); } addokbuf("insertBlocked() test #2 started \n"); for (i = 0; i < 10; i++) { procp[i] = allocPcb(); if (insertBlocked(&sem[i], procp[i])) adderrbuf("insertBlocked() test #2: unexpected TRUE "); } /* check if semaphore descriptors are returned to free list */ p = removeBlocked(&sem[11]); if (insertBlocked(&sem[11],p)) adderrbuf("removeBlocked(): fails to return to free list "); if (insertBlocked(&onesem, procp[9]) == FALSE) adderrbuf("insertBlocked(): inserted more than MAXPROC "); addokbuf("removeBlocked() test started \n"); for (i = 10; i< MAXPROC; i++) { q = removeBlocked(&sem[i]); if (q == NULL) adderrbuf("removeBlocked(): wouldn't remove "); if (q != procp[i]) adderrbuf("removeBlocked(): removed wrong element "); if (insertBlocked(&sem[i-10], q)) adderrbuf("insertBlocked(3): unexpected TRUE "); } if (removeBlocked(&sem[11]) != NULL) adderrbuf("removeBlocked(): removed nonexistent blocked proc "); addokbuf("insertBlocked() and removeBlocked() ok \n"); if (headBlocked(&sem[11]) != NULL) adderrbuf("headBlocked(): nonNULL for a nonexistent queue "); if ((q = headBlocked(&sem[9])) == NULL) adderrbuf("headBlocked(1): NULL for an existent queue "); if (q != procp[9]) adderrbuf("headBlocked(1): wrong process returned "); p = outBlocked(q); if (p != q) adderrbuf("outBlocked(1): couldn't remove from valid queue "); q = headBlocked(&sem[9]); if (q == NULL) adderrbuf("headBlocked(2): NULL for an existent queue "); if (q != procp[19]) adderrbuf("headBlocked(2): wrong process returned "); p = outBlocked(q); if (p != q) adderrbuf("outBlocked(2): couldn't remove from valid queue "); p = outBlocked(q); if(c = p) addokbuf("saaaaaas \n"); if (p != NULL) adderrbuf("outBlocked(): removed same process twice."); if (headBlocked(&sem[9]) != NULL) adderrbuf("out/headBlocked: unexpected nonempty queue "); addokbuf("headBlocked() and outBlocked() ok \n"); addokbuf("ASL module ok \n"); addokbuf("So Long and Thanks for All the Fish\n"); return 0; }
int main() { int i; initPcbs(); addokbuf("Initialized Process Control Blocks \n"); /* Check allocPcb */ for (i = 0; i < MAXPROC; i++) { if ((procp[i] = allocPcb()) == NULL) adderrbuf("allocPcb(): unexpected NULL "); } if (allocPcb() != NULL) { adderrbuf(" ERROR: allocPcb(): allocated more than MAXPROC entries "); } addokbuf(" allocPcb test OK \n"); /* Return the last 10 entries back to free list */ for (i = 10; i < MAXPROC; i++) freePcb(procp[i]); addokbuf(" Added 10 entries to the free PCB list \n"); /* Create a 10-element process queue */ INIT_LIST_HEAD(&qa); if (!emptyProcQ(&qa)) adderrbuf("ERROR: emptyProcQ(qa): unexpected FALSE "); addokbuf("Testing insertProcQ ... \n"); for (i = 0; i < 10; i++) { if ((q = allocPcb()) == NULL) adderrbuf("ERROR: allocPcb(): unexpected NULL while insert "); switch (i) { case 3: q->priority=DEFAULT_PCB_PRIORITY; proc = q; break; case 4: q->priority=MAX_PCB_PRIORITY; maxproc = q; break; case 5: q->priority=MIN_PCB_PRIORITY; minproc=q; break; default: q->priority=DEFAULT_PCB_PRIORITY; break; } insertProcQ(&qa, q); } addokbuf("Test insertProcQ: OK. Inserted 10 elements \n"); if (emptyProcQ(&qa)) adderrbuf("ERROR: emptyProcQ(qa): unexpected TRUE" ); /* Check outProcQ and headProcQ */ if (headProcQ(&qa) != maxproc) adderrbuf("ERROR: headProcQ(qa) failed "); /* Removing an element from ProcQ */ q = outProcQ(&qa, proc); if ((q == NULL) || (q != proc)) adderrbuf("ERROR: outProcQ(&qa, proc) failed to remove the entry "); freePcb(q); /* Removing the first element from ProcQ */ q = removeProcQ(&qa); if (q == NULL || q != maxproc) adderrbuf("ERROR: removeProcQ(&qa, midproc) failed to remove the elements in the right order "); freePcb(q); /* Removing other 7 elements */ addokbuf(" Testing removeProcQ ... \n"); for (i = 0; i < 7; i++) { if ((q = removeProcQ(&qa)) == NULL) adderrbuf("removeProcQ(&qa): unexpected NULL "); freePcb(q); } // Removing the last element q=removeProcQ(&qa); if (q != minproc) adderrbuf("ERROR: removeProcQ(): failed on last entry "); freePcb(q); if (removeProcQ(&qa) != NULL) adderrbuf("ERROR: removeProcQ(&qa): removes too many entries "); if (!emptyProcQ(&qa)) adderrbuf("ERROR: emptyProcQ(qa): unexpected FALSE "); addokbuf(" Test insertProcQ(), removeProcQ() and emptyProcQ(): OK \n"); addokbuf(" Test process queues module: OK \n"); addokbuf(" Testing process trees...\n"); if (!emptyChild(procp[2])) adderrbuf("ERROR: emptyChild: unexpected FALSE "); /* make procp[1],procp[2],procp[3], procp[7] children of procp[0] */ addokbuf("Inserting... \n"); insertChild(procp[0], procp[1]); insertChild(procp[0], procp[2]); insertChild(procp[0], procp[3]); insertChild(procp[0], procp[7]); addokbuf("Inserted 2 children of pcb0 \n"); /* make procp[8],procp[9] children of procp[7] */ insertChild(procp[7], procp[8]); insertChild(procp[7], procp[9]); addokbuf("Inserted 2 children of pcb7 \n"); if (emptyChild(procp[0])) adderrbuf("ERROR: emptyChild(procp[0]): unexpected TRUE "); if (emptyChild(procp[7])) adderrbuf("ERROR: emptyChild(procp[0]): unexpected TRUE "); /* Check outChild */ q = outChild(procp[1]); if (q == NULL || q != procp[1]) adderrbuf("ERROR: outChild(procp[1]) failed "); q = outChild(procp[8]); if (q == NULL || q != procp[8]) adderrbuf("ERROR: outChild(procp[8]) failed "); /* Check removeChild */ q = removeChild(procp[0]); if (q == NULL || q != procp[2]) adderrbuf("ERROR: removeChild(procp[0]) failed "); q = removeChild(procp[7]); if (q == NULL || q != procp[9]) adderrbuf("ERROR: removeChild(procp[7]) failed "); q = removeChild(procp[0]); if (q == NULL || q != procp[3]) adderrbuf("ERROR: removeChild(procp[0]) failed "); q = removeChild(procp[0]); if (q == NULL || q != procp[7]) adderrbuf("ERROR: removeChild(procp[0]) failed "); if (removeChild(procp[0]) != NULL) adderrbuf("ERROR: removeChild(): removes too many children "); if (!emptyChild(procp[0])) adderrbuf("ERROR: emptyChild(procp[0]): unexpected FALSE "); addokbuf("Test: insertChild(), removeChild() and emptyChild() OK \n"); addokbuf("Testing process tree module OK \n"); freePcb(procp[0]); freePcb(procp[1]); freePcb(procp[2]); freePcb(procp[3]); freePcb(procp[4]); freePcb(procp[5]); freePcb(procp[6]); freePcb(procp[7]); freePcb(procp[8]); freePcb(procp[9]); /* check ASL */ initASL(); addokbuf("Initializing active semaphore list \n"); /* check removeBlocked and insertBlocked */ addokbuf(" Test insertBlocked(): test #1 started \n"); for (i = 10; i < MAXPROC; i++) { procp[i] = allocPcb(); if (insertBlocked(i, procp[i])) adderrbuf("ERROR: insertBlocked() test#1: unexpected TRUE "); } addokbuf("Test insertBlocked(): test #2 started \n"); for (i = 0; i < 10; i++) { procp[i] = allocPcb(); if (insertBlocked(i, procp[i])) adderrbuf("ERROR:insertBlocked() test #2: unexpected TRUE "); } /* check if semaphore descriptors are returned to the free list */ p = removeBlocked(11); if (insertBlocked(11,p)) adderrbuf("ERROR: removeBlocked(): fails to return to free list "); if (insertBlocked(MAXPROC+1, procp[9]) == FALSE) adderrbuf("ERROR: insertBlocked(): inserted more than MAXPROC "); addokbuf("Test removeBlocked(): test started \n"); for (i = 10; i< MAXPROC; i++) { q = removeBlocked(i); if (q == NULL) adderrbuf("ERROR: removeBlocked(): wouldn't remove "); if (q != procp[i]) adderrbuf("ERROR: removeBlocked(): removed wrong element "); } if (removeBlocked(11) != NULL) adderrbuf("ERROR: removeBlocked(): removed nonexistent blocked proc "); addokbuf("Test: insertBlocked() and removeBlocked() ok \n"); if (headBlocked(11) != NULL) adderrbuf("ERROR: headBlocked(): nonNULL for a nonexistent queue "); if ((q = headBlocked(9)) == NULL) adderrbuf("ERROR: headBlocked(1): NULL for an existent queue "); if (q != procp[9]) adderrbuf("ERROR: headBlocked(1): wrong process returned "); p = outBlocked(q); if (p != q) adderrbuf("ERROR: outBlocked(1): couldn't remove from valid queue "); /* Creating a 2-layer tree */ insertChild(procp[0], procp[1]); insertChild(procp[0], procp[2]); insertChild(procp[0], procp[3]); insertChild(procp[3], procp[4]); /* Testing outChildBlocked */ outChildBlocked(procp[0]); if (headBlocked(0) != NULL) adderrbuf("ERROR: outChildBlocked(): nonNULL for a nonexistent queue (0) "); if (headBlocked(1) != NULL) adderrbuf("ERROR: outChildBlocked(): nonNULL for a nonexistent queue (1) "); if (headBlocked(2) != NULL) adderrbuf("ERROR: outChildBlocked(): nonNULL for a nonexistent queue (2) "); if (headBlocked(3) != NULL) adderrbuf("ERROR: outChildBlocked(): nonNULL for a nonexistent queue (3) "); if (headBlocked(4) != NULL) adderrbuf("ERROR: outChildBlocked(): nonNULL for a nonexistent queue (4) "); if (headBlocked(5) == NULL) adderrbuf("ERROR: outChildBlocked(): NULL for an existent queue (5) "); addokbuf("Test headBlocked() and outBlocked(): OK \n"); addokbuf("ASL module OK \n"); addokbuf("So Long and Thanks for All the Fish\n"); return 0; }