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

Os lab Manual

The document contains several programs simulating different CPU scheduling algorithms including FCFS, SJF, Priority, Round Robin, FIFO, and LRU. Each section provides the aim, program code, and sample output for the respective algorithm. The programs demonstrate how each scheduling method operates and calculates average waiting and turnaround times or page faults.

Uploaded by

23b01a1253
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Os lab Manual

The document contains several programs simulating different CPU scheduling algorithms including FCFS, SJF, Priority, Round Robin, FIFO, and LRU. Each section provides the aim, program code, and sample output for the respective algorithm. The programs demonstrate how each scheduling method operates and calculates average waiting and turnaround times or page faults.

Uploaded by

23b01a1253
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

FCFS

Aim: A Program to Simulate the FCFS CPU Scheduling


Algorithm.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the process Name,Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[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];
}
}
printf("\nPName Arrtime Burtime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],
finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n);
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
getch();
}
OUTPUT:
Enter the Number of Processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 2 3
Enter the Process Name, Arrival Time & Burst Time: 2 5 6
Enter the Process Name, Arrival Time & Burst Time: 3 6 7
Pname Arrtime Burtime Start TAT Finish
1 2 3 2 3 5
2 5 6 5 6 11
3 6 7 11 12 18
Average Waiting Time: 1.666667
Average Turn around Time: 7.000000
SJF
Aim: A Program to Simulate the SJF CPU Scheduling
Algorithm.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void 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];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrival time & execution time:");
flushall();
scanf("%s%d%d",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+=at[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("/nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
OUTPUT:
Enter the Number of Processes: 3
Enter the Process Name, Arrival Time & Execution time: 1 4 6
Enter the Process Name, Arrival Time & Execution time: 2 5 15
Enter the Process Name, Arrival Time & Execution time: 3 6 11
Pname arrivaltime executiontime Waitingtime tatime
1 4 6 0
3 6 11 4
2 5 15 16

Average Waiting Time is: 6.666667


Average turn around time is: 17.333333334
PRIORITY
Aim: A Program to simulate the PRIORITY CPU
Scheduling Algorithm.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void 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];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("Enter the name ,arrivaltime,execution time &priority:");
flushall();
scanf("5s%d%d%d",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;
printf("/n Pname\tarrivaltime\texecution\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t%5d\t\t%5d\t\t%5d\t\t5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
OUTPUT:
Enter the Number of Processes: 3
Enter the Process Name, Arrival Time & Execution time & priority: 1 2 3 1
Enter the Process Name, Arrival Time & Execution time & priority: 2 4 5 2
Enter the Process Name, Arrival Time & Execution time & priority: 3 5 6 3

Pname Arrivaltime Executiontime waitingtime tattime


1 2 3 1 0
2 4 5 2 1
3 5 6 3 5

Average waiting time is: 2.0000000


Average Turnaround time is: 6.6666667
ROUND ROBIN
Aim: A Program to Simulate the ROUND ROBIN CPU
Scheduling Algorithm.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
clrscr();
printf("Enter the no of process:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter process name & estimated time:");
scanf("%s%d",pn[i],&et[i]);
}
printf("The process are:");
for(i=0;i<n;i++)
printf("process %d:%s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s->%d",pn[i],ts);
et[i]=et[i]-ts;
}
elseif((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n %s->%d",pn[i],et[i]);
et[i]=0;
}
}
}
printf("\n Total Estimated Time:%d",x);
getch();
}
OUTPUT:
Enter the No of Process: 2
Enter the time quantum: 3
Enter Process name & estimated time: p1 12
Enter Process name & estimated time: p2 15
The Process Are: 1:p1
2:p2
P1 ->3
P2 ->3
P1 ->3
P2 ->3
P1 ->3
P2 ->3
P1 ->3
P2 ->3
P2 ->3

Total Estimated Time: 27


FIFO
AIM: A Program to Simulate the FIFO Page
Replacement Algorithm.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
clrscr();
printf("Enter the Number of pages:");
scanf("%d",&n);
printf("Enter %d page Number:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m);
getch();
}
OUTPUT:
Enter the No of pages: 12
Enter 12 page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
2 2-->F
3 23-->F
2 23
1 231-->F
5 531-->F
2 521-->F
4 524 -->F
5 524
3 324 -->F
2 324
5 354 -->F
2 352 -->F

No of faults: 9
LRU
AIM: A Program to Simulate the LRU Page
Replacement Algorithm.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,I,q1,j,u,n;
char f=’F’;
clrscr();
printf(“Enter the no of pages:”);
scanf(“%d”,&n);
printf(“Enter %d page numbers:”,n);
for(i=0;i<n;i++)
scanf(“%d”,&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
g=1;
} }
printf(“\n%d”,b[i]);
printf(“\t”);
for(h=0;h<q;h++)
printf(“%d”,a[h]);
if((p==0)&&(q<=3))
{
Printf(“-->%c”,f);
m++;
}
p=0;
q=0;
if(q1==3)
{
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
for(j=0;j<q1;j++)
{
u=0;
k=i;
while(k>=(i-1)&&(k>=0))
{
if(b[k]==a[j])
u++;
k--;
}
if(u==0)
q=j;
} }
else
{
for(k=0;k<q;k++)
{
if(b[i+1]==a[k])
p=1;
} } }
printf(“\n No of Faults:%d”,m);
getch();
}
OUTPUT:

Enter the No of pages: 12


Enter the 12 page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
2 2-->F
3 23-->F
2 23
1 231-->F
5 251-->F
2 251
4 254-->F
5 254
3 354-->F
2 352-->F
5 352
2 352

No of Faults: 7

You might also like