Write a C program to simulate the operation of two of CPU sc

Write a C++ program to simulate the operation of two of CPU scheduling methods. The program does the following:

1. Get the number of processes from the user.
2. Get the burst time of each process from the user.
3. Assume that all processes arrive at \"0\" to the ready queue.
4. The program let the user select one of the two methods to implement the CPU scheduling.
5. The program calculates the average waiting time and the average turnaround time.
6. The program shows scheduling time of the CPU by using Gant Chart or table.

Solution

/* CPP Programe for compare FCFS & SJF CPU scheduling methods */

#include<iostream>

using namespace std;

int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cout<<\"Enter total number of processes(maximum 20):\";
cin>>n;

cout<<\"\ Enter Process Burst Time\ \";
for(i=0;i<n;i++)
{
cout<<\"P[\"<<i+1<<\"]:\";
cin>>bt[i];
}
/////////////////////////////////////////////////////////////////First Come First Serve Code ///////////////////////////////////////////////////
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];
}

cout<<\"\ Process\\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];
cout<<\"\ P[\"<<i+1<<\"]\"<<\"\\t\\t\"<<bt[i]<<\"\\t\\t\"<<wt[i]<<\"\\t\\t\"<<tat[i];
}

avwt/=i;
avtat/=i;
cout<<\"\ \ FCFS_Average Waiting Time:\"<<avwt;
cout<<\"\ FCFS_Average Turnaround Time:\"<<avtat;
  
  
//////////////////////////////////////////////First Come First Serve Code Ends/////////////////////////////////////////////////////////
  
//////////////////////////////////////////////Shortest Job First code///////////////////////////////////////////////////////////////////
  

//sorting burst time in ascending order using selection sort
int temp,p[20],total;
float avg_wt,avg_tat;
for(i=0;i<n;i++)
{
   int 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(\"\ Process\\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];
cout<<\"\ p\"<<p[i]<<\"\\t\\t \"<<bt[i]<<\"\\t\\t \"<<wt[i]<<\"\\t\\t\\t\"<<tat[i];
}

avg_tat=(float)total/n; //average turnaround time
cout<<\"\ \ SJF_Average Waiting Time=\"<<avg_wt<<\"\ \";
cout<<\"\ SJF_Average Turnaround Time=\"<<avg_tat<<\"\ \";

  
  
/////////////////////////////////////////////////////SJ CODE ENDS//////////////////////////////////////////////////////////////

return 0;
}

Output :

Enter total number of processes(maximum 20):2

Enter Process Burst Time
P[1]:2
P[2]:3

Process Burst Time Waiting Time Turnaround Time
P[1] 2 0 2
P[2] 3 2 5

FCFS_Average Waiting Time:1
FCFS_Average Turnaround Time:3
Process Burst Time Waiting Time Turnaround Time
p1999008730 2 0 2
p1189597896 3 2 5

SJF_Average Waiting Time=2.12417e+006

SJF_Average Turnaround Time=3.5

Write a C++ program to simulate the operation of two of CPU scheduling methods. The program does the following: 1. Get the number of processes from the user. 2.
Write a C++ program to simulate the operation of two of CPU scheduling methods. The program does the following: 1. Get the number of processes from the user. 2.
Write a C++ program to simulate the operation of two of CPU scheduling methods. The program does the following: 1. Get the number of processes from the user. 2.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site