DUE ASAP !
Program Assignment: 1. Implement the following 3 scheduling Algorithms: a. First Come, First Served (batch, non preemptive) (FCFS) b. Shortest Job First (batch, non preemptive) (SJF) c. Round Robin (preemptive) (RR) 2. Implement Using C or C++ on a Linux machine 3. To run the 3 scheduling algorithms, create a Priority Scheduling Program with menu options that will call the 3 scheduling programs, (fcfs, sjf, and Round Robin). These are simulation algorithms, that will be run from your terminal window. 4. Below is an example of an input file. This is the input that generated the example output. ProcessID Arrival cpuBurst Priority 100 0 10 1 101 6 10 1 102 8 4 1 103 12 20 1 104 19 15 1 105 30 5 1 106 35 10 1 4.Below is an example of the way the output should look. In this example each algorithm is run with the same data file (“jobs.txt”), then the output and statistics are presented. This is a good example to use to test your algorithms and program. For the Round Robin, the time Quanta was 15, I think. ProcessID Arrival cpuBurst Priority 100 0 10 1 101 6 10 1 102 8 4 1 103 12 20 1 104 19 15 1 105 30 5 1 106 35 10 1 number of jobs in newQ = 7 Terminated Jobs (First Come, First Served) ProcessId arrival completion 100 0 10 101 6 20 102 8 24 103 12 44 104 19 59 105 30 64 106 35 74 Run Stats Throughput = 0.09 Average turnaround time = 26.43 Average response time = 15.86 ProcessID Arrival cpuBurst Priority 100 0 10 1 101 6 10 1 102 8 4 1 103 12 20 1 104 19 15 1 105 30 5 1 106 35 10 1 Number of jobs in newQ = 7 Terminated Jobs. (Shortest Job First) ProcessId arrival completion 100 0 10 102 8 14 101 6 24 104 19 39 105 30 44 106 35 54 103 12 74 Run Stats Throughput = 0.09 Average turnaround time = 21.29 Average response time = 10.71 ProcessID Arrival cpuBurst Priority 100 0 10 1 101 6 10 1 102 8 4 1 103 12 20 1 104 19 15 1 105 30 5 1 106 35 10 1 Number of jobs in newQ = 7 Terminated Jobs. (Round Robin) ProcessId arrival completion 100 0 10 101 6 20 102 8 24 104 19 54 103 12 59 105 30 64 106 35 74 Run Stats Throughput = 0.09 Average turnaround time = 27.86 Average response time = 11.14 5. Below is an example of a routine used to output the run statistics: void showRunStats(int numJobs, int time) { int j; float tPut, turn, resp; /* Initialize variables. */ turn = 0.0f; resp = 0.0f; /* Calculate turnaround time and waiting time. In this calculation the waiting time is really should be called response time. */ for(j=0;j
FCFS:
#include<stdio.h>
int main(){
int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0;
float totalTAT=0,totalWT=0;
printf("\n");
printf("First Come First Serve Scheduling\n");
printf("\n");
printf("Enter number of processes ");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("Arrival time of process[%d] ",i+1);
scanf("%d",&at[i]);
printf("Burst time of process[%d] ",i+1);
scanf("%d",&bt[i]);
printf("\n\n");
}
//calculate completion time of processes
for(int j=0;j<n;j++)
{
sum+=bt[j];
ct[j]+=sum;
}
//calculate turnaround time and waiting times
for(int k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}
for(int k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalWT+=wt[k];
}
printf("Solution: \n\n");
printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n");
for(int i=0;i<n;i++)
{
printf("P%d\t %d\t %d\t %d\t %d\t %d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);
}
printf("\n\nAverage Turnaround Time = %f\n",totalTAT/n); printf("Average WT = %f\n\n",totalWT/n);
return 0;
}
Priority:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("\t\t\t\t\t_________________________________________________________
__________\n\n");
printf("\t\t\t\t\t\t\tPriority Scheduling\n");
printf("\t\t\t\t\t______________________________________________________
_____________\n\n\n");
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name:");
scanf("%s",pn[i]);
printf("Enter Arrival Time:");
scanf("%d",&at[i]);
printf("Enter Execution Time:");
scanf("%d",&et[i]);
printf("Enter Priority:");
scanf("%d",&p[i]);
printf("\n");
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
11strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
Round Robin
#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("\t\tRound Robin Scheduling\n\n");
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
printf("\n");
for(count=0;count<n;count++)
{
printf("Enter Arrival Time Process P[%d] :",count+1);
scanf("%d",&at[count]);
printf("Enter Burst Time for Process P[%d]:",count+1);
scanf("%d",&bt[count]); rt[count]=bt[count];
printf("\n"); }printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t\tTurnaround Time\t\tWaiting Time\n\n");
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]\t\t\t%d\t\t\t%d\n",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("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
SJF(Non- Preemptive):
#include<stdio.h>
int main()
{
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0; float wavg=0,tavg=0,tsum=0,wsum=0;
printf("\t\n");
printf("\tShortest Job First Non-Preemptive Scheduling\n");
printf("\\n");
printf("\nEnter the No. of processes :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\tEnter the burst time of %d process :",i+1);
scanf(" %d",&bt[i]);
printf("\tEnter the arrival time of %d process :",i+1);
scanf(" %d",&at[i]);
printf("\n");
}
/*Sorting According to Arrival Time*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}}}
/*Arranging the table according to Burst time, Execution time and Arrival Time
Arrival time <= Execution time
*/
for(j=0;j<n;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<n;i++)
{
if (btime>=at[i] && bt[i]<min)
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}} k++;
}
wt[0]=0;
for(i=1;i<n;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];
}
wavg=(wsum/n);
for(i=0;i<n;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];
}
tavg=(tsum/n);
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
for(i=0;i<n;i++)
{
printf("\n p%d\t %d\t %d\t\t %d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
}
printf("\n\nAVERAGE WAITING TIME : %f",wavg);
printf("\nAVERAGE TURN AROUND TIME : %f",tavg);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.