int* waitingTime; int* turnaroundTime; int* completeTime; int* serviceTime; int* ganttSequence; int totalCompleteTime; int totalWaitingTime; int totalTurnaroundTime;
// Get the process with the lowest priority in the waiting queue // in case of tie, the one with the lowest process number will be returned intgetPrio(int* inPr, int* inWait, int num){ int i; int maxPr=-1; for (i=0;i<num;i++) { if (inPr[i]>maxPr) { maxPr=inPr[i]; } } int min=maxPr+1; int prio=-1; for (i=0;i<num;i++) { if (inWait[i]==1 && inPr[i]<min) { min=inPr[i]; prio=i; } } return prio+1; }
voidPR(int* inArrivalTime, int* inBurstTime, int* inPriority, int inN, int isPreemptive) { int num = inN; int isWaiting[num]; int burstTime[num]; int k; for (k=0; k<num; k++) { burstTime[k]=inBurstTime[k]; isWaiting[k]=0; totalCompleteTime+=inBurstTime[k]; } totalCompleteTime = totalCompleteTime>(inArrivalTime[num-1]+inBurstTime[num-1])?totalCompleteTime:(inArrivalTime[num-1]+inBurstTime[num-1]); int currentTime=0; // the clock waitingTime = (int*) malloc(sizeof(int)*num); waitingTime[0]=0; serviceTime=(int*) malloc(sizeof(int)*num); serviceTime[0]=0; turnaroundTime = (int*) malloc(sizeof(int)*num); completeTime = (int*) malloc(sizeof(int)*num); ganttSequence = (int*) malloc(sizeof(int)*totalCompleteTime);
int processInRun=-1; int processInArrive; while (currentTime<=totalCompleteTime) { // complete if (processInRun!=-1 && burstTime[processInRun-1]==0) { completeTime[processInRun-1]=currentTime; turnaroundTime[processInRun-1]=completeTime[processInRun-1]-inArrivalTime[processInRun-1]; isWaiting[processInRun-1]=0; processInRun=-1; } // arrive int i; for (i=0;i<num;i++) { if (inArrivalTime[i]==currentTime) { // new process arrive isWaiting[i]=1; if (isPreemptive == 1 && processInRun!=-1 && inPriority[i]<inPriority[processInRun-1]) { // take over isWaiting[processInRun-1]=1; isWaiting[i]=0; processInRun=i+1; // preemptive take over } break; } } // new run if (processInRun==-1) { processInRun=getPrio(inPriority, isWaiting, num); // get the boss if (processInRun!=-1) { isWaiting[processInRun-1]=0; } } // get service time if (processInRun!=-1 && inBurstTime[processInRun-1] == burstTime[processInRun-1]) { serviceTime[processInRun-1]=currentTime; } // decrease burst time if (processInRun!=-1) { burstTime[processInRun-1]--; } ganttSequence[currentTime]=processInRun; currentTime++; } int i; // get waiting time for (i=0;i<inN;i++) { waitingTime[i]=turnaroundTime[i]-inBurstTime[i]; } for (i=0;i<inN;i++) { totalWaitingTime += waitingTime[i]; totalTurnaroundTime += turnaroundTime[i]; } }
voidupdatePriority(int* inPriority, int* inRemainingTime,int inN){ int* temp = (int*) malloc(sizeof(int)*inN); int i; for (i=0;i<inN;i++) { temp[i]=inRemainingTime[i]; } // sort the temp int j; for (i=0;i<inN;i++) { for (j=i+1;j<inN;j++) { if (temp[i]>temp[j]) { int t = temp[i]; temp[i]=temp[j]; temp[j]=t; } } } // assign priority for (i=0;i<inN;i++) { for (j=0;j<inN;j++) { if (inRemainingTime[i]==temp[j]) { inPriority[i]=j+1; break; } } } }
int* waitingTime; int* turnaroundTime; int* completeTime; int* serviceTime; int* ganttSequence; int totalCompleteTime; int totalWaitingTime; int totalTurnaroundTime;
voidRR(int inNum, int inArrivalArray[], int inServeArray[], int inQuantum) { int num=inNum; Queue* inArriveQueue=newQueue(num); // queue for processes in arriving Queue* waitingQueue=newQueue(num); // queue for processes in waiting int burstTime[num]; // burst time int k; for (k=0; k<num; k++) { burstTime[k]=inServeArray[k]; totalCompleteTime+=inServeArray[k]; } totalCompleteTime = totalCompleteTime>(inArrivalArray[num-1]+inServeArray[num-1])?totalCompleteTime:(inArrivalArray[num-1]+inServeArray[num-1]); int currentTime=0; // the clock int quantum=inQuantum; // the designed quantum int currentQuantum=0; // the current quantum waitingTime = (int*) malloc(sizeof(int)*num); waitingTime[0]=0; serviceTime=(int*) malloc(sizeof(int)*num); serviceTime[0]=0; turnaroundTime = (int*) malloc(sizeof(int)*num); completeTime = (int*) malloc(sizeof(int)*num); ganttSequence = (int*) malloc(sizeof(int)*totalCompleteTime); int* lastEnqueueTime = (int*) malloc(sizeof(int)*num);
int i; for (i=0; i<num; i++) { enqueue(inArriveQueue, i+1); } int processInRun; // store the current process in service int processInArrive=front(inArriveQueue); // first process in come dequeue(inArriveQueue); enqueue(waitingQueue, processInArrive); lastEnqueueTime[processInArrive-1]=inArrivalArray[processInArrive-1];
while(isQueueEmpty(waitingQueue)==-1 || isQueueEmpty(inArriveQueue)==-1) { processInRun=dequeue(waitingQueue); processInArrive=front(inArriveQueue); if (processInRun==-1) { currentTime=inArrivalArray[processInArrive-1]; processInRun=processInArrive; dequeue(inArriveQueue); } // get the service time if (burstTime[processInRun-1] == inServeArray[processInRun-1]) { serviceTime[processInRun-1]=currentTime; } // get the waiting time if (currentTime-lastEnqueueTime[processInRun-1]>0) { waitingTime[processInRun-1]+=currentTime-lastEnqueueTime[processInRun-1]; } // get time duration in this round int duration; int isQuantumUp; if (burstTime[processInRun-1]<=quantum) { duration=burstTime[processInRun-1]; isQuantumUp=0; } else { duration=quantum; isQuantumUp=1; } // gantt chart int j; for (j=0;j<duration;j++) { ganttSequence[currentTime+j]=processInRun; } // check arrival in the duration if (processInArrive!=-1) { int nextArrTime=inArrivalArray[processInArrive-1]; while (1) { if (nextArrTime>currentTime+duration) { break; } elseif (nextArrTime<=currentTime+duration && nextArrTime>currentTime) { enqueue(waitingQueue, processInArrive); lastEnqueueTime[processInArrive-1]=nextArrTime; dequeue(inArriveQueue); } processInArrive=front(inArriveQueue); if (processInArrive==-1) { break; } nextArrTime=inArrivalArray[processInArrive-1]; } } // finish if (isQuantumUp==0) { burstTime[processInRun-1]=0; completeTime[processInRun-1]=currentTime+duration; // get the complete time processInRun=0; } // quantum time up if (isQuantumUp==1) { burstTime[processInRun-1]-=duration; enqueue(waitingQueue, processInRun); lastEnqueueTime[processInRun-1]=currentTime+duration; processInRun=0; } currentTime+=duration; } for (i=0;i<num;i++) { turnaroundTime[i] = completeTime[i] - inArrivalArray[i]; } for (i=0;i<num;i++) { totalWaitingTime += waitingTime[i]; totalTurnaroundTime += turnaroundTime[i]; } }