Вы находитесь на странице: 1из 26

Kashi Institute of Technology, Varanasi

Lab Manual of Operating System (RCS-451)

Prepared By-
Sunil Kr. Yadav
(Assistant Professor)
CSE Dept.,KIT,Varanasi
List of Experiments

1. Write A Program To Implement CPU Scheduling Algorithm.

1. FCFS
2. SJF
3. SRJF
4. Round-Robin
5. Priority Based.

2. Write A Program To Implement Page Replacement.

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

AIM: Write A Program To Implement CPU Scheduling


Algorithm.

1. FCFS
2. SJF
3. SRJF
4. Round-Robin
5. Priority Based.

1. First Come First Serve (FCFS)


By far the simplest CPU-scheduling algorithm is the first-come, first-served
(FCFS) scheduling algorithm. With this scheme, the process that requests the
CPU first is allocated the CPU first. The implementation of the FCFS policy is
easily managed with a FIFO queue. When a process enters the ready queue, its
PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to
the process at the head of the queue. The running process is then removed from
the queue. The code for FCFS scheduling is simple to write and understand.
On the negative side, the average waiting time under the FCFS policy is often
quite long. Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds:

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;

cout<<"Enter Process Burst Time"<<endl;


for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:"<<endl;
cin>>bt[i];
}

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

cout<<"Process BurstTime WaitingTime TurnaroundTime"<<endl;

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
cout<<"P["<<i+1<<"]"<<endl<<bt[i]<<endl<<wt[i]<<endl<<tat[i]
<<endl;
}

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

printf("Enter Burst Time:n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

//sorting burst time in ascending order using selection sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total;
//average waiting time
total=0;

printf("P.id B.T W.T T.A.T\n");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("%d %d %d %d\n",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total; //average turnaround time


printf("Average Waiting Time=%f \n",avg_wt);
printf("Average Turnaround Time=%f\n",avg_tat);
getch();
}

OUTPUT

3. Shortest Remaining Time First (SRTF)

A preemptive SJF algorithm will preempt the currently executing process,


whereas a nonpreemptive SJF algorithm will allow the currently running process
to finish its CPU burst. Preemptive SJF scheduling is sometimes called shortest-
remaining-time-first scheduling.

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

The round-robin (RR) scheduling algorithm is designed especially for


timesharing systems. It is similar to FCFS scheduling, but preemption is added to
enable the system to switch between processes. A small unit of time, called a time
quantum or time slice, is defined. A time quantum is generally fronc 10 to 100
milliseconds in length. The ready queue is treated as a circular queue.
The CPU scheduler goes around the ready queue, allocating the CPU to each
process for a time interval of up to 1 time quantum. To implement RR scheduling,
we keep the ready queue as a FIFO queue o£ processes. New processes are added
to the tail of the ready queue. The CPU scheduler picks the first process from the
ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the
process. One of two things will then happen. The process may have a CPU burst
of less than 1 time quantum. In this case, the process itself will release the CPU
voluntarily.
The scheduler will then proceed to the next process in the ready queue.
Otherwise, if the CPU burst of the currently running process is longer than 1 time
quantum, the timer will go off and will cause an interrupt to the operating system.
A context switch will be executed, and the process will be put at the tail of the
ready queue. The CPU scheduler will then select the next process in the ready
queue.
PROGRAM
#include<stdio.h>
#include<conio.h>
void 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;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
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;
printf("Pno BT WT TAT\n");
for(i=0;i<n;i++)
printf("%d %d %d %d\n",i+1,bt[i],wt[i],tat[i]); printf("Avg wait time is %f\nAvg
turn around time is %f\n",awt,atat);
getch();
}

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;
}

wt[0]=0; //waiting time for first process is zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

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]);
}

avg_tat=total/n; //average turnaround time


printf("\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d",avg_tat);
getch();
}
OUTPUT
EXPERIMENT -2

AIM: Write A Program To Implement Page Replacement.

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 :

1. First In First Out (FIFO)


When a page must be replaced, the oldest page is chosen.
2. LEAST RECENTLY USED (LRU)

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

AIM: To write a ‘C’ program to implement the Disk Scheduling algorithm


for First Come First Served (FCFS), Shortest Seek Time First (SSTF), and
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.

SCAN Scheduling algorithm –


The disk arm starts at one end of the disk, and moves toward the other end,
servicing requests as it reaches each cylinder, until it gets to the other end of the
disk.
At the other end, the direction of head movement is reversed, and servicing
continues.
The head continuously scans back and forth across the disk. The seek time is
calculated. Display the seek time and terminate the program.

Program :

#include<stdio.h>
#include<math.h>

void fcfs(int noq, int qu[10], int st)


{
int i,s=0;
for(i=0;i<noq;i++)
{
s=s+abs(st-qu[i]);
st=qu[i];
}
printf("\n Total seek time :%d",s);
}

void sstf(int noq, int qu[10], int st, int visit[10])


{
int min,s=0,p,i;
while(1)
{
min=999;
for(i=0;i<noq;i++)
if (visit[i] == 0)
{
if(min > abs(st - qu[i]))
{
min = abs(st-qu[i]);
p = i;
}
}
if(min == 999)
break;
visit[p]=1;
s=s + min;
st = qu[p];
}
printf("\n Total seek time is: %d",s);
}

void scan(int noq, int qu[10], int st, int ch)


{
int i,j,s=0;
for(i=0;i<noq;i++)
{
if(st < qu[i])
{
for(j=i-1; j>= 0;j--)
{
s=s+abs(st - qu[j]);
st = qu[j];
}
if(ch == 3)
{
s = s + abs(st - 0);
st = 0;
}
for(j = 1;j < noq;j++)
{
s= s + abs(st - qu[j]);
st = qu[j];
}
break;
}
}
printf("\n Total seek time : %d",s);
}

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;

case 2: printf("\n SSTF \n");


printf("\n*****\n");
sstf(noq,qu,st,visit);
break;
case 3: printf("\n SCAN \n");
printf("\n*****\n");
scan(noq,qu,st,ch);
break;
case 4: return(0);
}
}
}

OUTPUT

Вам также может понравиться