Write a program that will simulate non - preemptive process scheduling algorithm
ID: 3821004 • Letter: W
Question
Write a program that will simulate non - preemptive process scheduling algorithms in C++:
FCFS(First Come First Serve).
SJN(Shortest Job Next)
Priority
Your program should input the information necessary for the calculation of average turnaround time including:
Time required for a job execution;
Arrival time;
Priority
Note: The information can vary for different algorithms.
The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time.
Explanation / Answer
FCFS(First Come First Serve)
#include<iostream>
using namespace std;
int main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
cout<<"Enter the number of processes:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the Process Name, Arrival Time & Burst Time:";
cin>>pn[i]>>arr[i]>>bur[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
cout<<" PName Arrtime Burtime WaitTime Starttime Turnaroundtime Finishtime";
for(i=0;i<n;i++)
{
cout<<pn[i]<<arr[i]<<bur[i]<<wt[i]<<star[i]<<tat[i]<<finish[i];
totwt+=wt[i];
tottat+=tat[i];
}
cout<<" Average Waiting time:"<<(float)totwt/n;
cout<<" Average Turn Around Time:"<<(float)tottat/n;
return 0;
}
SJN(Shortest Job Next)
#include<iostream>
using namespace std;
int main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
cout<<"Enter the number of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter process name, arrival time & execution time:";
cin>>pn[i]>>at[i]>>et[i];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(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];
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;
cout<<" Pname arrivaltime executiontime waitingtime tatime";
for(i=0;i<n;i++)
cout<<pn[i]<<at[i]<<et[i]<<wt[i]<<ta[i];
cout<<" Average waiting time is:"<<awt;
cout<<" Average turnaroundtime is:"<<ata;
}
Priority
#include<iostream>
using namespace std;
int 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];
cout<<"Enter the number of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter process name,arrivaltime,execution time & priority:";
cin>>pn[i]>>at[i]>>et[i]>>p[i];
}
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;
strcpy(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;
cout<<" Pname arrivaltime executiontime priority waitingtime tatime";
for(i=0;i<n;i++)
cout<<pn[i]<<at[i]<<et[i]<<p[i]<<wt[i]<<ta[i];
cout<<" Average waiting time is:"<<awt;
cout<<" Average turnaroundtime is"<<ata;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.