diff --git a/allocate.c b/allocate.c index e2aba4b7fad9fc72d73acb1df90691ca9c23da62..13776a7b773f5a489eccb685b58471a59653dae2 100644 --- a/allocate.c +++ b/allocate.c @@ -49,6 +49,7 @@ void calStats(llist *completedProcesses, int *currentTime, float *aveTurnaroundT void addProcessToQueue(llist *processQueue, process *newProcess, int currentTime, int cpuID); process *getNextProcess(FILE *f, llist *arrivals, process *readAhead); +process *getNextProcessB(FILE *f, llist *arrivals, process *readAhead); int main(int argc, char *argv[]) { @@ -107,7 +108,7 @@ int main(int argc, char *argv[]) { //while (fgets(line, sizeof line, f) != NULL) { - while (newProcess = getNextProcess(f, arrivalLounge, readAhead)) { + while (newProcess = getNextProcessB(f, arrivalLounge, readAhead)) { //printf("process id:%s process arrival time: %d\n", newProcess->processID, newProcess->timeArrived); @@ -484,14 +485,16 @@ process *getNextProcess(FILE *f, llist *arrivals, process *readAhead) { //add to arrivals until the readAhead is different //memcpy(nextProcess,readAhead, sizeof(process)); llist_add_inorder(readAhead, arrivals, (int (*)(void *, void *)) &leastExecutionTime); + llist_print(arrivals,(void (*)(void *)) &printQEntry); printf("readAhead: %s\n", readAhead->processID); - printf("New Process: %s\n", readAhead->processID); + printf("New Process: %s\n", newProcess->processID); memcpy(readAhead,newProcess,sizeof(process)); printf("in if\n"); llist_print(arrivals,(void (*)(void *)) &printQEntry); printf("readAhead: %s\n", readAhead->processID); int count = 0; + llist_print(arrivals, (void (*)(void *)) &printQEntry); while (newProcess->timeArrived == readAhead->timeArrived ){ //readAhead = nextProcess; //memcpy(nextProcess,readAhead, sizeof(process)); @@ -505,7 +508,7 @@ process *getNextProcess(FILE *f, llist *arrivals, process *readAhead) { if(fgets(line, sizeof line, f) == NULL){ printf("in while, end of file\n"); llist_print(arrivals,(void (*)(void *)) &printQEntry); - readAhead->timeArrived = -1; + readAhead->timeArrived = -1; // Flag EOF return llist_pop(arrivals); } initializeProcess(line,newProcess); @@ -513,7 +516,7 @@ process *getNextProcess(FILE *f, llist *arrivals, process *readAhead) { printf("New Process: %s\n", readAhead->processID); } - llist_print(arrivals, (void (*)(void *)) &printQEntry); +// llist_print(arrivals, (void (*)(void *)) &printQEntry); //readAhead = nextProcess; memcpy(readAhead,nextProcess,sizeof(process)); @@ -529,4 +532,36 @@ process *getNextProcess(FILE *f, llist *arrivals, process *readAhead) { } +} + +process *getNextProcessB(FILE *f, llist *arrivals, process *readAhead) { + char line[36]; + + process *newProcess; + int batchEnd; + + if (arrivals != NULL) { + if (headData(arrivals) == NULL) {// no buffered processes so...{ + // queue the next batch of arrivals - starting with the read ahead. + newProcess = malloc(sizeof(process)); + memcpy(newProcess, readAhead, sizeof(process)); + llist_add_inorder(newProcess, arrivals, (int (*)(void *, void *)) &leastExecutionTime); + while (readAhead->timeArrived != -1 && !batchEnd) { + if (fgets(line, sizeof line, f) != NULL) { + initializeProcess(line, newProcess); + if (newProcess->timeArrived == readAhead->timeArrived) { + llist_add_inorder(newProcess, arrivals, (int (*)(void *, void *)) &leastExecutionTime); + } else { + batchEnd = -1; + memcpy(readAhead, newProcess, sizeof(process)); + } + } else { + readAhead->timeArrived = -1; + } + } + } + // now there's stuff in the arrivals lounge or there are no more to arrive + } + llist_print(arrivals, (void (*)(void *)) &printQEntry); + return llist_pop(arrivals); } \ No newline at end of file