You are on page 1of 25

Practical : 1

/* Program to implement problem of First Come First Serve */

Source Code
#include<stdio.h>
#include<conio.h>

int main()
{

int jb;
float brstime[10];
float strtime[10];
int i,j;
float wtngtime=0;

float avg;

printf("enter num of process ");


scanf("%d", &jb);

for(i=0;i<jb;i++)
{
printf("enter cpu burst time for process %d\t",i+1);
scanf("%f",&brstime[i]);
}
i=0;
strtime[i]=0;
for(i=1;i<jb;i++)
{
strtime[i]=brstime[i-1]+strtime[i-1];
wtngtime+=strtime[i];
}
avg=wtngtime/jb;
printf("Avg waiting time %f ",avg);

getch();
return 0;
}
Output
Practical : 02

/* Program to implement problem of Shortest Job First */

Source Code
#include<stdio.h>
#include<conio.h>
int main()
{

int i,j,jb,cb[100],x,p[100],key,sum=0,s=0;
float avg;
printf("Enter number of job");
scanf("%d",&jb);
printf("burst time before sorting\n");
printf("CPU Burst time\n");
for(x=0;x<jb;x++)
{
scanf("%d",&cb[x]);
}
printf("Burst time after sorting\n");
for(j=1;j<jb;j++)
{
i=j-1;
key=cb[j];
while(i>-1 && cb[i]>key)
{
cb[i+1]=cb[i];
i--;
}
cb[i+1]=key;
}
for(x=0;x<jb;x++)
{
printf("%d\n",cb[x]);
}

for(x=0;x<jb-1;x++)
{
s=s+cb[x];
}
avg=s/(float)jb;
printf("avg waiting time is:=%f\n",avg);
getch();
return 0;
}

Output
Practical : 03

/* Program to implement Round Robin Algorithm */

Source Code
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int t,n,s,bt[10],ct[10],ta[10],w[10],lat[10],wav,taav;
int allover()
{
for(int i=0;i<n;i++)
if(ct[i]>0)
return 0;
return 1;
}
void select(int p)
{
w[p]+=t-lat[p];
if(ct[p]>=s)
{
ct[p]-=s;
t+=s;
}
else
{
t+=ct[p];
ct[p]=0;
}
if(ct[p]==0)
ta[p]=t;
lat[p]=t;
}
int main()
{
int p=0,t=0,taav=0,wav=0,i;

printf("Enter the number of process : ");


scanf("%d",&n);
printf("\n Enter the time slice : ");
scanf("%d",&s);
printf("\nEnter the burst time of processes \n ");
for(i=0;i<n;i++)
{
printf("\n process %d : ",i+1);
scanf("%d",&bt[i]);
ct[i]=bt[i];
lat[i]=0;
}
while(!allover())
{
if(ct[p]!=0)
select(p);
p=(p+1)%n;
}
printf("\n\n Process Burst time Wait time Turn around \n\n");
for(i=0;i<n;i++)
{
printf("\n %d\t\t%d\t\t%d\t\t%d",i+1,bt[i],w[i],ta[i]);
wav+=w[i];
taav+=ta[i];
}
wav/=n;
taav/=n;
printf("\n\nAverage waiting time : %d ",wav);
printf("\n\nAverage turn around time : %d ",taav);
getch();
return 0;
}
Output
Practical : 04

/* Program to implement Banker’s Algorithm */

Source Code
#include<iostream.h>
#include<conio.h>
int main()
{

int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
cout<<"Enter the no of processes:";
cin>>p;
cout<<"Enter the no of resources:";
cin>>r;
cout<<"Enter the claim for each process:";
for(i=1;i<=p;i++)
{
cout<<"\nFor process "<<i<<":";
for(j=1;j<=r;j++)
{
cin>>clm[i][j];
}}
cout<<"Enter the allocation for each process:";
for(i=1;i<=p;i++)
{
cout<<"\nFor process "<<i<<":";
for(j=1;j<=r;j++)
{
cin>>alloc[i][j];
}}
cout<<"Enter total no of each resource:";
for(j=1;j<=r;j++)
cin>>rsrc[j];
for(j=1;j<=r;j++)
{
avail[j]=0;
int total=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{ for(j=1;j<=r;j++)
{ req[i][j]=clm[i][j]-alloc[i][j];
}}
cout<<"\n\nAvailable resorces is:";
for(j=1;j<=r;j++)
{ cout<<" "<<avail[j]; }
cout<<"\nClaim matrix:\t\tAllocation matrix:\n";
for(i=1;i<=p;i++)
{ for(j=1;j<=r;j++)
{ cout<<clm[i][j]<<" "; }
cout<<"\t\t\t";
for(j=1;j<=r;j++)
{ cout<<alloc[i][j]<<" "; }
cout<<"\n";
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j]<req[i][j])
{ prc=0;break; }
}}
if(prc!=0)
break;
}
if(prc!=0)
{
cout<<"\nProcess "<<prc<<"runs to completion!";
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
cout<<"\nThe system is in a safe state!!";
else
cout<<"\nThe system is in an unsafe state!!";
getch();
return 0;}
Output
Practical : 05

/* Program to implement Priority Scheduling Algorithm*/

Source Code
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("\nEnter the number of process : ");
scanf("%d",&n);
printf("\nGREATER NUMBER GREATER PRIORITY\n");
printf("\n Enter Burst Time & Time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
} }
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
return 0;
}
Output
Practical : 06

/* Program to implement LRU Page Replacement Algorithm*/

