Write a simulation program for scheduling algorithm using FCFS. The arrival time and first CPU burst of different jobs should be input to the system. The output should give the Gantt chart & turnaround time for each process and average turnaround time.
Write a simulation program for scheduling algorithm using FCFS. The arrival time and first CPU burst of different jobs should be input to the system. The output should give the Gantt chart & turnaround time for each process and average turnaround time.
Program:
#include <stdio.h> #include <stdlib.h> struct sch{ int at,st,bt,tat,ft; }pr[50]; int rq[50],x,n,timer; void addP(){ int i; for(i=0;i<n;i++){ if(pr[i].at==timer){ rq[x++]=i; } } } int selectP(){ int pp,i; if(x<0){ return(-1); }else{ pp=rq[0]; for(i=0;i<n;i++){ rq[i]=rq[i+1]; } x--; return(pp); } } int main(void){ setbuf(stdout,NULL); int i,t,p; float atat; timer = 0; x=-1; printf("Enter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++){ printf("\nEnter the Arrival time:"); scanf("%d",&pr[i].at); printf("\nEnter the Burst time:"); scanf("%d",&pr[i].bt); } t=0; addP(); while(t<n){ p=selectP(); if(p==-1){ printf("idle|"); timer++; addP(); }else{ pr[p].st=timer; for(i=0;i<pr[p].bt;i++){ printf("P%d|",p); timer++; addP(); } pr[p].ft=pr[p].st+pr[p].bt; t++; } } atat=0; printf("\nTurn Around Time for each Process:"); for(i=0;i<n;i++){ pr[i].tat = pr[i].ft-pr[i].at; printf("\nP%d=%d",i,pr[i].tat); atat+=pr[i].tat; } atat = atat/n; printf("\nAverage Turn Around Time:%f",atat); return EXIT_SUCCESS; }
Comments
Post a Comment