Write the programs to implement in C Code FCFS First Come First Served Process Scheduling CPU scheduling algorithms. Take the inputs, Process name, Arrival time of the process, CPU execution time of the process. Use system timer to check the time quantum and display after every second the process running on the CPU or if the CPUis idle until all the inputted
processes complete their execution. Calculate waiting time, turn around time for each
process.
In this problem we can use time_t and get system time using time() which in the <time.h> header file so we need to import it also.Following Solution only for FCFS
/* Program For FCFS Implementation */ /*Program from Nullpointer.in*/ #include<stdio.h> #include<time.h> void setIdel(int ideltime,int processno); void cpuallocate(int exectime,int processno); time_t ptime; struct CpuSch { int pno,at,xt;//process no,arrival time ,execution time }; int main() { //No of process,waiting time,turn around time int processes,waitm[100],tartm[100]; printf("Please Enter No of Process\n"); scanf("%d",&processes); int i; struct CpuSch process[processes],temp; // Take i processesut for no of Process in Structure for(i=0;i<processes;i++) { printf("Please Enter Arrival time for %d\n",i+1); scanf("%d",&process[i].at); printf("Please Enter Execution time for %d\n",i+1); scanf("%d",&process[i].xt); process[i].pno=i+1; } int a,b; //Sorting Process According to Arrival time for(a=0;a<processes;a++) { for(b=0;b<processes-1;b++) { if(process[b].at>process[b+1].at) { temp=process[b]; process[b]=process[b+1]; process[b+1]=temp; } } } int j; /** FCFS for wait time tat**/ //waiting time ,turnAroundtime ,finish time, ideal time int waittm,tatm,finishtm,ideltm; waittm=0; tatm=0; finishtm=process[0].at; printf("\n******FCFS SCHEDULING******\n"); //printf("Process No\tArrival Time\tExecution Time\tWaitingTime\tTurnAroundTime\n"); //printf("----------\t------------\t--------------\t-----------\t--------------\n"); for(j=0;j<processes;j++) { ptime=time(NULL); printf("Process No %d start at %s",process[j].pno,ctime(&ptime)); ideltm=0; waitm[j]=finishtm-process[j].at; if(waitm[j]<0) { ideltm=(-1)*waitm[j]; waitm[j]=0; } if(ideltm>0) { setIdel(ideltm,process[j].pno); } if(process[j].xt>0) { cpuallocate(process[j].xt,process[j].pno); } finishtm=finishtm+process[j].xt+ideltm; tartm[j]=finishtm-process[j].at; //printf("%5d %5d %5d %5d %5d\n",j+1,process[j].at,process[j].xt,waittm,tatm); } printf("\tP.No ATime\tExTime WTime TATime\n"); //printf("----------\t------------\t--------------\t-----------\t--------------\n"); for(j=0;j<processes;j++) { printf("\t%d\t%d\t%d\t%d\t%d\n",process[j].pno,process[j].at,process[j].xt,waitm[j],tartm[j]); } printf("\nTotal Time:-%d\n",finishtm); float meantt; meantt=0; for(j=0;j<processes;j++) { meantt+=tartm[j]; } meantt/=processes; printf("\nMean Turn Around Time :-%0.2f\n",meantt); } void setIdel(int it,int processno) { int ideltime; ideltime=it; while(ideltime>0) { printf("\nCPU IDEL for %d secs for Process %d",ideltime,processno); sleep(1); ideltime--; } } void cpuallocate(int exectime,int processno) { ptime=time(0); printf("\nCPU Allocate to Process %d at time %s",processno,ctime(&ptime)); while(exectime>0) { printf("\nWait Process %d Processing....",processno); sleep(1); exectime--; } ptime=time(0); printf("\nCPU Release to Process %d at time %s\n",processno,ctime(&ptime)); }Output Of This Program :-
[om@localhost ~]$ ./a.out Please Enter No of Process 2 Please Enter Arrival time for 1 2 Please Enter Execution time for 1 2 Please Enter Arrival time for 2 3 Please Enter Execution time for 2 4 ******FCFS SCHEDULING****** Process No 1 start at Mon Dec 3 08:03:11 2012 CPU Allocate to Process 1 at time Mon Dec 3 08:03:11 2012 Wait Process 1 Processing.... Wait Process 1 Processing.... CPU Release to Process 1 at time Mon Dec 3 08:03:13 2012 Process No 2 start at Mon Dec 3 08:03:13 2012 CPU Allocate to Process 2 at time Mon Dec 3 08:03:13 2012 Wait Process 2 Processing.... Wait Process 2 Processing.... Wait Process 2 Processing.... Wait Process 2 Processing.... CPU Release to Process 2 at time Mon Dec 3 08:03:17 2012 P.No ATime ExTime WTime TATime 1 2 2 0 2 2 3 4 1 5 Total Time:-8 Mean Turn Around Time :-3.50
0 comments:
Post a Comment