Академический Документы
Профессиональный Документы
Культура Документы
Prepared By-
Sunil Kr. Yadav
(Assistant Professor)
CSE Dept.,KIT,Varanasi
List of Experiments
1. FCFS
2. SJF
3. SRJF
4. Round-Robin
5. Priority Based.
1. FIFO
2. LRU
3. OPTIMAL
3. IMPLEMENTATION OF DISK SCHEDULING ALGORITHMS.
1. FCFS
2. SSTF
3. SCAN
OPERATING SYSTEM LAB
RCS-451
EXPERIMENT -1
1. FCFS
2. SJF
3. SRJF
4. Round-Robin
5. Priority Based.
PROGRAM
#include<iostream.h>
#include<conio.h>
void main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
clrscr();
cout<<"Enter total number of processes(maximum 20):"<<endl;
cin>>n;
avwt/=i;
avtat/=i;
cout<<"Average Waiting Time:"<<avwt<<endl;
cout<<"Average Turnaround Time:"<<avtat<<endl;
//return(0);
getch();
OUTPUT
2. Shortest -Job –First (SJF)
A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling
algorithm. This algorithm associates with each process the length of the process's
next CPU burst. When the CPU is available, it is assigned to the process that has
the smallest next CPU burst. If the next CPU bursts of two processes are the same,
FCFS scheduling is used to break the tie. Note that a more appropriate term for
this scheduling method would be the shortest-next-CPU-burst algorithm, because
scheduling depends on the length of the next CPU burst of a process, rather than
its total length.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
clrscr();
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
avg_wt=(float)total;
//average waiting time
total=0;
OUTPUT
PROGRAM
#include<iostream.h>
#include<conio.h>
void main()
{
int n=3;
float total,wait[3]={0};
float p[3],twaiting=0,waiting=0;
int proc;
int stack[3];
float brust[3],arrival[3],sbrust,temp[3],top=3;
clrscr();
for(int i=0;i<n;i++)
{
p[i]=i;
stack[i]=i;
cout<<"enter arival time :";
cin>>arrival[i];
cout<<"enter brust time:";
cin>>brust[i];
temp[i]=arrival[i];
sbrust=brust[i]+sbrust;
}
for(i=0;i<sbrust;i++)
{
proc=stack[0];
if(temp[proc]==i)
twaiting=0;
else
twaiting=i-(temp[proc]);
temp[proc]=i+1;
wait[proc]=wait[proc]+twaiting;
waiting=waiting+(twaiting);
brust[proc]=brust[proc]-1;
if(brust[proc]==0)
{
for(int x=0;x<top-1;x++)
stack[x]=stack[x+1];
top=top-1;
}
for(int z=0;z<top-1;z++)
{
if((brust[stack[0]]>brust[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))
{
int t=stack[0];
stack[0]=stack[z+1];
stack[z+1]=t;
}
}
}
cout<<endl<<"waiting:"<<waiting;
//return 0;
getch();
}
OUTPUT
4. Round-Robin (R.R.)
OUTPUT
5. Priority Based (PRIORITY)
The SJF algorithm is a special case of the general priority scheduling algorithm.
A priority is associated with each process, and the CPU is allocated to the process
with the highest priority. Equal-priority processes are scheduled in FCFS order.
An SJF algorithm is simply a priority algorithm where the priority (p) is the
inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower
the priority, and vice versa.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
clrscr();
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority");
for(i=0;i<n;i++)
{
printf("\nP[%d]:",i+1);
printf("Brust Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
printf("Pid BT WT TAT\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("P[%d] %d %d %d\n",p[i],bt[i],wt[i],tat[i]);
}
1. FIFO
2. LRU
3. OPTIMAL
Page replacement :
When a page fault occurs, the operating system has to choose a page to remove
from memory to make room for the page that has to be brought in.Following
algorithms are used for this purpose :
When a page must be replaced, LRU chooses the page that has not been used
for the longest period of time.
3. OPTIMAL
Optimal Page Replacement refers to the removal of the page that will not be
used in the future, for the longest period of time.
PROGRAM
#include<stdio.h>
#include<conio.h>
int n,pg[30],fr[10];
void fifo();
void optimal();
void lru();
void main()
{ int i,ch;
clrscr();
printf("\nEnter total number of pages:");
scanf("%d",&n);
printf("\nEnter sequence:\n");
for(i=0;i<n;i++) //accepting sequence
scanf("%d",&pg[i]);
do
{
printf("\nMENU\n");
printf("\n1)FIFO");
printf("\n2)OPTIMAL");
printf("\n3)LRU");
printf("\n4)Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: fifo();
break;
case 2: optimal();
break;
case 3: lru();
break;
}}
while(ch!=4);
getch();
}
void fifo()
{ int i,f,r,s,count,flag,num,psize;
f=0;
r=0;
s=0;
flag=0;
count=0;
printf("\nEnter size of page frame:");
scanf("%d",&psize);
for(i=0;i<psize;i++)
{ fr[i]=1;
}
while(s<n)
{
flag=0;
num=pg[s];
for(i=0;i<psize;i++)
{ if(num==fr[i])
{s++;
flag=1;
break;
}} if(flag==0)
{ if(r<psize)
{ fr[r]=pg[s];
r++;
s++;
count++;
}
else
{ if(f<psize)
{ fr[f]=pg[s];
s++;
f++;
count++;
}
else
f=0;
}}
printf("\n");
for(i=0;i<psize;i++)
{
printf("%d",fr[i]);
}}
printf("\nPage Faults=%d",count);
getch();
}
void optimal()
{ int count[10],i,j,k,fault,f,flag,temp,current,c,max,m,cnt,p,x;
fault=0;
k=0;
printf("\nEnter frame size:");
scanf("%d",&f);
//initilizing distance and frame array
for(i=0;i<f;i++)
{
count[i]=0;
fr[i]=1;
} for(i=0;i<n;i++)
{ flag=0;
temp=pg[i];
for(j=0;j<f;j++)
{ if(temp==fr[j])
{ flag=1;
break;
}} if((flag==0)&&(k<f))
{ fault++;
fr[k]=temp;
k++;
}else if((flag==0)&&(k==f))
{ fault++;
for(cnt=0;cnt<f;cnt++)
{current=fr[cnt];
for(c=i;c<n;c++)
{ if(current!=pg[c])
count[cnt]++;
else
break;
}}
max=0;
for(m=0;m<f;m++)
{ if(count[m]>max)
{
max=count[m];
p=m;
}} fr[p]=temp;
}
printf("\n");
for(x=0;x<f;x++)
{
printf("%d\t",fr[x]);
}} printf("\nTotal number of faults=%d",fault);
getch();
}
void lru()
{ int count[10],i,j,k,fault,f,flag,temp,current,c,max,m,cnt,p,x;
fault=0;
k=0;
printf("\nEnter frame size:");
scanf("%d",&f);
//initilizing distance and frame array
for(i=0;i<f;i++)
{count[i]=0;
fr[i]=1;
} for(i=0;i<n;i++)
{ flag=0;
temp=pg[i];
for(j=0;j<f;j++)
{ if(temp==fr[j])
{ flag=1;
break;
}} if((flag==0)&&(k<f))
{ fault++;
fr[k]=temp;
k++;
}else if((flag==0)&&(k==f))
{ fault++;
for(cnt=0;cnt<f;cnt++)
{current=fr[cnt];
for(c=i;c>0;c++)
{ if(current!=pg[c])
count[cnt]++;
else
break;
}}
max=0;
for(m=0;m<f;m++)
{
if(count[m]>max)
{
max=count[m];
p=m;
}} fr[p]=temp;
}
printf("\n");
for(x=0;x<f;x++)
{
printf("%d\t",fr[x]);
}}
printf("\nTotal number of faults=%d",fault);
getch();
}
OUTPUT
EXPERIMENT -3
IMPLEMENTATION OF DISK SCHEDULING ALGORITHMS.
1. FCFS
2. SSTF
3. SCAN
PROBLEM DESCRIPTION:
Disk Scheduling is the process of deciding which of the cylinder request is in the
ready queue is to be accessed next. The access time and the bandwidth can be
improved by scheduling the servicing of disk I/O requests in good order.
Access Time:
The access time has two major components: Seek time and Rotational
Latency.
Seek Time:
Seek time is the time for disk arm to move the heads to the cylinder
containing the desired sector.
Rotational Latency:
Rotational latency is the additional time waiting for the disk to rotate the
desired sector to the disk head.
Bandwidth:
The disk bandwidth is the total number of bytes transferred, divided by
the total time between the first request for service and the completion of the last
transfer.
ALGORITHM:
Input the maximum number of cylinders and work queue and its head starting
position.
First Come First Serve Scheduling (FCFS) algorithm –
The operations are performed in order requested.There is no reordering of work
queue.
Every request is serviced, so there is no starvation. The seek time is calculated.
Shortest Seek Time First Scheduling (SSTF) algorithm –
This algorithm selects the request with the minimum seek time from the current
head position.
Since seek time increases with the number of cylinders traversed by the head,
SSTF chooses the pending request closest to the current head position.
The seek time is calculated.
Program :
#include<stdio.h>
#include<math.h>
int main()
{
Clrscr();
int n,qu[20],st,i,j,t,noq,ch,visit[20];
printf("\n Enter the maximum number of cylinders : ");
scanf("%d",&n);
printf("enter number of queue elements");
scanf("%d",&noq);
printf("\n Enter the work queue");
for(i=0;i<noq;i++)
{
scanf("%d",&qu[i]);
visit[i] = 0;
}
printf("\n Enter the disk head starting posision: \n");
scanf("%d",&st);
while(1)
{
printf("\n\n\t\t MENU \n");
printf("\n\n\t\t 1. FCFS \n");
printf("\n\n\t\t 2. SSTF \n");
printf("\n\n\t\t 3. SCAN \n");
printf("\n\n\t\t 4. EXIT \n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
if(ch > 2)
{
for(i=0;i<noq;i++)
for(j=i+1;j<noq;j++)
if(qu[i]>qu[j])
{
t=qu[i];
qu[i] = qu[j];
qu[j] = t;
}
}
switch(ch)
{
case 1: printf("\n FCFS \n");
printf("\n*****\n");
fcfs(noq,qu,st);
break;
OUTPUT