diff --git a/allocate.c b/allocate.c index d10457f7306551b24960441388fc25f8c21cf277..72ad624a4640540aa40468ae3d68eda78b12abee 100644 --- a/allocate.c +++ b/allocate.c @@ -46,6 +46,8 @@ int leastTimeRemaining(process *p, process *q); int leastExecutionTime(process *p, process *q); +int leastCompletionTime(process *p, process *q); + void calStats(llist *completedProcesses, int *currentTime, float *aveTurnaroundTime, float *maxOverhead, float *averageOverhead); @@ -176,7 +178,7 @@ int main(int argc, char *argv[]) { process * currProc; if (currNode) currProc = currNode->data; while ( currNode ){ - if ( currProc->timeCompleted >= currentTime ) { + if ( currProc!= NULL && currProc->timeCompleted >= currentTime ) { printf("%d,FINISHED,pid=%s,proc_remaining=%d\n", currProc->timeCompleted, currProc->processID, numProcessesLeft); @@ -230,6 +232,18 @@ int main(int argc, char *argv[]) { CPUs[j].totalExecutionTimeRemaining -= deltaTime; } } + // Log finished processes. + struct node * currNode = *completedProcesses; //(struct node *) + headData(completedProcesses); + process * currProc = currNode->data; + while ( currNode ){ + if ( currProc!= NULL && currProc->timeCompleted >= currentTime ) { + printf("%d,FINISHED,pid=%s,proc_remaining=%d\n", + currProc->timeCompleted, currProc->processID, numProcessesLeft); + } + currNode = currNode->next; + if (currNode) currProc = currNode->data; + } // Log process starts for (int j = 0; j < numCPU; j++) { headProcess = headData(CPUs[j].processQueue); @@ -274,16 +288,47 @@ void printQEntry(process *p) { else printf("EMPTY LIST\n"); } +void headNULL(llist * p){ + struct node * currNode = * p; + currNode->data = NULL; + currNode->next = NULL; +} + int leastTimeRemaining(process *p, process *q) { - if (p->timeRemaining < q->timeRemaining) + int pid = atoi(p->processID); + int qid = atoi(q->processID); + + + if (p->timeRemaining < q->timeRemaining){ + return -1; //true + }else if(p->executionTime == q->executionTime && + pid<qid) { return -1; + } else return 0; } int leastExecutionTime(process *p, process *q) { - if (p->executionTime < q->executionTime || p->executionTime == q->executionTime && - strcmp(p->processID, q->processID )) + int pid = atoi(p->processID); + int qid = atoi(q->processID); + + if (p->executionTime < q->executionTime ){ return -1; //true + }else if(p->executionTime == q->executionTime && + pid<qid) { + return -1; + }else return 0; //false +} + +int leastCompletionTime(process *p, process *q) { + int pid = atoi(p->processID); + int qid = atoi(q->processID); + + if (p->timeCompleted < q->timeCompleted ){ + return -1; //true + }else if(p->timeCompleted == q->timeCompleted && pid<qid){ + return -1; //true + } else return 0; //false } @@ -364,12 +409,8 @@ void advanceProcessQueue(llist *processQueue, int currentTime, int deltaTime, ll *numProcessComplete += 1; paralizedProcess->timeCompleted = currentTime - deltaTime; paralizedProcess->timeRemaining = 0; - headProcess->timeRemaining = 0; - //printf("process had finished: Current time: %d, deltaTime: %d\n", currentTime, deltaTime); -// printf("%d,FINISHED,pid=%s,proc_remaining=%d\n", -// paralizedProcess->timeCompleted, paralizedProcess->processID, *numProcessesLeft); - // Could accumulate statistics here instead of keeping list of completed processes - llist_push(processesComplete, paralizedProcess); + llist_add_inorder( paralizedProcess,processesComplete ,(int (*)(void *, void *)) &leastCompletionTime ); + //llist_push(processesComplete, paralizedProcess); } llist_pop(processQueue); headProcess = headData(processQueue); @@ -385,7 +426,9 @@ void advanceProcessQueue(llist *processQueue, int currentTime, int deltaTime, ll // printf("%d,FINISHED,pid=%s,proc_remaining=%d\n", // headProcess->timeCompleted, headProcess->processID, *numProcessesLeft); // Could accumulate statistics here instead of keeping list of completed processes - llist_push(processesComplete, headProcess); + llist_add_inorder(headProcess, processesComplete, (int (*)(void *, void *)) &leastCompletionTime ); + + //llist_push(processesComplete, headProcess); llist_pop(processQueue); headProcess = headData(processQueue); if (headProcess){ @@ -455,7 +498,6 @@ process *getNextProcessB(FILE *f, llist *arrivals, process *readAhead) { newProcess = malloc(sizeof(process)); memcpy(newProcess, readAhead, sizeof(process)); llist_add_inorder(newProcess, arrivals, (int (*)(void *, void *)) &leastExecutionTime); - //llist_print(); while (readAhead->timeArrived != -1 && !batchEnd) { if (fgets(line, sizeof line, f) != NULL) { newProcess = malloc(sizeof(process));