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

Operating System Lab

The document describes algorithms for CPU scheduling and memory management. It includes code implementations of First Come First Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), Priority scheduling, and Round Robin algorithms. It also includes code to simulate First In First Out (FIFO) and Least Recently Used (LRU) page replacement algorithms and the paging technique for memory management.

Uploaded by

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

Operating System Lab

The document describes algorithms for CPU scheduling and memory management. It includes code implementations of First Come First Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), Priority scheduling, and Round Robin algorithms. It also includes code to simulate First In First Out (FIFO) and Least Recently Used (LRU) page replacement algorithms and the paging technique for memory management.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1

Operating System Lab

NO TITLE Page no

A CPU Scheduling Algorithm 2

A1 FCFS(First Come First Serve) 2-3

A2 SJF(Shortest Job first) 4-6

A3 SRTF(shortest remaining time first) 7-9

A4 PRIORITY 10-12

A5 ROUND ROBIN 13-15

B Simulate all Pages Replacement algorithms 16

B1 FIFO(first come first serve) 16-18

B2 LRU(least Recently used) 19-22

C Simulate Paging Technique of Memory Management 23-24


2

A CPU Scheduling Algorithm


A1 : First come First Serve
#include<stdio.h>
#include<conio.h>
void main() {
int bt[20],wt[20],tat[20],i,n;
float wtavg,tatavg;
clrscr();
printf("\n Enter the number of process ---");
scanf("%d",&n);
for(i=0;i<n;i++){
printf(" Enter Burst Time for Process[%d]=",i);
scanf("%d",&bt[i]);
}
wt[0]=wtavg=0;
tat[0]=tatavg=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=tat[i-1]+bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg+tat[i];
}
printf("\t Process\tBurst Time\tWating time\tTurnaroundTime\n");
3

for(i=0;i<n;i++)
printf("\tP%d\t\t%d\t\t%d\t\t%d\n",i,bt[i],wt[i],tat[i]);
printf("\n Average Waiting Time --%f",wtavg/n);
printf("\n Average turnaround Time --%f",tatavg/n);
getch();
}

FCFS ScreenShot
4

A2: Shortest Job First


#include<stdio.h>
#include<conio.h>
void main()
{
int p[20],bt[20],wt[20],tat[20],i,k,n,temp;
float wtavg,tatavg;
clrscr();
printf("\nEnter the no of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("enter the burst time %d--",i);
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
5

temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0]=wtavg=0;
tat[0]=tatavg=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=tat[i-1]+bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg=tat[i];
}
printf("\n\tPROCEESS\tBURST TIME \tWAITING TIME \t TURNARROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\tP%d\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}printf("\nAverage waiting Time--%f",wtavg);
printf(\nAverage Turnarround Time-- %f"tatavg/n);
getch();
}
6

SJF Screenshot
7

A3: Shortest Remaining Time First


#include <stdio.h>
int main()
{
int a[10],b[10],x[10],i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;
printf("enter the number of Processes:\n");
scanf("%d",&n);
printf("enter arrival time\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter burst time\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
x[i]=b[i];
b[9]=9999;
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
smallest=i;
8

}
b[smallest]--;
if(b[smallest]==0)
{
count++;
end=time+1;
avg=avg+end-a[smallest]-x[smallest];
tt= tt+end-a[smallest];
}
}
printf("\n\nAverage waiting time = %lf\n",avg/n);
printf("Average Turnaround time = %lf",tt/n);
return 0;
}
9

SRTF ScreenShot
10

A3: Priority
#include<stdio.h>
void main()
{
int p[20],bt[20],pri[20],wt[20],tat[20],i,k,n,temp;
float wtavg,tatavg;
clrscr();
printf("enter the number of process--");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("enter the burst time and priorty of Proces %d--",i);
scanf("%d%d",&bt[i],&pri[i]);
}
for(k=i+1;k<n;k++)
if(pri[i]>pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=p[i];
bt[i]=bt[k];
bt[k]=temp;
11

temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg=wt[0]=0;
tatavg=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+b[i-1];
tat[i]=tat[i-1]+bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg+tat[i];
}
printf("\nProcess\tPriorty\t Burst time\t Waiting Time \t Turnaround Time");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waitng Time is -%f",wtavg/n);
printf("\n Average Turnaround Time is --%f",tatavg/n);
getch();
}
12

PRIORITY ScreenShot
13

A4: Round Robin


#include<stdio.h>
void main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("enter the no of process-");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter burst Time for process %d--",i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice--");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
14

if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\n The Avrage Turnaround time is --%f",att/n);
printf("\n The Average waiting time is --%f ",awt/n);
printf("\n\tPROCESS\t\tBURST TIME\t WAITING TIME\tTURNARUND TIME \n");
for(i=0;i<n;i++)
printf("\t%d\t\t%d\t\t%d\t\t%d\n",i+1,ct[i],wa[i],tat[i]);
getch();
}
15

RoundRobin ScreenShot:
16

B Simulate all Pages Replacement algorithms


B1: First In First Out
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,f,pf=0,count=0,rs[25],m[10],n;
clrscr();
printf("Enter the length of reference string--");
scanf("%d",&n);
printf("enter the reference string ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\nEnter no of frames--");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("The PageReplacement Process--\n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
17

}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF NO %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of page fault FIFO are %d",pf);
getch();}
18

FIFO ScreeenShot:
19

B2 Least Recently Used


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,min,rs[25],m[10],count[10],flag[25],n,f,pf=0,next=1;
clrscr();
printf("Enter the length of reference string--");
scanf("%d",&n);
printf("enter the reference string ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter no of frames--");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("The PageReplacement Process--\n");
for(i=0;i<n;i++)
20

{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min]>count[j])
min=j;
m[min]=rs[i];
21

count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t",m[j]);
if(flag[i]==0)
printf("PF NO-- %d",pf);
printf("\n");
}
printf("\n The number of page fault using LRU are %d",pf);
getch();
}
22

LRU Screen SHOT


23

Simulate Paging Technique of Memory Management


#include<stdio.h>
#include<conio.h>
void main()
{
int ms,ps,nop,np,rempages,i,j,x,y,pa,offset;
int s[10],fno[10][20];
clrscr();
printf("Enter the memory size--");
scanf("%d",&ms);
printf(" enter the page size--");
scanf("%d",&ps);
nop=ms/ps;
printf("\n The no of pages available in memory are %d",nop);
printf("\n Enter number of process --");
scanf("%d",&np);
rempages=nop;
for(i=1;i<=np;i++)
{
printf("Enter no of pages required for p[%d]--",i);
scanf("%d",&s[i]);
if(s[i]>rempages)
{
printf("Memory Full ");
24

break;
}
rempages=rempages-s[i];
printf("Enter pagetable for[%d]--",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\n Enter Logical Addresss to find pysical Address");
printf("\n Enter process no and pagenumber and offset--");
scanf("%d%d%d",&x,&y,&offset);
if (x>np||y>=s[i]||offset>=ps)
printf("\n In valid proceess or page number or offset ");
else
{
pa=fno[x][y]*ps+offset;
printf("\n the physical address is %d",pa);
}
getch();
}
25

ScreenShot
26

You might also like