I\'m trying to implement simulation of CPU Scheduling Round Robin. I have the co
ID: 3693322 • Letter: I
Question
I'm trying to implement simulation of CPU Scheduling Round Robin. I have the code below, but it needs to read the input from a txt file and output the results to a different txt file(preferrably in the same directory as the code. Here is the details of how it works, followed by the code.
Input File:
The first line has a space-separated list of scheduling algorithm names to run (any combination of the four names given above). The second line has a single integer representing the number of processes in the file. The rest of the file has one line per process with the following information:
Process number Arrival Time CPU burst time Priority
If multiple processes have the same arrival time, your scheduling algorithm should assume that there are negligibly small differences in arrival times with the processes arriving in the order they appear in the file. For priority scheduling, assume that smaller numbers indicate higher priorities. Non-priority algorithms will simply ignore the priority field.
Output File:
The output file will have the scheduling results for each of the algorithms listed in the input file. There should be one line for each CPU assignment (one line for each vertical line in the Gantt chart). Each line should have two numbers: one indicating the time point and one indicating the process number that got the CPU at that point. The last line for each scheduling algorithm should have the average waiting time.
Example: As shown in the example below, when the algorithm is RR, there should be an integer parameter next to the algorithm’s name specifying the length of the time quantum:
RR 4
3
1 0 24 1
2 0 3 1
3 0 3 1
Output 1
RR
1
4 2
7 3
10 1
14 1
18 1
22 1
26 1
AVG Waiting Time: 5.67
Here is the code I have so far:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process: ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum: ");
scanf("%d",&time_quantum);
printf(" Process |Turnaround Time|Waiting Time ");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d] | %d | %d ",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf(" Average Waiting Time= %f ",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
Explanation / Answer
int main()
{
int count,j,n,time,remain,flag=0,time_quantum_here;
int waiting_timehere=0,turnaround_timehere=0,arrivaltime[10],bursttime[10],rtarray[10];
printf(“Please enter Total Process: ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Now we will be entering Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&arrivaltime[count]);
scanf("%d",&bursttime[count]);
rtarray[count]=bursttime[count];
}
printf("Now we will enter Time Quantum: ");
scanf("%d",&time_quantum_here);
printf(" Process |Turnaround Time|Waiting Time ");
for(time=0,count=0;remain!=0;)
{
if(rtarray[count]<=time_quantum_here && rtarray[count]>0)
{
time+=rtarray[count];
rtarray[count]=0;
flag=1;
}
else if(rtrray[count]>0)
{
rtarray[count]-=time_quantum_here;
time+=time_quantum_here;
}
if(rtarray[count]==0 && flag==1)
{
remain--;
printf("P[%d] | %d | %d ",count+1,time-arrivaltime[count],time-arrivaltime[count]-bursttime[count]);
waiting_time+=time-arrivaltime[count]-bursttime[count];
turnaround_timehere+=time-arrivaltime[count];
flag=0;
}
if(count==n-1)
count=0;
else if(arrivaltime[count+1]<=time)
count++;
else
count=0;
}
printf(" The average Waiting Time= %f ",waiting_time*1.0/n);
printf("The average Turnaround Time = %f",turnaround_timehere*1.0/n);
return 0;
}