void free_Procs(){ int i; for( i= 0; i <P1_MAXPROC; i++){ if(procTable[i].state == 3 && procTable[i].notFreed) { freeProc(i); if(procTable[i].isOrphan){ removeProc(i); } //USLOSS_Console("Freed %s\n",procTable[i].name); } } }
// remove proc from linked list of procedures based on generic id pid_t find(proc **headP, int id) { if (jobs == 1) { printf("No current background jobs\n"); return (pid_t)(-1); } int p = 0; proc *current = *headP; while (current != NULL) { if (p == id) return removeProc(current); else { current = current->next; p++; } } printf("No process with id %d exists\n", id); return (pid_t)(-1); }
/*Join: Causes current Process to wait for first child to quit*/ int P1_Join(int *status){ Check_Your_Privilege(); if(procTable[currPid].numChildren == 0){ return -1; } int i; /*get the PID of the child that quit*/ P1_Semaphore sema = &semTable[currPid]; // USLOSS_Console("Join:Before P\n"); P1_P(sema); // USLOSS_Console("Join:After P\n"); for( i= 0; i < P1_MAXPROC; i++){ if(procTable[i].parent==currPid&&procTable[i].state==3){ *status=procTable[i].status; procTable[currPid].numChildren--; removeProc(i); return i; } } return -2; }
/* ------------------------------------------------------------------------ Name - P1_Quit Purpose - Causes the process to quit and wait for its parent to call P1_Join. Parameters - quit status Returns - nothing Side Effects - the currently running process quits ------------------------------------------------------------------------ */ void P1_Quit(int status) { Check_Your_Privilege(); // USLOSS_Console("In quit PID -- before: %d\n", currPid); int i; for (i = 0; i < P1_MAXPROC; i++) { if(procTable[i].parent==currPid){ procTable[i].isOrphan = 1; // orphanize the children } } // USLOSS_Console("Process: %s Quitting pid=%d\n",procTable[currPid].name,currPid); procTable[currPid].state = 3; procTable[currPid].status = status; numProcs--; // USLOSS_Console("Process state: %d\n", procTable[currPid].state); // USLOSS_Console("PID : %d\n", currPid); /*Remove from Ready List*/ removeFromList(currPid); /*Add to blocked List*/ addToQuitList(currPid); // USLOSS_Console("Orphan Status: %d",procTable[currPid].isOrphan); /*remove if orphan*/ if(procTable[currPid].isOrphan){//||procTable[currPid].parent==-1){ removeProc(currPid); }else{ P1_Semaphore semi = &semTable[procTable[currPid].parent]; // USLOSS_Console("Quit P1_V for %d",currPid); P1_V(semi); } //USLOSS_Console("In quit PID -- after: %d\n", currPid); // USLOSS_Console("Number of processes: %d\n", numProcs); dispatcher(); }
// checks if command is built in; if so, executes it int isBuiltIn(char *args[], int cnt) { int i, id; int stat=0, exists=0, pStatus=0, len=0; char cwd[pathconf(".", _PC_PATH_MAX)]; // exit if (strcmp(args[0], "exit") == 0) cleanUp(); // cd else if (strcmp(args[0], "cd") == 0) { if (cnt == 1) args[1] = getenv("HOME"); pStatus = chdir(args[1]); if (pStatus == -1) { printf("cd: \'%s\': No such file or directory\n", args[1]); pos--; } } // pwd else if (strcmp(args[0], "pwd") == 0) { if (cnt != 1) printf("pwd: Too many args"); else { if (getcwd(cwd, pathconf(".", _PC_PATH_MAX)) == cwd) printf("%s\n", cwd); else { printf("pwd: Error getting pwd"); pos--; } } } // print history else if (strcmp(args[0], "history") == 0) { for (i=1; i<MAX_HISTORY+1; i++) { if ((pos-i) < 1) break; printf("command %d: %s\n", (pos-i), hist[(pos-i-1)%(MAX_HISTORY+1)].lineptr); } } // access command in history else if (strcmp(args[0], "r") == 0) { if (cnt > 2) { printf("r: Cannot have more than 1 argument\n"); pos--; } else if ((cnt == 2) && (strlen(args[1]) != 1)) { printf("r: Faulty argument\n"); pos--; } else if (pos < 2) { printf("r: No recent commands\n"); pos--; } else { if (cnt == 1) exists = pos-2; else { exists = -1; for (i=2; i<=MAX_HISTORY+1; i++) { if (strncmp(hist[(pos-i)%(MAX_HISTORY+1)].lineptr, args[1], 1) == 0) { exists = pos-i; break; } } if (exists < 0) { printf("r: No recent command starting with \'%s\'\n", args[1]); pos--; return stat; } } printf("running command %d: %s\n", (exists+1), hist[exists%(MAX_HISTORY+1)].lineptr); getcmdFromHist(args, hist[exists%(MAX_HISTORY+1)].lineptr); stat = 1; // to run thru again } } // fg else if (strcmp(args[0], "fg") == 0) { if (cnt != 2) { printf("fg: Faulty argument\n"); pos--; } else if (jobs == 1) { printf("fg: No current background jobs\n"); pos--; } else { if (sscanf(args[1], "%d", &id) != 1) { printf("fg: Faulty argument\n"); pos--; } else { pid_t cpid = find(&headP, id); if (!(cpid == -1)) { kill(cpid, SIGCONT); waitpid(cpid, &pStatus, WUNTRACED); if (pStatus == -1) printf("An error occured in waiting for child proccess %d\n", id); } } } } // jobs else if (strcmp(args[0], "jobs") == 0) { proc *current = headP->next; if (jobs != 1) { int idx = 0; while (current != NULL) { if (current->pid != 0) { pid_t result = waitpid(current->pid, &pStatus, WNOHANG); if (result == 0) printf("[%d]\t%d\t%s\n", (idx), current->pid, current->command); else removeProc(current); } current = current->next; idx++; } } } else stat = -1; // not built-in return stat; }
/*Method to run a first come first serve scheduling routine*/ int runFCFS(Processes *proc, int time_interval) { if(proc == NULL) { DEBUGPRINTF("ERROR: No process list passed to FCFS\n"); exit(1); } RunningProcesses * rp = createRunningProcesses(); addProc(rp,0); int runTime = time_interval; Node * temp = NULL; //decalre phase int block = 0; int arrival = 0; int proc_arrival = 0; int current_time = 0; int finish = 0; int runProcReturn = 0; //initilize variables for printing int startArray[10000]; int endArray[10000]; int runArray[10000]; int blocksArray[10000]; double upArray[10000]; int totalIdle = 0; int j = 0; for(j = 0; j <10000; j++) { startArray[j] = 0; endArray[j]= 0; runArray[j]=0; blocksArray[j]=0; upArray[j]=0; } while(rp->size != 0)//while there are still jobs to be run { //reset variables block = 0; arrival = 0; proc_arrival = 0; finish = 0; runProcReturn = 0; // temp = rp->head; runTime = time_interval; //find the shortest blocktime to run up too while (temp != NULL && temp->isBlocked != 0)//0 is not blocked 1 is blocked { if (temp->remainingBlockTime < runTime) { runTime = temp->remainingBlockTime; } temp = temp->next; } //NOTE //If TEMP==NULL then we can do run_noProc_check else if temp is a proc then do run_proc if (temp != NULL)//a process was found that could be run { runProcReturn = run_proc(proc, temp->procID, runTime, &block, &finish, current_time, &arrival, &proc_arrival);//run the process } else//there are no processes that can be run { runProcReturn = proc_norun_check_arrival(proc, time_interval, current_time, &arrival, &proc_arrival); totalIdle += runProcReturn; } if (runProcReturn != -1) { current_time += runProcReturn;//increment current time } //arrival found if (arrival == 1) { addProc(rp,proc_arrival); startArray[proc_arrival] = current_time; } //block found if(block == 1) { temp->isBlocked = 1; blocksArray[temp->procID]++; } //finish process if (finish == 1) { endArray[temp->procID] = current_time; upArray[temp->procID] = runArray[temp->procID] / endArray[temp->procID]; runArray[temp->procID] = endArray[temp->procID] - startArray[temp->procID]; removeProc(rp,temp->procID); } if (rp->size != 0) // set temp back to the head { temp = rp->head; } //increment all times while (temp != NULL && temp->isBlocked != 0 && runProcReturn != -1)//0 is not blocked 1 is blocked { if (temp->isBlocked == 1) { temp->remainingBlockTime -= runProcReturn; } if(temp->remainingBlockTime <= 0)//unblock processthat have done there time in the slammer { temp->isBlocked = 0; temp->remainingBlockTime = 200; } temp = temp->next; } } // Fill in with your stats! int i = 0; printf("********** SUMMARY ***********\n"); printf("\tStart\tend\trun\tblocks\tutil percent\n"); int sumRun = 0; for(i=0;endArray[i] != 0;i++) { upArray[i] = (double)runArray[i] / (double) endArray[i]; printf("PID%d:\t%d\t%d\t%d\t%d\t%1.3f\n",i,startArray[i],endArray[i],runArray[i],blocksArray[i],upArray[i]); sumRun += runArray[i]; } printf("Mean Time to Finish:\t%d\n",(sumRun / i)); printf("Total idle time:\t%d\n",totalIdle); return 0; }