Simulate Scheduling
Write three separate programs in C (any flavor) that will simulate the following algorithms:
1. Shortest Job Next
2. Shortest Remaining Time Next
3. Round Robin
For each algorithm, the program should compute and output the following:
- Waiting Time,
- Turnaround time
- Average Waiting Time,
- Turnaround Time
- Order in which the jobs will finish
To make the program easier, you are given a finite and specific job details below to simulate and compute.
Solution
Round_robin.c
#include<stdio.h>
// Function to find the waiting time for all
// processes
voidfindWaitingTime(int processes[], int n,
intbt[], intwt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining
// burst times.
intrem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
// Keep traversing processes in round robin manner
// until all of them are not done.
while (1)
{
int done = 1;
// Traverse all processes one by one repeatedly
for (int i = 0 ; i < n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = 0; // There is a pending process
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process
// by quantum
rem_bt[i] -= quantum;
}
// If burst time is smaller than or equal to
// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];
// Waiting time is current time minus time
// used by this process
wt[i] = t – bt[i];
// As the process gets fully executed
// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == 1)
break;
}
}
// Function to calculate turn around time
voidfindTurnAroundTime(int processes[], int n,
intbt[], intwt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
// Function to calculate average time
voidfindavgTime(int processes[], int n, intbt[],
int quantum)
{
intwt[n], tat[n], total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
printf(“Processes Burst time Waiting time Turn around time\n”);
// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(” %c\t\t%d\t %d\t\t%d\n”,(char)(processes[i]+64),bt[i],wt[i],tat[i]);
}
printf(“Average waiting time = %f”,(float)total_wt/(float)n);
printf(“\nAverageturn around time = %f”,(float)total_tat / (float)n);
}
// Driver code
int main()
{
// process id’s
int processes[10];
int n = 10;
// Burst time of all processes
intburst_time[10] = {16,2,11,6,1,9,4,14,1,8};
inti,j,pos,temp;
for(i=0;i<n;i++)
{
processes[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(burst_time[j]<burst_time[pos])
pos=j;
}
temp=burst_time[i];
burst_time[i]=burst_time[pos];
burst_time[pos]=temp;
temp=processes[i];
processes[i]=processes[pos];
processes[pos]=temp;
}
printf(“\nThe order of execution is:”);
for(i=0;i<n;i++)
{
printf(“\n%c”,(char)(processes[i]+64));
}
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
SJN.c
#include<stdio.h>
//I have used burst time which stands for no. of Cpu cycles in the assignment
int main(void)
{
int p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
floatavg_wt,avg_tat;
n=10;
intbt[10]={16,2,11,6,1,9,4,14,1,8};
/*printf(“Enter number of process:”);
scanf(“%d”,&n);
printf(“\nEnter Burst Time:\n”);
for(i=0;i<n;i++)
{
printf(“%c:”,char(i+65));
scanf(“%d”,&bt[i]);
p[i]=i+1; //contains process number
}*/
// the above commented code can be used for any kind of input
for(i=0;i<n;i++)
{
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf(“\nThe order of execution is:”);
for(i=0;i<n;i++)
{
printf(“\n%c”,(char)(p[i]+64));
}
printf(“\n\nProcess\t Burst Time \tWaiting Time\tTurnaround Time”);
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf(“\n%c\t\t %d\t\t %d\t\t\t%d”,(char)(p[i]+64),bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround time
printf(“\n\nAverage Waiting Time=%f”,avg_wt);
printf(“\nAverage Turnaround Time=%f\n”,avg_tat);
}
SRTN.c
#include<stdio.h>
structProces
{
intpid; // Process ID
intbt; // Burst Time
int art; // Arrival Time
};
typedefstructProces Process;
// Function to find the waiting time for all
// processes
voidfindWaitingTime(Process proc[], int n,
intwt[])
{
intrt[n];
// Copy the burst time into rt[]
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = 9999999;
int shortest = 0, finish_time;
int check = 0;
// Process until all processes gets
// completed
while (complete != n) {
// Find process with minimum
// remaining time among the
// processes that arrives till the
// current time`
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] <minm) &&rt[j] > 0) {
minm = rt[j];
shortest = j;
check = 1;
}
}
if (check == 0) {
t++;
continue;
}
// Reduce remaining time by one
rt[shortest]–;
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = 9999999;
// If a process gets completely
// executed
if (rt[shortest] == 0) {
// Increment complete
complete++;
// Find finish time of current
// process
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time –
proc[shortest].bt –
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate turn around time
voidfindTurnAroundTime(Process proc[], int n,
intwt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
// Function to calculate average time
voidfindavgTime(Process proc[], int n)
{
intwt[n], tat[n], total_wt = 0,
total_tat = 0;
// Function to find waiting time of all
// processes
findWaitingTime(proc, n, wt);
// Function to find turn around time for
// all processes
findTurnAroundTime(proc, n, wt, tat);
inti,p[10],pos,j,temp;
for(i=0;i<n;i++)
{
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(wt[j]<wt[pos])
pos=j;
}
temp=wt[i];
wt[i]=wt[pos];
wt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
printf(“\nThe order of execution is:”);
for(i=0;i<n;i++)
{
printf(“\n%c”,(char)(p[i]+64));
}
printf(“\n”);
// Display processes along with all
// details
printf(“Processes Burst time Waiting time Turn around time\n”);
// Calculate total waiting time and
// total turnaround time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(” %c\t\t%d\t %d\t\t%d\n”,(char)(proc[i].pid+64),proc[i].bt,wt[i],tat[i]);
}
printf(“Average waiting time = %f”,(float)total_wt/(float)n);
printf(“\nAverageturn around time = %f”,(float)total_tat / (float)n);
}
// Driver code
int main()
{
Process proc[] = { { 1, 16, 0 }, { 2, 2, 3 },
{ 3, 11, 5 }, { 4, 6, 9 } ,
{ 5, 1, 10 }, { 6, 9, 12 } ,
{ 7, 4, 14 }, { 8, 14, 16 } ,
{ 9, 1, 17 }, { 10, 8, 19 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}