0% found this document useful (0 votes)
68 views

Operating System CH2 Lab

The document summarizes different CPU scheduling algorithms implemented in C++ code, including: 1) First-Come First-Served (FCFS) scheduling with and without arrival times. 2) Shortest Job First scheduling without arrival times. 3) Priority scheduling with and without arrival times. 4) Round Robin scheduling. For each algorithm, the code provides examples to calculate waiting times, turnaround times, and average waiting and turnaround times.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Operating System CH2 Lab

The document summarizes different CPU scheduling algorithms implemented in C++ code, including: 1) First-Come First-Served (FCFS) scheduling with and without arrival times. 2) Shortest Job First scheduling without arrival times. 3) Priority scheduling with and without arrival times. 4) Round Robin scheduling. For each algorithm, the code provides examples to calculate waiting times, turnaround times, and average waiting and turnaround times.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Computer Science Department

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;

1|Page Operating System


cout<<"Average awt="<<awt<<endl;
cout<<"Average att ="<<att<<endl;
cout<<"throghout="<<th;
return 0;
}
2. First-Come First-Served (FCFS) Scheduling can consider arrival time is none
zero
#include<iostream.h>
int main()
{
int i,n, bt[20], wt[20],tt[20], ct[20],at[20],Twt=0,Ttt=0,Tbt=0;
float avgwait, avgturn,th;
cout<<"enter number of process:"<<endl;
cin>>n;
for(i=0; i<n;i++){
cout<<"enter Arrival time ||burst time :p"<<i<<endl;
cin>>at[i];
cin>>bt[i];
}
for(i=0; i<n;i++){
ct[0]=0;
ct[i+1]=ct[i]+bt[i];
tt[i]=ct[i+1]-at[i];
wt[i]=tt[i]-bt[i];
Ttt=Ttt+tt[i];
Twt=Twt+wt[i];
Tbt=Tbt+bt[i];
}
avgwait=Twt/n;
avgturn=Ttt/n;
th=n/Tbt;

2|Page Operating System


cout<<"process\t arrivaltime\t burse time\t turnaround time \t waiting time\t"<<endl;
for(int i=0;i<n;i++){
cout<<"p"<<i<<"\t\t"<<" "<<at[i]<<"\t\t"<<" "<<bt[i]<<"\t\t"<<" "<<tt[i]<<"\t\t"<<"
"<<wt[i]<<endl;
}
cout<<"\n AVERAGE WAITING TIME ="<<avgwait<<endl;
cout<<"\n AVERAGE TURN AROUND TIME ="<<avgturn<<endl;
cout<<"\n Throughout="<<th;

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;

3|Page Operating System


cin>>p[i].name >>p[i].at >>p[i].bt;
}
for(i=0; i<n-1; i++)
for(j=0; j<n-1; j++)
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
p[0].wt=0;
t=p[0].tt=p[0].bt;
att+=p[0].bt;
for(i=1;i<n;i++)
{
p[i].wt=t-p[i].at;
t+=p[i].bt;
p[i].tt=p[i].wt+p[i].bt;
awt+=p[i].wt;
att+=p[i].tt;
}
cout<<"\n Process Name ||Arrival time ||burst time ||Waiting Time ||Turnaround
Time"<<endl;
for(i=0;i<n;i++)
cout<<" \t"<< p[i].name<<"\t "<< p[i].at <<"\t"<<p[i].bt<<"\t"<< p[i].wt<<"\
t"<<p[i].tt<<endl;
awt/=n;
att/=n;
cout<<"\n Average waiting Time :"<< awt;
cout<<"\n Average Turnaround Time:"<< att;
return 0;

4|Page Operating System


}
3. Shorts Job first scheduling implementation when consider arrival time is 0
#include<iostream.h>
int main()
{
int i,j,bt[10],t,n,wt[10],tt[10],Twt=0,Ttt=0;
float awt,att;
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++)
{
for(j=i;j<n;j++)
if(bt[i]>bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}}
for(i=0;i<n;i++)
cout<<bt[i]<<endl;
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];

5|Page Operating System


}
awt=Twt/n;
att=Ttt/n;
cout<<"\nprocess\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;
cout<<"Average awt="<<awt<<endl;
cout<<"Average att ="<<att;
return 0;
}

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++)

6|Page Operating System


{
if(at[i]<at[j]){
if(bt[i]<bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[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<<"\nPname\tarrivaltime\tburst time\twaitingtime\ttatime"<<endl;

7|Page Operating System


for(i=0;i<n;i++)
cout<<pn[i]<<"\t"<<at[i]<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\t"<<ta[i]<<endl;
cout<<"\nAverage waiting time is:"<<awt<<endl;
cout<<"\nAverage turnaroundtime is:"<<ata<<endl;
return 0;
}
4. PS(Priority Scheduling ) Implementation

#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];

8|Page Operating System


prior[i]=prior[j];
prior[j]=s;
s=bt[i];
bt[i]=bt[j];
bt[j]=s;
s=pno[i];
pno[i]=pno[j];
pno[j]=s;
}
}
}
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];
awt=Twt/n;
att=Ttt/n;
}
cout<<"process\t bt \t prior\t wt \t tat"<<endl;
for(i=0;i<n;i++)
cout<<pno[i]<<"\t"<<bt[i]<<"\t"<<prior[i]<<"\t"<<wt[i]<<"\t"<<tt[i]<<endl;
cout<<"average wating time="<<awt<<endl;
cout<<"average turnaround time="<<att;
return 0;

A Program to Simulate the Priority CPU Scheduling Algorithm Program: When


Arrival Time is non-Zero

9|Page Operating System


#include<stdio.h>
#include<iostream.h>
#include<string.h>
int main()
{
int bt[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:"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter process name || arrivaltime || execution time & priority:"<<endl;
cin>>pn[i]>>at[i]>>bt[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=bt[i];
bt[i]=bt[j];
bt[j]=temp;
strcpy(t,pn[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
}

5. RRS (Round Robin Scheduling )Implementation


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
cout<<"Enter number of processes:"<<endl;
cin>>n;
cout<<"Enter burst time for sequences:"<<endl;
for(i=0;i<n;i++)
{
cin>>bt[i];
st[i]=bt[i];
}
cout<<"Enter time quantum:"<<endl;
cin>>tq;
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}

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

You might also like