Source Code
#include<stdio.h>
int main()
{
int i,j,k,l,Place,f0=0,f1=0,f2=0,f=0,pagefault=0;
int temp[3];
int Frame[3]; //Defining a three frame architecture
int Page[20]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1}; //Set of pages
printf("Defining a three frame architecture");
printf("\n\nList of pages:>\n");
for(i=0;i<20;i++)
{
printf("%d ",Page[i]);//Printing pages
}
printf("\n\nList of Content of frame\n");
Frame[0]=Page[0]; // Initialising
Frame[1]=Page[1]; // first three pages to the
Frame[2]=Page[2]; // Frame
printf("%d %d %d \n",Frame[0],Frame[1],Frame[2]);
for(i=3;i<20;i++)
{
for(j=0;j<3;j++)
{

f=0;
if(Frame[j]==Page[i]) // Checking whether the current page is in Frame
{

f=1;break;
}
}

if(f!=1) //If not


{
for(k=i-1;k>=0;k--)
{

if(f0==0 && Page[k]==Frame[0]){f0=1;} //Checking the


if(f1==0 && Page[k]==Frame[1]){f1=1;}// page to be
if(f2==0 && Page[k]==Frame[2]){f2=1;}//replaced

if(f0==1 && f1==1){Frame[2]=Page[i];pagefault++;break;}


else //Replacing the least
if(f0==1 && f2==1){Frame[1]=Page[i];pagefault++;break;}
else
if(f1==1 && f2==1){Frame[0]=Page[i];pagefault++;break;}
// caculating the page fault
}
printf("%d %d %d \n",Frame[0],Frame[1],Frame[2]);
//Printing the content of the frame
}
f0=0;f1=0;f2=0;f=0;//Resetting all the flag variables
}
printf("\n\n\tNumber of page faults <%d>\n",pagefault);

//Printing the number of page faults


}
Output
Practical : 07

/* Program to implement Optimal Page Replacement Algorithm*/

Source Code
#include<stdio.h>
#include<conio.h>
int fr[3];
int main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
printf("\nOptimal Page Repalcement for 3 Page Frames\n");
printf("\n -1 Represents no page present in frame\n\n");
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}

if(flag2==0)
{
for(i=0;i<3;i++)
lg[i]=0;
for(i=0;i<frsize;i++)
{
for(k=j+1;k<12;k++)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<frsize;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults:%d",pf);
getch();
return(0);
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}
Output
Practical : 08

/* Program to implement FIFO Page Replacement Algorithm*/

Source Code
#include<stdio.h>
#include<conio.h>
int fr[3];
int main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;
printf("\nFrame Size is 3 Total Page 12\n");
printf("\n -1 Represents No page present in frame\n");
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{if(fr[i]==-1)
{fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults : %d ",pf);
getch();
return 0;
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}
Output
Practical : 09

/* Program to implement Scan Disk Scheduling Algorithm*/

Source Code
#include<stdio.h>
#include<conio.h>
int main()
{
int queue[10],init_head,cylin,head_mov=0,i,j,n,pos;

printf("Enter the number the number of requests:");


scanf("%d",&n);
printf("Enter the %d requests:",n);
for(i=0;i<n;i++)
scanf("%d",&queue[i]);
printf("Enter the number of cylinders:");
scanf("%d",&cylin);
printf("Enter the initial head position:");
scanf("%d",&init_head);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(queue[i]>queue[j]){
queue[i]=queue[i]+queue[j];
queue[j]=queue[i]-queue[j];
queue[i]=queue[i]-queue[j];
}
}
}
for(i=0;i<n;i++){
if(queue[i]>init_head){
pos=i;
break;
}
}
head_mov=head_mov+(queue[pos]-init_head);
for(j=pos;j<n-1;j++)
head_mov=head_mov+(queue[j+1]-queue[j]);
head_mov=head_mov+(cylin-queue[j]);
head_mov=head_mov+(cylin-queue[pos-1]);
for(j=pos-1;j>0;j--)
head_mov=head_mov+(queue[j]-queue[j-1]);
printf("\nTotal Head Movements = %d\t",head_mov);
float f=head_mov/(float)n;
printf("\n\nTotal Head Movements = %f",f);
getch();
return 0;
}

Output
Practical : 10

/* Program to implement C-Scan Disk Scheduling Algorithm*/

Source Code

#include<stdio.h>
#include<conio.h>
int main()
{
int queue[10],init_head,cylin,head_mov=0,i,j,n,pos;

printf("Enter the number the number of requests:");


scanf("%d",&n);
printf("Enter the %d requests:",n);
for(i=0;i<n;i++)
scanf("%d",&queue[i]);
printf("Enter the number of cylinders:");
scanf("%d",&cylin);
printf("Enter the initial head position:");
scanf("%d",&init_head);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(queue[i]>queue[j]){
queue[i]=queue[i]+queue[j];
queue[j]=queue[i]-queue[j];
queue[i]=queue[i]-queue[j];
}
}
}
for(i=0;i<n;i++){
if(queue[i]>init_head){
pos=i;
break;
}
}
head_mov=head_mov+(queue[pos]-init_head);
for(j=pos;j<n-1;j++)
head_mov=head_mov+(queue[j+1]-queue[j]);
head_mov=head_mov+(cylin-queue[j]);
printf("Total Head Movements = %d",head_mov);
float f=head_mov/(float)n;
printf("Average Head Movements = %f",f);
getch();
return 0;
}

Output
S.No Practical Name Date Sign

1. First Come First Serve

2. Shortest Job First

3. Round Robin

4. Banker’s Algorithm

5. Priority Scheduling

6. LRU Page Replacement

7. Optimal Page Replacement

8. FIFO Page Replacement

9. SCAN Disk Scheduling Algorithm

10. C-Scan Disk Scheduling Algorithm

You might also like