/** * Read job dispatch list from the file */ void readFile(char *fileName) { int counter = 0; FILE *fp; if ( (fp = freopen(fileName, "r", stdin)) ) { char buffer[MAX_BUFFER]; PcbPtr ps; while (!feof(fp) && counter <= 1000) { // up to maximum 1000 jobs if (fgets(buffer, MAX_BUFFER, fp)) { ps = createnullPcb(); /* initialize Pcb */ ps->arrivalTime = atoi(strtok(buffer, DELIMITER)); ps->priority = atoi(strtok(NULL, DELIMITER)); ps->remainingCpuTime = atoi(strtok(NULL, DELIMITER)); ps->memNeed = atoi(strtok(NULL, DELIMITER)); ps->io.printer = atoi(strtok(NULL, DELIMITER)); ps->io.scanner = atoi(strtok(NULL, DELIMITER)); ps->io.modem = atoi(strtok(NULL, DELIMITER)); ps->io.cd = atoi(strtok(NULL, DELIMITER)); inputQ = enqPcb(inputQ, ps); } counter++; } } else { error("Job dispatch list file is not found\n"); } }
/** description: read from input file. file - path of input file inputQ - head of input queue returns: TRUE on success, and ... inputQ ... FALSE if failed */ int readInput(const char *file, PcbPtr *inputQ) { FILE *fp = NULL; int arrivalTime, priority, cputime, memoryAlloc, res1, res2, res3, res4; PcbPtr newPcb = NULL; PcbPtr pre = NULL; fp = fopen(file, "r"); if (fp == NULL) { fprintf(stderr, "fopen error: %p\n", strerror(errno)); return FALSE; } *inputQ = NULL; while (fscanf(fp, "%d, %d, %d, %d, %d, %d, %d, %d", &arrivalTime, &priority, &cputime, &memoryAlloc, &res1, &res2, &res3, &res4) == 8) { newPcb = createnullPcb(); newPcb->pid = 0; newPcb->args[0] = "./process"; newPcb->args[1] = NULL; newPcb->arrivalTime = arrivalTime; newPcb->remainingCpuTime = cputime; newPcb->status = DEFAULT; newPcb->memAllocSize = memoryAlloc; newPcb->memBlock = NULL; newPcb->next = NULL; if (*inputQ == NULL) { *inputQ = newPcb; } else { pre->next = newPcb; } pre = newPcb; } if (!feof(fp)) { goto FAILURE; } fclose(fp); return TRUE; FAILURE: fclose(fp); return FALSE; }
int main (int argc, char *argv[]) { char * inputfile; // job dispatch file FILE * inputliststream; PcbPtr inputqueue = NULL; // input queue buffer PcbPtr currentprocess = NULL; // current process PcbPtr process = NULL; // working pcb pointer int timer = 0; // dispatcher timer // 0. Parse command line if (argc == 2) inputfile = argv[1]; else PrintUsage (stderr, argv[0]); // 1. Initialize dispatcher queue; // (already initialised in assignments above) // 2. Fill dispatcher queue from dispatch list file; if (!(inputliststream = fopen(inputfile, "r"))) { // open it SysErrMsg("could not open dispatch list file:", inputfile); exit(2); } while (!feof(inputliststream)) { // put processes into input_queue process = createnullPcb(); if (fscanf(inputliststream,"%d, %d, %d, %d, %d, %d, %d, %d", &(process->arrivaltime), &(process->priority), &(process->remainingcputime), &(process->mbytes), &(process->req.printers), &(process->req.scanners), &(process->req.modems), &(process->req.cds)) != 8) { free(process); continue; } process->status = PCB_INITIALIZED; inputqueue = enqPcb(inputqueue, process); } // 3. Start dispatcher timer; // (already set to zero above) // 4. While there's anything in the queue or there is a currently running process: while(inputqueue != NULL || currentprocess != NULL){//begin while // i. If a process is currently running; if(currentprocess != NULL){//begin if // a. Decrement process remainingcputime; currentprocess->remainingcputime--; // b. If times up: if(currentprocess->remainingcputime <=0){//begin if1 // A. Send SIGINT to the process to terminate it; currentprocess = terminatePcb(currentprocess); // B. Free up process structure memory free(currentprocess); printf("Successful"); }//end if1 }//end if // ii. If no process now currently running && // dispatcher queue is not empty && // arrivaltime of process at head of queue is <= dispatcher timer: if(currentprocess == NULL && inputqueue != NULL && inputqueue->arrivaltime <= timer){//begin if // a. Dequeue process and start it (fork & exec) // b. Set it as currently running process; process = deqPcb(&inputqueue); currentprocess = startPcb(process); }//end if // iii. sleep for one second; sleep(1); // iv. Increment dispatcher timer; timer++; // v. Go back to 4. continue; }//end while // 5. Exit exit (0); }
int main (int argc, char *argv[]) { char * inputfile; // job dispatch file FILE * inputliststream; PcbPtr inputqueue = NULL; // input queue buffer PcbPtr inputqueue_tail = NULL; PcbPtr curr = NULL; // for creating queue PcbPtr currentprocess = NULL; // current process PcbPtr process = NULL; // working pcb pointer int timer = 0; // dispatcher timer char buffer[75]; // for reading current line of file int notNeeded[3]; // for storing the unneeded arguements of input line // 0. Parse command line if (argc == 2) inputfile = argv[1]; else PrintUsage (stderr, argv[0]); // 1. Initialize dispatcher queue; // (already initialised in assignments above) // 2. Fill dispatcher queue from dispatch list file; if (!(inputliststream = fopen(inputfile, "r"))) { // open it SysErrMsg("could not open dispatch list file:", inputfile); exit(2); } inputqueue = createnullPcb(); curr = inputqueue; while (!feof(inputliststream)) { // put processes into input_queue // your code goes here // don't forget to initialize the PCB status // and put PCB on the queue // MH DONE curr->next = malloc(sizeof(Pcb)); curr->args[0] = DEFAULT_PROCESS; curr->args[1] = NULL; fgets(buffer,sizeof(buffer),inputliststream); sscanf(buffer, "%d, %d, %d, %d", &curr->arrivaltime, &curr->priority, &curr->remainingcputime, &curr->mbytes ); curr = curr->next; } inputqueue_tail = curr; // 3. Start dispatcher timer; // (already set to zero above) // 4. While there's anything in the queue or there is a currently running process: while (inputqueue || currentprocess ) {// i'll give you this outer loop, now do the rest /* i. If a process is currently running; a. Decrement process remainingcputime; b. If times up: A. Send SIGINT to the process to terminate it; B. Free up process structure memory ii. If no process currently running && dispatcher queue is not empty && arrivaltime of process at head of queue is <= dispatcher timer: a. Dequeue process and start it (fork & exec) b. Set it as currently running process; iii. sleep for one second; iv. Increment dispatcher timer; v. Go back to 4. */ //If a process is currently running; if(currentprocess) { //a. Decrement process remainingcputime; currentprocess->remainingcputime = currentprocess->remainingcputime - 1; // b. If times up: if(currentprocess->remainingcputime == 0 ) //A. Send SIGINT to the process to terminate it; //B. Free up process structure memory if(terminatePcb(currentprocess)!=NULL) free(currentprocess); } /* ii. If no process currently running && dispatcher queue is not empty && arrivaltime of process at head of queue is <= dispatcher timer: a. Dequeue process and start it (fork & exec) b. Set it as currently running process; */ if(!currentprocess && inputqueue!=NULL && inputqueue->arrivaltime < timer) { PcbPtr dequed = deqPcb(inputqueue); currentprocess = startPcb(dequed); } //iii. sleep for one second; sleep(1000); //increment dispatch timer timer++; /*printf("current process: %d, remainingcputime: $d, dispatch timer: $d", currentProcess->pid, currentProcess->remainingcputime, timer);*/ } // 5. Exit exit (0); }
int main (int argc, char *argv[]) { char * inputfile; // job dispatch file FILE * inputliststream; PcbPtr inputqueue = NULL; // input queue buffer PcbPtr currentprocess = NULL; // current process // PcbPtr process = NULL; // working pcb pointer int timer = 0; // dispatcher timer // 0. Parse command line if (argc == 2) inputfile = argv[1]; else PrintUsage (stderr, argv[0]); // 1. Initialize dispatcher queue; // (already initialised in assignments above) // 2. Fill dispatcher queue from dispatch list file; if (!(inputliststream = fopen(inputfile, "r"))) { // open it SysErrMsg("could not open dispatch list file: ", inputfile); exit(2); } PcbPtr nextPCB; while (!feof(inputliststream)) { // put processes into input_queue nextPCB = createnullPcb(); if(fscanf(inputliststream, "%d, %d, %d, %d, %d, %d, %d, %d", &(nextPCB->arrivaltime), &(nextPCB->priority), &(nextPCB->remainingcputime), &(nextPCB->mbytes), &(nextPCB->req.printers), &(nextPCB->req.scanners), &(nextPCB->req.modems), &(nextPCB->req.cds) )!= 8){ free(nextPCB); continue; } nextPCB->status = PCB_INITIALIZED; inputqueue = enqPcb(inputqueue, nextPCB); } // 3. Start dispatcher timer; // (already set to zero above) // 4. While there's anything in the queue or there is a currently running process: while (inputqueue || currentprocess ) { if(currentprocess){ (currentprocess->remainingcputime) -= 1; if(currentprocess->remainingcputime == 0){ // if(prevprocess == NULL){ // // this would have to be the first/only process, or head of the queue // // that would mean the last process added would be the previous process // nextPCB->next = currentprocess->next; // }else { // prevprocess->next = currentprocess->next; // } //send SIGINT to terminate process terminatePcb(currentprocess); free(currentprocess); currentprocess = NULL; } } if (!currentprocess && inputqueue && inputqueue->arrivaltime <= timer){ currentprocess = deqPcb(&inputqueue); if(currentprocess) startPcb(currentprocess); } sleep(1); timer++; } // 5. Exit exit (0); }
int main (int argc, char *argv[]) { char * inputfile; // job dispatch file FILE * inputliststream; PcbPtr inputqueue = NULL; // input queue buffer PcbPtr fbqueue[N_FB_QUEUES]; // feedback queues PcbPtr currentprocess = NULL; // current process PcbPtr process = NULL; // working pcb pointer int timer = 0; // dispatcher timer int quantum = QUANTUM; // current time-slice quantum int i; // working index // 0. Parse command line if (argc == 2) inputfile = argv[1]; else PrintUsage (stderr, argv[0]); // 1. Initialize dispatcher queues; for (i = 0; i < N_FB_QUEUES; fbqueue[i++] = NULL); // 2. Fill dispatcher queue from dispatch list file; if (!(inputliststream = fopen(inputfile, "r"))) { // open it SysErrMsg("could not open dispatch list file:", inputfile); exit(2); } while (!feof(inputliststream)) { // put processes into input_queue process = createnullPcb(); if (fscanf(inputliststream,"%d, %d, %d, %d, %d, %d, %d, %d", &(process->arrivaltime), &(process->priority), &(process->remainingcputime), &(process->mbytes), &(process->req.printers), &(process->req.scanners), &(process->req.modems), &(process->req.cds)) != 8) { free(process); continue; } process->status = PCB_INITIALIZED; inputqueue = enqPcb(inputqueue, process); } // 3. Start dispatcher timer; // (already set to zero above) //initialize memory MabPtr memall = createnullMab(1024); //split memory into "protected" Real Time process memory and User Memory MabPtr rtmem = memSplit(memall, 64); MabPtr mem = rtmem->next; //work with "mem" as user memory area mem->justused = 1; //initialize, the "justused" represents the most recently used, and the algorithm assumes one exists no matter what PcbPtr feedbackpending = NULL; // processes waiting for memory // 4. While there's anything in any of the queues or there is a currently running process: while (inputqueue || (CheckQueues(fbqueue) >= 0) || currentprocess ) { // i. Unload any pending processes from the input queue: // While (head-of-input-queue.arrival-time <= dispatcher timer) // dequeue process from input queue and and enqueue on feebackpending queue while (inputqueue && inputqueue->arrivaltime <= timer) { process = deqPcb(&inputqueue); // dequeue process process->status = PCB_READY; // set pcb ready process->priority = 0; // override any priority feedbackpending = enqPcb(feedbackpending, process); // & put on queue } PcbPtr nextprocess = feedbackpending; //unload pending processes from user job queue: //while (feedbackpending->mbytes is free somewhere in user mem: while(feedbackpending && memChk(mem, feedbackpending->mbytes)){ nextprocess = deqPcb(&feedbackpending); // dequeue process from user job queue nextprocess->memoryblock = memAlloc(mem, nextprocess->mbytes); // allocate memory to the process nextprocess->priority = 0; fbqueue[nextprocess->priority] = enqPcb(fbqueue[nextprocess->priority], nextprocess); } puts("got it"); // iii. If a process is currently running; if (currentprocess) { currentprocess->remainingcputime -= quantum; if (currentprocess->remainingcputime <= 0) { terminatePcb(currentprocess); memFree(currentprocess->memoryblock); free(currentprocess); currentprocess = NULL; // c. else if other processes are waiting in feedback queues: } else if (CheckQueues(fbqueue) >= 0) { suspendPcb(currentprocess); if (++(currentprocess->priority) >= N_FB_QUEUES) currentprocess->priority = N_FB_QUEUES - 1; fbqueue[currentprocess->priority] = enqPcb(fbqueue[currentprocess->priority], currentprocess); currentprocess = NULL; } } // iv. If no process currently running && feedback queues are not empty: if (!currentprocess && (i = CheckQueues(fbqueue)) >= 0) { currentprocess = deqPcb(&fbqueue[i]); startPcb(currentprocess); } // v. sleep for quantum; quantum = currentprocess && currentprocess->remainingcputime < QUANTUM ? currentprocess->remainingcputime : !(currentprocess) ? 1 : QUANTUM; sleep(quantum); timer += quantum; } exit (0); }