Documente Academic
Documente Profesional
Documente Cultură
b[9]=9999;
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
smallest=i;
}
b[smallest]--;
if(b[smallest]==0)
{
count++;
end=time+1;
avg=avg+end-a[smallest]-x[smallest];
tt= tt+end-a[smallest];
}
}
printf("\n\nAverage waiting time = %lf\n",avg/n);
printf("Average Turnaround time = %lf",tt/n);
return 0;
}
output:-
enter the number of Processes:
4
enter arrival time
0
1
2
3
enter burst time
8
4
9
5
Average waiting time = 6.500000
Average Turnaround time = 13.000000
--------------------------------
#include<stdio.h>
int main()
{
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
return 0;
}
Code Explanation
Line no 8-15 are used for taking inputs. The array variables `at` and `bt` stores
arrival time and burst time of processes.
Array `rt` stores the remaining time of the processes which initially are equal to
the burst time of the processes. This is the time to complete execution of all the
processes.
Line no 18 starts a loop where time starts from zero, line no 20 to 27 are used
to find a process which have the least remaining time and its arrival time is less
than or equal to the current time. This condition is checked at every unit time,
that is why time is incremented by 1 (time++).
Line no 26 is used to print the arrival time and burst time for finished processes.
At this moment, variable `time` gives the time of start of the process.
Therefore Turnaround time=(time+burst time-arrival time),
Waiting time =(time arrival time)
In line no 28 remaining time for shortest process is decreased by one since time
is increased by one.
If a particular process have just finished/completed, then it satisfies the condition
in line no 29.
Since the variable time stores the current time, and the process was running at
this moment, therefore its ending time = time+1 (line no 32).
Since
burst time+waiting time = end time - Start time
therefore
waiting time=end Time-burst time-arrival time
waiting time and turnaround time of all processes are summed up and divided by
n to give their average.
Related Questions
More Answers Below
How does one implement a C code for priority scheduling with arrival time
and priorities?
2.5k Views
Here's the code I wrote which illustrates SRTF algorithm and also displays the
ghantt chart.
#include<stdio.h>
int main()
int n,ari[10],bur[10],total=0,i,j,small,temp,procs[100],k,waiting[10],finish[10];
float tavg=0.0,wavg=0.0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ari[i]);
scanf("%d",&bur[i]);
waiting[i]=0;
total+=bur[i];
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(ari[i]>ari[j])
temp=ari[i];
ari[i]=ari[j];
ari[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
for(i=0;i<total;i++)
small=3200;
for(j=0;j<n;j++)
small=bur[j]; k=j;
bur[k]--;
procs[i]=k;
k=0;
for(i=0;i<total;i++)
for(j=0;j<n;j++)
if(procs[i]==j)
finish[j]=i;
waiting[j]++;
for(i=0;i<n;i++)
wavg=wavg+(((finish[i]+1)-waiting[i])-ari[i]);
tavg=tavg+((finish[i]-ari[i])+1);
return 0;
/* to display the ghantt chat print the contents of the array procs[ ] */
By continuing, I agree that I am at least 13 years old and have read and agree to the Terms of
Service and Privacy Policy.
5 Answers
843 Views
Code
#include<stdio.h>
int main()
{
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
return 0;
}
Code Explanation
Line no 8-15 are used for taking inputs. The array variables `at` and `bt` stores
arrival time and burst time of processes.
Array `rt` stores the remaining time of the processes which initially are equal to
the burst time of the processes. This is the time to complete execution of all the
processes.
Line no 18 starts a loop where time starts from zero, line no 20 to 27 are used
to find a process which have the least remaining time and its arrival time is less
than or equal to the current time. This condition is checked at every unit time,
that is why time is incremented by 1 (time++).
Line no 26 is used to print the arrival time and burst time for finished processes.
At this moment, variable `time` gives the time of start of the process.
Therefore Turnaround time=(time+burst time-arrival time),
Waiting time =(time arrival time)
In line no 28 remaining time for shortest process is decreased by one since time
is increased by one.
If a particular process have just finished/completed, then it satisfies the condition
in line no 29.
Since the variable time stores the current time, and the process was running at
this moment, therefore its ending time = time+1 (line no 32).
Since
burst time+waiting time = end time - Start time
therefore
waiting time=end Time-burst time-arrival time
waiting time and turnaround time of all processes are summed up and divided by
n to give their average.
How does one implement a C code for priority scheduling with arrival time
and priorities?
2.5k Views
Here's the code I wrote which illustrates SRTF algorithm and also displays the
ghantt chart.
#include<stdio.h>
int main()
int n,ari[10],bur[10],total=0,i,j,small,temp,procs[100],k,waiting[10],finish[10];
float tavg=0.0,wavg=0.0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&ari[i]);
scanf("%d",&bur[i]);
waiting[i]=0;
total+=bur[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(ari[i]>ari[j])
temp=ari[i];
ari[i]=ari[j];
ari[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
for(i=0;i<total;i++)
{
small=3200;
for(j=0;j<n;j++)
small=bur[j]; k=j;
bur[k]--;
procs[i]=k;
k=0;
for(i=0;i<total;i++)
for(j=0;j<n;j++)
if(procs[i]==j)
finish[j]=i;
waiting[j]++;
for(i=0;i<n;i++)
tavg=tavg+((finish[i]-ari[i])+1);
return 0;
/* to display the ghantt chat print the contents of the array procs[ ] */
Peace !
5.7k Views
#include<stdio.h>
#include<malloc.h>
int n,*a;
struct pro
{
int name,at,bt,tt,wt,left,status;
}*p;
void create(int num)
{
a=(int*)malloc(num*sizeof(int));
}
int smallers(int num)
{
int c=0,i;
for(i=0;i<n;i++)
{
if((p+i)->bt<num && (p+i)->status==0)
{
*(a+c)=(p+i)->bt;
c++;
}
}
return c;
}
int search(int num)
{
int i;
for(i=0;i<n;i++)
{
if((p+i)->bt==num && (p+i)->status==0)
return i;
}
}
int main()
{
int i,c_time=0,b,s_nums,j,pro_num,p_time;
float att=0,awt=0;
printf("Enter no. of processes- ");
scanf("%d",&n);
create(n);
p=(struct pro *)malloc(n*sizeof(struct pro));
for(i=0;i<n;i++)
(p+i)->status=0;
printf("---Enter Process Name, Arrival Time & CPU Burst Time---\n");
for(i=0;i<n;i++)
scanf("%d %d %d",&(p+i)->name,&(p+i)->at,&(p+i)->bt);
for(i=0;i<n;i++)
{
if((p+i)->status==0)
{
b=(p+i)->bt;
s_nums=smallers(b);
if(s_nums>0)
{
(p+i)->left=(p+i)->bt;
for(j=0;j<s_nums;j++)
{
p_time=c_time;
pro_num=search(*(a+j));
if(c_time<(p+pro_num)->at)
{
if((p+i)->left>(p+pro_num)->at)
{
c_time=(p+pro_num)->at;
(p+i)->left=(p+i)->bt-(c_time-p_time);
c_time=c_time+(p+pro_num)->bt;
(p+pro_num)->status=1;
}
else
{
c_time=c_time+(p+i)->bt;
(p+i)->tt=c_time-(p+i)->at;
(p+i)->status=1;
if(c_time<(p+pro_num)->at)
break;
}
}
else
{
c_time=c_time+(p+pro_num)->bt;
}
(p+pro_num)->tt=c_time-(p+pro_num)->at;
(p+pro_num)->status=1;
}
if((p+i)->status==0)
{
c_time=c_time+(p+i)->left;
(p+i)->tt=c_time-(p+i)->at;
(p+i)->status=1;
}
}
else
{
c_time=c_time+(p+i)->bt;
(p+i)->tt=c_time-(p+i)->at;
(p+i)->status=1;
}
}
}
printf("\nProcess\t\tArrival Time\t\tCPU Burst Time\t\tTurnAround Time\t\tWaiting
Time\n");
for(i=0;i<n;i++)
{
(p+i)->wt=(p+i)->tt-(p+i)->bt;
awt=awt+(p+i)->wt;
att=att+(p+i)->tt;
}
for(i=0;i<n;i++)
{
printf("%d\t\t%d\t\t\t%d\t\t\t%d\t\t\t%d\n",(p+i)->name,(p+i)->at,(p+i)->bt,
(p+i)->tt,(p+i)->wt);
}
printf("\nAverage Turnaround Time : %f\n",att/n);
printf("Average Waiting Time : %f\n\n",awt/n);
return 0;
}
//SAMPLE OUTPUT
/*Enter no. of processes- 4
---Enter Process Name, Arrival Time & CPU Burst Time---
1 0 8
2 1 4
3 2 9
4 3 5
Process Arrival Time CPU Burst Time TurnAround Time Waiting Time
1 0 8 17 9
2 1 4 4 0
3 2 9 24 15
4 3 5 7 2
707 Views
#include<stdio.h>
int main()
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
rt[9]=9999;
for(time=0;remain!=n;time++)
smallest=9;
for(i=0;i<n;i++)
smallest=i;
rt[smallest]--;
if(rt[smallest]==0)
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-
at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
return 0;
Written 16 Mar
887 Views
#include<stdio.h>
int main()
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
rt[i]=bt[i];
rt[9]=9999;
for(time=0;remain!=n;time++)
smallest=9;
for(i=0;i<n;i++)
smallest=i;
rt[smallest]--;
if(rt[smallest]==0)
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
return 0;
Related Questions
What C program can help me simulate the shortest job first scheduling
without arrival times to calculate the average waiting time?
Is the following Gantt diagram correct for a preemptive Shortest Job First
algorithm (SJF)?
What are the best Gantt chart plugins available for project schedule
display and management in a web project?
How much time does a person take while writing code for the
implementation of algorithms for the first time?
What is the solution for the starvation problem in the shortest job first-
scheduling algorithm?
What is the shortest C program you can come up with for implementing
Sieve of Eratosthenes ?