In: Computer Science
Please write in C using linux or unix.please include pictures of the terminal output.
Write a program that will simulate non - preemptive process scheduling algorithm:
First Come – First Serve
Your program should input the information necessary for the calculation of average turnaround time including:
The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time.
Step 1: generate the input data (totally 10 jobs) and save it in the array of structure composing the arrival time, service time, termination time, turnaround time. The service time follows the uniform distribution in the range of [5, 25], and the arrival time is generated by uniform distribution in the range of [0,10] accumulated based on the previous arrival time.
Step 2: program FCFS algorithm.
Note: be careful about the situation that one job is finished while the next job is not arrived yet, so you have the idle time between them.
Step 3: output
print out one line for each job with arrival time, start time, service, termination time, turnaround time, finally average turnaround time in the last line.
Answer :
Code :
#include<stdio.h>
int main()
{
int n=10,at[20],bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
for(i = 0; i<n;i++){
at[i] = i;
}
for(i=0;i<n;i++)
{
bt[i] = 5 + 2*i;
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tArrival Time\t\tBurst Time\tWaiting
Time\tTurnaround Time");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t%d\t\t%d",i+1,at[i],bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}