Upload
farhan-ahmad
View
153
Download
0
Embed Size (px)
DESCRIPTION
C program for priority based pre-emptive CPU scheduling algorithm.
Citation preview
/******************************************************************** C implementation of Pre-emptive Priority based process scheduling* lower numerical value indicates higher priority* AUTHOR : AHMAD F.* Date : 2009.AUG.21* Version : 0.0* *******************************************************************/
#include<stdio.h>#include<stdlib.h>
/** Globals*/
int nprocess = 0; // holds total number of processes
struct node {int PID;
int ArrivalTime;int BurstTime;int Priority;
int RemBurst;
int WaitingTime;
struct node *ptr;};
typedef struct node NODE;
/** function prototypes*/
NODE *ReadProcessData(void);void SimulatePriority(NODE*);NODE *GetNextProcess(NODE*,int,int);void makewait(NODE*,NODE*,int,int);void SsortLL(NODE*);void PrintLL(NODE*);
void PrintGChart(int[100][2],int);
int main(){NODE *start;
start = ReadProcessData();
SimulatePriority(start);
exit(EXIT_SUCCESS);
} // end of main
/*** entry point of function ReadProcessData()*/
NODE *ReadProcessData(void){NODE *start;NODE *temp;NODE *NEW;
FILE *fp;
int pid,atime,btime,priority;
start = (NODE*)malloc(sizeof(NODE));
if(!start){printf("\nError in memiry allocation");exit(EXIT_FAILURE);}
start->ptr = NULL;
temp = start;
fp = fopen("priority.txt","r");
if(fp == NULL){printf("\nerror openong file \"priority.txt\"");exit(EXIT_FAILURE);}
while(1){fscanf(fp,"%d %d %d %d",&pid,&atime,&btime,&priority);
if(feof(fp)){fclose(fp);break;}
NEW = (NODE*)malloc(sizeof(NODE));
if(!NEW){printf("\nMemory allocation failed");fclose(fp);exit(EXIT_FAILURE);}NEW->ptr = NULL;
NEW->PID = pid;NEW->ArrivalTime = atime;NEW->BurstTime = btime;NEW->Priority = priority;NEW->RemBurst = btime;NEW->WaitingTime = 0;
temp->ptr = NEW;temp = temp->ptr;nprocess++;}
return(start);
} //end of ReadProcessData()
/** Entry point of function SimulatePriority()
*/
void SimulatePriority(NODE *list){NODE *current;int ctime = 0; //absolute clock timeint cpriority = 9999; //priority of current processint Tslice = 1; //quantum of time sliceint ncomplete = 0; //no of terminated processint GanttArray[100][2]; //An array to store Gantt chart infoint index = 0;
while( ncomplete != nprocess){current = GetNextProcess(list,ctime,cpriority);
if( current == NULL ){ctime += Tslice;cpriority = 9999;continue;
GanttArray[index][0] = -1;GanttArray[index][1] = ctime;index++;}
/** if current != NULL **/
GanttArray[index][0] = current->PID;GanttArray[index][1] = ctime;index++;
/** priority is now the priority of current* process*/
cpriority = current->Priority;
/** All processes that have arrived* but not terminated* except for the current process* must wait one time slice
*/makewait(list,current,ctime,Tslice);
/** decrease remaining burst time* of current process by one* time slice*/current->RemBurst -= Tslice;
/** increment clock time*/ctime += Tslice;
/** check if the current process* has no more burst remaining**/
if( current->RemBurst == 0){ncomplete++;cpriority = 9999;}} //End of while
SsortLL(list); //sort the list according to arrival timePrintLL(list); //Print scheduling dataPrintGChart(GanttArray,index);
} //end of SimulatePriority()
/** Entry point of makewait()*/
void makewait(NODE *list, NODE *current, int ctime, int Tslice){NODE *temp = list->ptr;
while( temp != NULL ){/** ifthe process has arrived*/
if( temp->ArrivalTime <= ctime ){/** if the process has not terminated*/
if(temp->RemBurst != 0){/** if the process is not the* one in execution*/
if(temp != current){/** make this process wait* for a time slice*/
temp->WaitingTime += Tslice;
} //end of if#3} //end of if#2} //end of if #1
temp = temp->ptr;
} //end of while
} //end of makewait()
/** entry point for GetNextProcess()*/
NODE *GetNextProcess(NODE *list,int ctime, int priority){NODE *temp;NODE *next = NULL; //next process by default is NULL
temp = list->ptr;
while( temp != NULL){/** if this process has arrived*/if(temp->ArrivalTime <= ctime){/** but this process has not terminated*/
if(temp->RemBurst != 0 ){/** if this process has a priority* greater than or equal to* the current priority*/if(temp->Priority <= priority ){/** then current process should* be the next process*/
next = temp;priority = next->Priority;} //end of if#3
} //end of if #2
} //end of if #1
temp = temp->ptr;
} //end of while
return(next);
} //end of GetNextProcess()
/** entry point for SsortLL()*/void SsortLL(NODE *list){NODE *temp1 = list->ptr;NODE *temp2;NODE *swapbuf;
swapbuf = (NODE*)malloc(sizeof(NODE));
if(!swapbuf){printf("\nError allocating memory to swapbuf");exit(EXIT_FAILURE);}
while(temp1->ptr != NULL){temp2 = temp1->ptr;
while(temp2 != NULL){if(temp2->ArrivalTime < temp1->ArrivalTime){*swapbuf = *temp1;*temp1 = *temp2;*temp2 = *swapbuf;}
temp2 = temp2->ptr;}
temp1 = temp1->ptr;}
} //End of SsortLL()
/** Entry point for PrintLL()*/
void PrintLL(NODE *list){NODE *temp = list->ptr;int tatime;float avg_tatime = 0;float avg_wtime = 0;
printf("\n%3s%15s%15s%15s%15s%15s\n","PID","Arr. Time","Burst time","Priority","Waiting Time","Trn-arnd Tim");
while( temp != NULL){printf("\n%3d",temp->PID);printf("%15d",temp->ArrivalTime);printf("%15d",temp->BurstTime);printf("%15d",temp->Priority);printf("%15d",temp->WaitingTime);
tatime = temp->WaitingTime + temp->BurstTime;
printf("%15d",tatime);
avg_wtime += temp->WaitingTime;avg_tatime += tatime;
temp = temp->ptr;}
printf("\nAvg waiting time = %f",avg_wtime/nprocess);printf("\nAvg turn-around time = %f",avg_tatime/nprocess);
printf("\n");} //end of PrintLL()
/** entrypoint for printGChart()*/
void PrintGChart(int a[100][2],int i){
int count;int j;printf("\n\n");
printf("[%d]--P%d--",a[0][1],a[0][0]);for(count=1 ; count<i; count++){if(a[count][0] != a[count-1][0]){printf("[%d]",a[count][1]);
if(a[count][0] == -1){printf("%4d","idle");}
else{printf("--P%d--",a[count][0]);}}}printf("[%d]",a[i-1][1]+1);printf("\n");}