Operating System CH2 Lab
Operating System CH2 Lab
Operating System Lab Manual about Short Term Scheduling (CPU Scheduling)
1. First-Come First-Served (FCFS) Scheduling can consider arrival time is 0
//First-Come, First-Served (FCFS) Scheduling
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main() {
int i,j,bt[10],n,wt[10],tt[10],tbt=0,Twt=0,Ttt=0;
float awt,att ,th;
cout<<"enter no. of processes:"<<endl;
cin>>n;
cout<<"enter the burst time of processes:"<<endl;
for(i=0;i<n;i++)
cin>>bt[i];
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
Twt=Twt+wt[i];
Ttt=Ttt+tt[i];
tbt=tbt+bt[i];
}
awt=Twt/n;
att=Ttt/n;
th=n/tbt;
cout<<"\n process\t bt\t wt\t tt\n"<<endl;
for(i=0;i<n;i++)
cout<<"p"<<i<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\t"<<tt[i]<<endl;
return 0;
Or
#include<stdio.h>
#include<iostream.h>
struct process{
char name[5]; int at,
bt, wt, tt;
};
int main( )
{
int i, j, n, t;
float awt=0, att=0;
struct process p[10], temp;
cout<<"\n Enter the number of process:"<<endl;
cin>>n;
for(i=0; i<n; i++)
{
cout<<"\n Enter the name, arrival time and burst time of process :"<< i+1<<endl;
A Program to Simulate the SJF CPU Scheduling Algorithm Program arrival time is
non –zero:
#include<stdio.h>
#include<iostream.h>
#include<string.h>
int main()
{
int bt[20],at[10],n,i,j,temp,st[10],ct[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:"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter process name, arrival time & execution time:"<<endl;
cin>>pn[i]>>at[i]>>bt[i];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
#include<iostream.h>
int main()
{
int i,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],Twt=0,Ttt=0,s;
float awt,att;
cout<<"enter the number of processes:"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"The process "<<i+1<<endl;
cout<<"Enter the burst time of processes:"<<endl;
cin>>bt[i];
cout<<"Enter the priority of processes:"<<i+1<<endl;
cin>>prior[i];
pno[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(prior[i]>prior[j])
{
s=prior[i];
10 | P a g e Operating System
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]+bt[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
cout<<"Pname\tarrivaltime\tbursttime\tpriority\twaitingtime\ttatime"<<endl;
for(i=0;i<n;i++)
cout<<pn[i]<<"\t\t"<<at[i]<<"\t"<<bt[i]<<"\t"<<p[i]<<"\t"<<wt[i]<<"\t"<<ta[i]<<endl;
cout<<"\nAverage waiting time is:"<<awt<<endl;
cout<<"\nAverage turnaroundtime is:"<<ata<<endl;
return 0;
11 | P a g e Operating System
}
12 | P a g e Operating System
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
cout<<"Process_no Burst time Wait time Turnaround time"<<endl;
for(i=0;i<n;i++)
cout<<"\n\t”<<i+1<<”\t<<bt[i]<<”\t”<<wt[i]<<”\”<<tat[i]<<endl;
cout<<"\nAvg wait time is=”<< awt<<endl;
cout<<"\nAvg turnaround time is=”<<atat;
return 0;
}
6. C++ program to implement SRTF CPU scheduling algorithm with arrival time
13 | P a g e Operating System
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int ar[10],id[10],bt[10];
int temp, mid[20],last[20],f[20];
int n;
cout<<"=================================================="<<endl;
cout<<"How many process to be entered :"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of "<<i+1<<" process :"<<endl;
cin>>bt[i];
cout<<"Enter arrival time of "<<i+1<<" process :"<<endl;
cin>>ar[i];
id[i]=i+1;
}
cout<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<bt[i]<<setw(15)<<ar[i]<<endl;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
14 | P a g e Operating System
f[z]=f[z+1];
f[z+1]=temp;
temp=mid[z];
mid[z]=mid[z+1];
mid[z+1]=temp;
temp=last[z];
last[z]=last[z+1];
last[z+1]=temp;
}
}
int exe2[10],flag=1;
int at=0,ind,wt,tnt,min,max=bt[0];
float avg=0,avtnt=0;
// sort(ar,id,bt);
for(int i=0;i<n;i++)
{
exe2[i]=bt[i];
if(max<bt[i])
max=bt[i];
}
at=ar[0];
min=max+1;
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at>=ar[i]&&min>bt[i]&&id[i]>0)
{
ind=i;
min=bt[i];
15 | P a g e Operating System
}
}
at++;
bt[ind]--;
min=max+1;
if(bt[ind]==0)
{
wt=at-exe2[ind]-ar[ind];
tnt=at-ar[ind];
cout<<setw(5)<<id[ind]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[ind]=-1;
avg+=wt;
avtnt+=tnt;
}
flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
return 0;
}
16 | P a g e Operating System