Practical 4 (1)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Program-4

Aim : Write a program for SJF CPU Scheduling.


Program :
using namespace std;
#include<iostream>
int main()
{
int P[10],BT[10],WT[10],i;
float avgWT,n,totalW=0;
cout<<"Enter how many processes you want to enter : ";
cin>>n;
cout<<"\nEnter the processes :\n";
for(i=0;i<n;i++)
{
cin>>P[i];
}
cout<<"\nEnter the Burst time of processes :\n";
for(i=0;i<n;i++)
{
cin>>BT[i];
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(BT[j]<BT[i])
{
int temp1=BT[j];
BT[j]=BT[i];
BT[i]=temp1;

int temp2=P[j];
P[j]=P[i];
P[i]=temp2;
}
}
}
WT[0]=0;
for(i=1;i<n;i++)
{
WT[i]=WT[i-1]+BT[i-1];
totalW=totalW+WT[i];
}
avgWT=float(totalW/n);
cout<<"\nProcesses \t Burst_Time \t Waiting_Time \n";
for(i=0;i<n;i++)
{
cout<<P[i]<<"\t\t"<<BT[i]<<"\t\t"<<WT[i]<<endl;
}
cout<<"\nTotal Wating time = "<<totalW;
cout<<"\nAverage waiting time = "<<avgWT<<endl;
return 0;
}
Output :
Enter how many processes you want to enter : 4
Enter the processes :
1
2
3
4

Enter the Burst time of processes :


70
50
30
10

Processes Burst_Time Waiting_Time


4 10 0
3 30 10
2 50 40
1 70 90

Total Wating time = 140


Average waiting time = 35

You might also like