You are on page 1of 47

B.

TECH 3-2 SEM (2010-2011)

Table of contents

ROUND ROBIN SCHEDULING ALGORITHM………………………….2

SHORTEST JOB FIRST……………………………………………………9

FIRST COME FIRST SERVE………………………………………….…14

PRIORITY SCHEDULING…………………………………………….…..19

FIRST IN FIRST OUT………………………………………………….…..25

LEAST RECENTLY USED………………………………………………..30

LEAST FREAQUENTLY USED…………………………………………..36

BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE………….40

HT NO: 08E21A0594 Page 1


B.TECH 3-2 SEM (2010-2011)

EXPERIMENT NO: 1

ROUND ROBIN SCHEDULING ALGORITHM

AIM: TO STUDY THE ROUND ROBIN CPU ALGORITHM

The ROUND ROBIN SCHEDULING ALGORITHM is designed


especially for the time sharing systems. A small unit of time called a time
quantum is defined. A time quantum is generally from ten to hundred
milliseconds. 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 one time quantum.

To implement the round robin scheduling, we keep the ready queue as


a FIFO queue of the 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 one time quantum in a row. If a process
CPU burst exceeds one time quantum, that process is preempted and is put
back in the ready queue. The RR scheduling algorithm is preemptive.

ALGORITHM:

1. Create a structure to represent process details.


2. Get the number of processes as input from the user and time slice.
3. Get the input from the process attributes, which are process name,
arrival time, service time.
4. Sort the circular linked list nodes based on its arrival time and if the
arrival time and if the arrival times of the processes are same then sort
it based in its service time.
5. Calculate the other process attributes such as TAT, average TAT,
waiting time, normalized TAT, initial starting time and final finish time.
6. RR is preemptive scheduling algorithm, so we need to preempt the
process execution up to the elapse of the time slice.
7. We allocate the CPU towards to the process on the first node of the
linked list.
8. Up to the time interval switch the CPU towards next process and reduce
the previous process service time by time slice. If the service time is

HT NO: 08E21A0594 Page 2


B.TECH 3-2 SEM (2010-2011)

found to be less than or equal to zero print the process attributes and
delete the node from the linked lists.
9. Repeat the step 8 until the linked list is empty.
10. The formula for waiting time initial starts time-arrival time of the
process.
11. Formula for TAT is :( Final Finish Time-Initial Staring Time) +waiting
time.
12. Formula for NTAT is (TAT)/(service time).
13. The finish time of the process is nothing but the sum of the time slice
and the finish time of previous process. Also the finish time of the
previous process is its time slice or its service time.
14. Intermediate starting time of the second process would be intermediate
finish time of the first process or arrival time of the second process,
whichever is bigger.
15. Calculate the other process attributes which are mentioned above.
16. Repeat these steps for each and every process.

SAMPLE RUN:

Required input: process name, Arrival time, Service time, Time slice
Expected output: process name, Arrival time, Service time, turn
around time, waiting time.
EXAMPLE OUTPUT:
Process name service time TAT waiting time arrival time
A 3 5 4 0
B 4 5 3 0
C 5 6 3 0

Average turn around time = 13.3333333

Average waiting time = 5.333333

HT NO: 08E21A0594 Page 3


B.TECH 3-2 SEM (2010-2011)

Program to implement RR Algorithm

/*Program to implement Round Robin algorithm*/

#include<stdio.h>
#include<conio.h>
struct process
{
char na[20];
int at,bt,ft,tat,rem;
float ntat;
}Q[5],temp;
int rr[20],q,x,k;
Void main()
{
int f,r,n,i,j,tt=0,qt,t,flag,wt=0;
float awt=0,antat=0,atat=0;
clrscr();
printf("Enter the no. of jobs:");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("Enter process name,arrival time and burst time:");
scanf("%s%d%d",Q[r].na,&Q[r].at,&Q[r].bt);
}

printf("Enter quantum:");
scanf("%d",&qt);
for(i=0;i<n;i++)
HT NO: 08E21A0594 Page 4
B.TECH 3-2 SEM (2010-2011)

{
for(j=i+1;j<n;j++)
{
if(Q[i].at>Q[j].at)
{
temp=Q[i];
Q[i]=Q[j];
Q[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
Q[i].rem=Q[i].bt;
Q[i].ft=0;
}
tt=0;
q=0;
rr[q]=0;
do
{
for(j=0;j<n;j++)
if(tt>=Q[j].at)
{
x=0;
for(k=0;k<=q;k++)
if(rr[k]==j)
HT NO: 08E21A0594 Page 5
B.TECH 3-2 SEM (2010-2011)

x++;
if(x==0)
{
q++;
rr[q]=j;
}
}
if(q==0)
i=0;
if(Q[i].rem==0)
i++;
if(i>q)
i=(i-1)%q;
if(i<=q)
{
if(Q[i].rem>0)
{
if(Q[i].rem<qt)

{
tt+=Q[i].rem;
Q[i].rem=0;
}
else
{
tt+=qt;
Q[i].rem-=qt;
HT NO: 08E21A0594 Page 6
B.TECH 3-2 SEM (2010-2011)

}
Q[i].ft=tt;
}
i++;
}
flag=0;
for(j=0;j<n;j++)
if(Q[j].rem>0)
flag++;
}while(flag!=0);
clrscr();
printf("\n\n\t\tROUND ROBIN ALGORITHM");
printf("\n***************************");
printf("\nprocesses Arrival time burst time finish time tat wt ntat");
for(f=0;f<n;f++)
{
wt=Q[f].ft-Q[f].bt-Q[f].at;
Q[f].tat=Q[f].ft-Q[f].at;
Q[f].ntat=(float)Q[f].tat/Q[f].bt;
antat+=Q[f].ntat;
atat+=Q[f].tat;
awt+=wt;
printf("\n\t%s\t%d\t\t%d\t%d\t%d\t%d %f",
Q[f].na,Q[f].at,Q[f].bt,Q[f].ft,Q[f].tat,wt,Q[f].ntat);
}
antat/=n;
atat/=n;
HT NO: 08E21A0594 Page 7
B.TECH 3-2 SEM (2010-2011)

awt/=n;
printf("\nAverage tat is %f",atat);
printf("\nAverage normalised tat is %f",antat);
printf("\n average waiting time is %f",awt);
getch();
}
Enter number of processes: 5
OUTPUT:
Process name service time TAT waiting time arrival time
A 3 5 4 0
B 4 5 3 0
C 5 6 3 0

Average turn around time = 13.3333333

Average waiting time = 5.333333

HT NO: 08E21A0594 Page 8


B.TECH 3-2 SEM (2010-2011)

Experiment number: 2

SHORTEST JOB FIRST


Aim: To study the shortest job first CPU scheduling algorithm.

Least frequently used page replacement algorithm requires that the


page with the smallest count be replaced. The reason for this selection is that
an actively used page should have a large reference count. This algorithm
suffers from the situation in which a page is used heavily during the initial
phase of a process, but then is never used again. Since it was used heavily, it
has a large count and remains in memory ever through it are no longer
needed. One solution is to shift the counts right by 1 bit at regular intervals,
forming an exponentially decaying average usage count.

ALGORITHM:

1. Create a structure to represent process details.


2. Get the number of process as input from user.
3. Get the input for the process attributes, which are process name, arrival
time, service time.
4. Sort the list nodes based on its service time and if service times are
same then sort it based on its arrival time.
5. Calculate the other process attributes such as TAT, avg TAT, waiting
time, avg waiting time, normalized TAT.
6. The formula for waiting time is,
Starting time - arrival time of the process.
7. The formula for calculating the TAT is,
TAT=(finish time- starting time) + waiting time.
8. The formula for calculating the NTAT is,
NTAT=TAT / service time.
9. The finish time of the process is nothing but the sum of service time of
this process and the finish time of this process is starting time of this
process + starting time of the process.
10. Starting time of second process would be finish time of first process or
arrival time of second process, whichever is bigger.
11. Calculate the other process attributes which are mentioned above.
12. Repeat these steps for each and every process.

HT NO: 08E21A0594 Page 9


B.TECH 3-2 SEM (2010-2011)

13. Print the result.

SAMPLE RUN:

Required input: process name, arrival time, service time.


Expected output: process name, Arrival time, service time,
starting time, finish time, turn around time,
normalized turn around time.

Example output:

Turn Normalized
Process Arrival Service Start Finish
around turn around
name time time time time
time time
C 2 1 2 3 1 1.00
A 0 1 3 4 4 4.00
B 1 1004 4 104 103 1.03
D 3 100 104 204 201 2.01

HT NO: 08E21A0594 Page 10


B.TECH 3-2 SEM (2010-2011)

Program to implement SHORTEST JOB FIRST algorithm

/*PROGRAM TO IMPLEMENT SHORTEST JOB FIRST ALGORITHM*/


#include<stdio.h>
#include<conio.h>
struct process
{
char name[20];
int at,bt,wt,tat,ft;
}q[20],temp;

void main()
{
int f,r,i,j,n,k,l;
float awt=0;
clrscr();
printf("enter the num of process :");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("\n enter the process name, arrival time and burst time=");
scanf("%s%d%d", &q[r].name, &q[r].at,&q[r].bt);
q[r].wt=0;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(q[i].at>q[j].at)
{
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
printf("\n\n\t\t\t\t\t\t ** SHORTEST JOB FIRST **");
printf("\n ------------------------------------------------");
printf("---------------------------------------------------");
printf("\n process name Arrival time burst time finish time waiting time
turn around time");

HT NO: 08E21A0594 Page 11


B.TECH 3-2 SEM (2010-2011)

printf("\n-------------------------------------------------");
for(f=0;f<n;f++)
{
if(f==0)
q[f].ft=q[f].bt+q[f].at;
else
q[f].ft=q[f-1].ft+q[f].bt;
for(j=f+1;j<n;j++)
if(q[f].ft>=q[j].at)
{
i=j;
for(k=j+1;k<n;k++)
if(q[f].ft>=q[k].at&&q[i].bt>q[k].bt)
i=k;
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
q[f].tat=q[f].ft-q[f].at;
if(f==0)
q[f].wt=0;
else
q[f].wt=q[f-1].ft-q[f].at;
if(q[f].wt<0)
q[f].wt=0;
awt=awt+q[f].wt;
printf("\n\t%-12s %-11d",q[f].name,q[f]
.at);
printf("%-11d %-11d %-14d",q[f].bt, q[f].ft, q[f].wt,q[f].tat);
}
printf("\n------------------------------------------------------------------");
awt=awt/n;
printf("\n\n\t\taverage waiting time=%5.4f",awt);
getch();
}

HT NO: 08E21A0594 Page 12


B.TECH 3-2 SEM (2010-2011)

Output:
Enter the num of processes: 5

Enter the process name, arrival time and burst time= a 0 3


Enter the process name, arrival time and burst time= b 2 6
Enter the process name, arrival time and burst time= c 4 4
Enter the process name, arrival time and burst time= d 6 5
Enter the process name, arrival time and burst time= e 8 2

** SHORTEST JOB FIRST **

Waiting Turn
Name Arrival time Burst time Finish time around
time

A 0 3 3 0 3

B 2 6 9 1 7

E 8 2 11 1 3

C 4 4 15 7 11

D 6 5 20 9 14

Average waiting time = 3.600000

HT NO: 08E21A0594 Page 13


B.TECH 3-2 SEM (2010-2011)

Experiment number: 3

FIRST COME FIRST SERVE

Aim: To study the FCFS CPU Algorithm.

With this scheme, the process that requests the CPU first is
allocated the CPU first. This FCFS policy is easily managed with a
FCFS 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.

Algorithm:
1. Create a structure to represent process details.
2. Get the number of process as input from the user.
3. Get the input for the process attributes, which are process name,
arrival time, service time.
4. Calculate the process attributes such TAT, avg TAT, waiting time,
avg waiting time, normalized TAT.
5. Formula for waiting time is
Starting time – arrival time of that process.
6. The formula for TAT is
(Finish time – starting time) + waiting time.
7. The formula for calculating normalized TAT is
(TAT) / (service time).
8. The finish time of the process is nothing but the sum of the service
time of this process and the finish time of the previous process.
Also, the finish time of this process + service time of the process.
9. Starting time of the second process would be finish time of the first
process or arrival time of the second process, whichever is
bigger.
10. Calculate the other process attributes which are mentioned
above.

HT NO: 08E21A0594 Page 14


B.TECH 3-2 SEM (2010-2011)

11. Repeat these steps for each and every process.


12. Print the result.

SAMPLE RUN:

Required input: process name, Arrival time, service time.

Expected output: process name, arrival time, service time, starting


time, finish time, turn around time, normalized
turn around time.

Example output:

Turn
Process Arrival Service Start Finish Normalized
around
name time time time time TAT
time
A 0 1 0 1 1 1
B 1 100 1 101 100 1
C 2 1 101 102 100 100
D 3 100 102 202 199 1.99

HT NO: 08E21A0594 Page 15


B.TECH 3-2 SEM (2010-2011)

Program for FCFS scheduling algorithm

/* PROGRAM FOR FCFS SCHEDULING ALGORITHM */


#include<stdio.h>
#include<conio.h>
struct process
{
char name[20];
int at,bt;

}q[20],temp;

void main()
{
int f,r,i,j,n,ft,wt,tat;
float awt=0;
clrscr();
printf("enter the number of processes:");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("\nenter process name,arrival time and burst time :");
scanf("%s%d%d",&q[r].name,&q[r].at,&q[r].bt);
}
for(i=0;i<n;i++)
for(j=j+1;j<n;j++)
if(q[i].at>q[j].at)
{
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
printf("\n\n\t\t\t **FCFS**");
printf("\n--------------------------------");
printf("----------------------------------");
printf("\n process name Arrival time Burst time");
printf("Finish time Turn around time Waiting time");

HT NO: 08E21A0594 Page 16


B.TECH 3-2 SEM (2010-2011)

printf("\n---------------------------------------------");
printf("_______________________________________________");
wt=0;
ft=q[0].at;
tat=0;
for(f=0;f<n;f++)
{
wt=ft-q[f].at;
if(wt<0) wt=0;
ft=ft+q[f].bt;
tat=ft-q[f].at;
awt=awt+wt;
printf("\n%-14s,%-11d",q[f].name,q[f].at);
printf("%-11d %-15d %-8d",q[f].bt,ft,tat,wt);
}
printf("\n-----------------------------------");
printf("-------------------------------------");
awt=awt/n;
printf("\n\n\t\t average waiting time=%5.4f", awt);
getch();
}

HT NO: 08E21A0594 Page 17


B.TECH 3-2 SEM (2010-2011)

Output:

Enter the number of process: 5


Enter process name, arrival time and burst time: a 0 3
Enter process name, arrival time and burst time: b 2 6
Enter process name, arrival time and burst time: c 4 4
Enter process name, arrival time and burst time: d 6 5
Enter process name, arrival time and burst time: e 8 2

** FCFS **
Process Arrival time Burst time Finish time Turn Norm. TAT
name around time
A 0 3 3 3 0
B 2 6 9 7 1
C 4 4 13 9 5
D 6 5 18 12 7
E 8 2 20 12 10

Average waiting time = 4.600000

HT NO: 08E21A0594 Page 18


B.TECH 3-2 SEM (2010-2011)

Experiment number: 4
PRIORITY SCHEDULING

Aim: To study the round robin 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 is the inverse of the next CPU burst, the lower the priority, and the
vice-versa. Priorities are generally some fixed range of numbers, such as 0 to
7, or 0 to 4,095.
Priority scheduling can be either primitive or non-primitive. When a
process arrives at the ready queue, its priority is compared with the priority of
currently running process. A primitive priority scheduling algorithm will
preempt the CPU if the priority of the newly arrived process is higher than the
priority of the currently running process. A no primitive priority scheduling will
simply but the new process at the head of the ready queue.

ALGORITHM:
1. Create a structure to represent process details.
2. Get the number of process as input from the user.
3. Get the input for the process attributes, which are process name, arrival
time, service time and priority.
4. Sort the list nodes based on its priority if the process are same then sort
it based on its arrival time.
5. Calculate the other process attributes such as TAT, average TAT,
waiting time, average waiting time, normalized TAT.
6. Formula for waiting time is, starting time – arrival time.
7. Formula for calculating TAT,
(Finish time – starting time) + waiting time
8. Formula for normalized TAT= (TAT) / (service time).
9. The finish time of a process is nothing but the sum of service time of
first process and finish time of the previous process. Also, the finish
time of the first process is its service time or finish time of the process is
starting time of this process + service time of the process.

HT NO: 08E21A0594 Page 19


B.TECH 3-2 SEM (2010-2011)

10. Starting time of second process would be finish time of the first
process or arrival time of second process, whichever is bigger.
11. Calculate other process attributes which are mentioned above.
12. Repeat these steps for each and every process.
13. Print the results.

SAMPLE RUN:

Required input: process name, arrival time, service time, time slice

Expected output: process name, arrival time, service time, turn around
time, waiting time.

Example output:

Process Service Waiting


TAT Priority Arrival time
name time time

A 3 5 4 2 0

B 4 5 3 3 0

C 5 6 3 5 0

Average turn around time= 8.666667

Average waiting time= 4.0000000

HT NO: 08E21A0594 Page 20


B.TECH 3-2 SEM (2010-2011)

Program to implement PRIORITY SCHEDULING algorithm

/*Program to implement priority scheduling algorithm*/

#include<stdio.h>

#include<conio.h>

struct process

char name[20];

int at,bt,wt,tat,ft,pt;

}Q[20],temp;

void main()

int f,r,i,j,k,l,n;

float awt=0;

clrscr();

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

scanf("%d",&n);

for(r=0;r<n;r++)

printf("\n enter process name,priority,arrival time,burst time:");

scanf("%s%d%d%d",&Q[r].name,&Q[r].pt,&Q[r].at,&Q[r].bt);

Q[r].wt=0;

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

HT NO: 08E21A0594 Page 21


B.TECH 3-2 SEM (2010-2011)

if(Q[i].at>Q[j].at)

temp=Q[i];

Q[i]=Q[j];

Q[j]=temp;

printf("\n\n\n\t\t PRIORITY ALGORITHM");

printf("\n process name,priority,arrival time,burst time");

printf(" waiting time,turn around time");

for(f=0;f<n;f++)

if(f==0)

Q[f].ft=Q[f].bt+Q[f].at;

else

Q[f].ft=Q[f-1].ft+Q[f].bt;

for(j=f+1;j<n;j++)

if(Q[f].ft>=Q[j].at)

l=j;

for(k=j+1;k<n;k++)

if(Q[f].ft>=Q[k].at && Q[l].pt>=Q[k].pt)

l=k;

HT NO: 08E21A0594 Page 22


B.TECH 3-2 SEM (2010-2011)

temp=Q[l];

Q[l]=Q[j];

Q[j]=temp;

Q[f].tat=Q[f].ft-Q[f].at;

if(f==0)

Q[f].wt=0;

else

Q[f].wt=Q[f-1].ft-Q[f].at;

if(Q[f].wt<0)

Q[f].wt=0;

awt=awt+Q[f].wt;

printf("\n%-14s%-11d",Q[f].name,Q[f].at);

printf("%-11d%-15d%-8d%d",Q[f].at,Q[f].bt,Q[f].wt,Q[f].tat);

awt=awt/n;

printf("\n\n\t average waiting time =%5.4f",awt);

getch();

HT NO: 08E21A0594 Page 23


B.TECH 3-2 SEM (2010-2011)

Output:
Enter number of processes: 5

Enter process name, priority, arrival time, and burst time:


A104
Enter process name, priority, arrival time, and burst time:
B426
Enter process name, priority, arrival time, and burst time:
C344
Enter process name, priority, arrival time, and burst time:
D356
Enter process name, priority, arrival time, and burst time:
E245

Waiting
Name Priority Arrival time Burst time TAT
time
A 1 0 4 0 4
B 4 2 6 0 5
C 3 4 4 5 9
D 3 5 6 8 14
E 2 4 6 17 23

Average waiting time = 6.0000

HT NO: 08E21A0594 Page 24


B.TECH 3-2 SEM (2010-2011)

Experiment number: 5

FIRST IN FIRST OUT


Aim: To study the first in first out page replacement algorithm.

A FIFO replacement algorithm associates with each page the time


when that page was brought into memory. When a page must be replaced,
the oldest page is chosen. We can create a FIFO queue to hold all pages in
memory. We replace the page at the head of the queue. When the page is
brought into memory, we insert it at the tail of the queue.

Algorithm:

1. Create a structure to represent page details to indicate the maximum


number of pages. Declare 2 pointer objects.
2. The head pointer object is used to mark starting node and the list
pointer object I used for adding nodes.
3. The page details are page number and then a link to the next node.
4. Create a node using malloc function.
5. Then pass is this node to a function whose purpose is to create nodes.
6. With this function increment the counter and then get the next page
number and store in it. If that page number is present in the list then set
a flag and return to main program. In the main program if the flag is set
then call the create function with the current node itself.
7. The counter indicates the number of pages.
8. Have a terminating condition on page number.
9. If user enters page number apart from terminating number, then user
wishes to add another page. So in this case compare the counter with a
value. This value is the maximum number of the pages that can be
stored.
10. If the counter value is greater than max. number of pages then move
the head to the next page which means the page that first entered has
been taken out first.
11. Then create another node and call the same program with this newly
created node as an argument.
12. Here nodes are added in the tail of linked list and nodes are removed
by changing the position of head to the next location.

HT NO: 08E21A0594 Page 25


B.TECH 3-2 SEM (2010-2011)

13. Once user enters terminating page number it returns to main program.
14. Now call another function whose function is to print the nodes with
head as argument.
15. In this function print the page detail that is stored in the linked list and
return to the main function.

SAMPLE RUN:

Required input: max. Number of pages, page numbers, terminating


page number.
Expected output: page numbers.

Example output:
N=5

Enter page number: 1


Enter page number: 9
Enter page number: 3
Enter page number: 4
Enter page number: 1
Enter page number: 5
Enter page number: 7
Enter page number: 6
Enter page number: 8
Enter page number: -999

Pages in memory:
5
7
6

HT NO: 08E21A0594 Page 26


B.TECH 3-2 SEM (2010-2011)

PROGRAM TO IMPLEMENT FIFO

/* program to implement page replacement using FIFO algorithm*/


#include<stdio.h>
#include<conio.h>
int re[30],p[10],i,j,n,nr,line=6,c=0,a1=0;
void main()
{
clrscr();
printf(" enter length of reference string:");
scanf("%d",&nr);
printf(" enter reference string:");
for(i=1;i<=nr;i++)
{
scanf("%d",&re[i]);
}
printf(" \n enter number of frames:");
fflush(stdin);
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i]=-1;
for(i=1,j=1;i<=nr;i++)
{
a1=0;
if(j>n)
j=1;
for(c=1;c<=n;c++)
if(re[i]==p[c])
a1++;
if(a1==0)
{
p[j]=re[i];
j++;
}
display(n,p,i);
}

HT NO: 08E21A0594 Page 27


B.TECH 3-2 SEM (2010-2011)

printf("\n");
getch();
}

display(int no,int p[],int i)


{
int k;
if(i==1)
{
printf("\t\t\t");
for(k=1;k<=no;k++)
printf("_ _");
}
printf("\n%8d",re[i]);
gotoxy(25,line++);
for(k=1;k<=no;k++)
{
printf("|");
printf("_");
if(p[k]!=-1)
printf("%d",p[k]);
else
printf(" ");
printf("_");
}
printf("|");
}

HT NO: 08E21A0594 Page 28


B.TECH 3-2 SEM (2010-2011)

Output:

Enter length of reference string: 12


Enter reference string: 2 3 2 1 5 2 4 5 3 2 5 2
Enter number of frames: 3

2 2
3 2 3
2 2 3
1 2 3 1
5 5 3 1
2 5 2 1
4 5 2 4
5 5 2 4
3 3 2 4
2 3 2 4
5 3 5 4
2 3 5 2

HT NO: 08E21A0594 Page 29


B.TECH 3-2 SEM (2010-2011)

Experiment number: 6

LEAST RECENTLY USED

Aim: To study the least recently used page replacement algorithm.

LRU replacement algorithm associates with each page the time of


that page’s last use. When a page must be replaced, LRU chooses that
page that has not been used for the longest period of time. This strategy is
the optimal page replacement algorithm looking backward in time, rather
than forward.

ALGORITHM:
1. Create a structure to represent page details.
2. Every node should contain page number, link and also a counter in it
to represent how recently it has used.
3. Declare another counter globally to indicate the maximum number of
the pages. Declare 2 pointer objects.
4. The head pointer is used to mark starting node and its list pointer
object is used for adding nodes.
5. Create a node using a malloc function. As each node is added its
counter value is incremented by 1.
6. Then pass this node to a function whose purpose is to create
nodded.
7. Within this function, increment the global counter and then get the
page number and if the page number already exists then increment
the counter for that node and call the create function with the current
node as arguments.
8. Have a terminating condition on page number.
9. If user enters page number apart from terminating number, then user
wishes to add another page. So in this case compare the global
counter with a value. This value is the max. Number of pages that
can be stored.
10. If the global counter value is greater than the max. Number of
pages then the node which has been not used for longest time
should be removed.

HT NO: 08E21A0594 Page 30


B.TECH 3-2 SEM (2010-2011)

11. To find the node with minimum counter value call a function to find
the node which has minimum counter value. This node is deleted
from the list which means the page which is least recently used has
been taken out first.
12. Then create another node and call the same program with this
newly created node as an argument.
13. Here nodes are added in the tail of linked list and nodes are
removed based on their counter value.
14. Once user enters -999 it returns to main program.
15. Now call another function whose function is to print the nodes with
head as argument.
16. In this function print the page details that are stored in the linked list
and return to the main function.

SAMPLE RUN:
Required input: max. Number of pages, pages numbers, terminating
page number.
Expected output: page numbers.

Example output:
N=5

Enter page number: 1 pages in memory:


Enter page number: 2
Enter page number: 3 5
Enter page number: 4 7
Enter page number: 1 6
Enter page number: 5 8
Enter page number: 7 9
Enter page number: 6
Enter page number: 8
Enter page number: 9
Enter page number: -999

HT NO: 08E21A0594 Page 31


B.TECH 3-2 SEM (2010-2011)

Program to implement LRU

/* program to implement page replacement using LRU algorithm*/


#include<stdio.h>
int i,j=1,k,l,re[30],p[10],ch,no,nr,c,al=0,a,line=6;
struct re
{
int st,l,ps;
}opr;
void main()
{
clrscr();
printf("enter the length of the reference string:");
scanf("%d",&nr);
printf("enter the reference string:");
for(i=1;i<=nr;i++)
scanf("%d",&re[i]);
printf("\n enter the number of frames:");
scanf("%d",&no);
clrscr();
for(i=1;i<=no;i++)
p[i]=-1;
opr.st=0;
for(i=1;i<=nr;i++)
{
al=0;
opr.st=100;
for(c=1;c<=no;c++)
if(re[i]==p[c])
al++;
if(al==0)
{
if(j<=no)
{

HT NO: 08E21A0594 Page 32


B.TECH 3-2 SEM (2010-2011)

p[j]=re[i];
j++;
}
else
{
for(k=1;k<=no;k++)
{
for(ch=i-1;ch>=1;ch--)
{
a=0;
if(p[k]==re[ch])
{
a++;
break;
}
}
if(a!=0)
{
if(opr.st>ch)
{
opr.st=ch;
opr.l=re[ch];
opr.ps=k;
}
}
else if(a==0)
{
opr.ps=k;
break;
}
}
p[(opr.ps)]=re[i];
}
}
display(no,p,i);
}
printf("\n");

HT NO: 08E21A0594 Page 33


B.TECH 3-2 SEM (2010-2011)

getch();
}

display(int no,int p[],int i)


{
int k;
if(i==1)
{
printf("\t\t\t");
for(k=1;k<=no;k++)
printf("__");
}
printf("\n%d",re[i]);
gotoxy(25,line++);
for(k=1;k<=no;k++)
{
printf("|");
printf("_");
if(p[k]!=-1)
printf("%d",p[k]);
else
printf(" ");
printf("_");
}
printf("|");
}

HT NO: 08E21A0594 Page 34


B.TECH 3-2 SEM (2010-2011)

Output:

Enter length of reference string: 12


Enter reference string: 2 3 2 1 5 2 4 5 3 2 5 2
Enter number of frames: 3

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

HT NO: 08E21A0594 Page 35


B.TECH 3-2 SEM (2010-2011)

Experiment number: 7
LEAST FREQUENTLY USED

Aim: to study the least frequently used page replacement algorithm.

Least frequently used page replacement algorithm requires that the


page with the smallest count be replaced. The reason for this selection is that
an actively used page should have a large refrence count. This algorithm
suffers from the situation in which a page is used heavily during the initial
phase of a process, but then is never again. Since it was used heavily, it has
a large count and remains in memory ever through it are no longer needed.
One solution is to shift the counts right by 1 bit at regular intervals, forming an
exponentially decaying usage count.

ALGORITHM:
1. Create a structure to represent the page details.
2. Every node should contain page number, link and also a counter in it
to represent how frequently it has been used.
3. Declare another counter globally to indicate the maximum number of
pages. Declare two pointer objects.
4. The head pointer object is used to mark starting node and the least
pointer object is used for adding nodes.
5. Create a node using malloc function. As each node page is added its
counter value is incremented by 1.
6. Then pass this node to function whose purpose is to create nodes.
7. Within this function increment the global counter and then get the
page number and if the page number already exists then increment
the counter for that node and call the create function with the current
node as arguments.
A page can be found existing or not existing by calling
another function with arguments as a head and page number. If that
the main program if the flag set then call the crate function with the
current node itself.

HT NO: 08E21A0594 Page 36


B.TECH 3-2 SEM (2010-2011)

8. Have a terminating condition on page number. Suppose user enters -


999 as page numbers then return to main program which means
creation of nodes are over.

9. If the user enters page number apart from the terminating number,
then the user wishes to add another page. So in this case compare
the global counter with a value. This value is maximum number of
pages that can be stored.

10. If the global counter value is greater than the maximum number of
pages then the node which is least frequently used should be
removed i.e. the node with a minimum counter value should be
removed.

11. To find the node with a minimum counter value calls a function to
find the node which has the minimum counter value? This node is
deleted from the list which means the page which is least frequently
used is has been taken out first.

12. Then create another node and call the same program with this
newly created node as an argument.

13. Here nodes are added in the tail of linked list and nodes are
removed based on their counter value( i.e. page which is least
frequently used).

14. Once user enters -999(terminating page number) it returns to main


program.

15. Now call another function whose function is to print the nodes with
head as argument.

16. In this function print the page details and return to main function.

HT NO: 08E21A0594 Page 37


B.TECH 3-2 SEM (2010-2011)

SAMPLE RUN:

REQUIRED INPUT: maximum number of pages, page numbers,


terminating page number.
Expected output: page number.

Example output:

N=3
Enter page number: 0
Enter page number: 1
Enter page number: 2
Enter page number: 3
Enter page number: 0
Enter page number: 1
Enter page number: 2
Enter page number: 3
Enter page number: 0
Enter page number: 1
Enter page number: 2
Enter page number: 3
Enter page number: 4
Enter page number: 5
Enter page number: 6
Enter page number: 7
Enter page number: -999

Pages in memory:
3
1
7
Number of page faults after frame allocation has filled = 9
Frame allocation size = 3
Total page faults = 12

HT NO: 08E21A0594 Page 38


B.TECH 3-2 SEM (2010-2011)

Program to implement LFU

/*program to implement LFU*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,fz,i,j,k,fl,fll,c,m;
int p[50];
int f[10];
int pr[10]={0};
clrscr();
printf("enter how many no of processes :");
scanf("%d",&n);
printf("enter %d process memory locations :\n",n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter frame size:");
scanf("%d",&fz);
/*placing into the frame*/
for(i=0;i<fz;i++)
f[i]=-1;
for(i=0;i<n;i++)
{
for(k=0;k<fz;k++)
pr[k]=0;
fl=0;fll=0;
for(j=0;j<fz;j++)
if(f[j]==p[i])
{
fl=1;
break;
}
if(fl==0)
{
for(j=0;j<fz;j++)

HT NO: 08E21A0594 Page 39


B.TECH 3-2 SEM (2010-2011)

{
for(c=0,k=-1;k>=0;k--)
if(f[j]==p[k])
break;
else
c++;
pr[j]=c;
}
m=pr[0];
j=0;
for(k=0;k<fz;k++)
if(m<pr[k])
{
m=pr[k];
j=k;
}
for(k=0;k<fz;k++)
if(f[k]<0)
{
j=k;
break;
}
f[j]=p[i];
fll=1;
}
printf("\n");
if(fll==1)
{
for(k=0;k<fz;k++)
if(f[k]>=0)
printf("%6d",f[k]);
}
else
printf("\n\nno page fault");
}
getch();
}

HT NO: 08E21A0594 Page 40


B.TECH 3-2 SEM (2010-2011)

Output:

Enter length of reference string: 12


Enter reference string: 2 3 2 1 5 2 4 5 3 2 5 2
Enter number of frames: 3

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

HT NO: 08E21A0594 Page 41


B.TECH 3-2 SEM (2010-2011)

Experiment number: 8

BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE

Aim: To study the banker’s algorithm for deadlock avoidance.

This is an algorithm for the deadlock avoidance. As the name


itself this algorithm could be used in a banking to ensure that the bank never
allocates its available cash such that it can no longer satisfy the needs of all
its customers.
When a new process enters the system, it must declare the
maximum number of instances of each resource type that it may need. This
number may not exceed the total number of resources in the system. When a
user requests a set of resources, the system must determine whether the
allocation of these resources will leave the system in the safe state. If it will,
the resources are allocated; otherwise, the process must wait until some
other process releases enough resources.

Algorithm:
1. Define one dimensional arrays to represent resources on the system
and available resources.
2. Define two dimensional arrays to represent allocation of resources
and process claim for each class of resources i.e. a[i,j] represents
allocated jth class resource for ith process. Similarly, c[i,j] represents
ith process claim for jth class resource.
3. Prompt for the number of processes.
4. Prompt the different class of resources.
5. Read the allocation matrix and decrease the available resources
accordingly.
6. Read the claim matrix that represents type of resources and number
of each type of resources required for that process to complete its
execution.
7. Print the allocation matrix, claim matrix, available array and
resources array.

HT NO: 08E21A0594 Page 42


B.TECH 3-2 SEM (2010-2011)

8. Prompt for the extra resources, if yes do the steps


a) Prompt for the process which wants the resource.
b) Get the newly requested class and number of resources.
c) Check for the following conditions.
i. All resources are either available or allocated.
ii. No process can claim more than the total amount of
resources on the system.
iii. No process can be assigned with more than the
resources initially it is claimed.

If any of the above conditions fails print “ERROR”.

d) Call the subroutine SAFE, if it returns the safe mode carry out
the allocation. Else restore the original state, suspend the
process.

FUNCTION SAFE:
1. Current availability = Availability
2. Process rest = { number of processes }
3. Rest = { all processes }
4. Possible = true
5. While(possible)
{
Find the process in rest such that the process (claim –
allocation) is less than or equal to currently available. If such
process is found
i. Assign the resources to that process so that, that
process can complete its execution.
ii. Once the execution is over, relieve all the resources from
that process and add to the current availability.
}
Otherwise, possible is false.
6. Return NULL.

HT NO: 08E21A0594 Page 43


B.TECH 3-2 SEM (2010-2011)

Program to implement banker’s deadlock avoidance algorithm

/* program to implement banker's deadlock avoidance algorithm*/


#include<stdio.h>
#include<conio.h>
void 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;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("Enter the no of processes:\n");
scanf("%d",&p);
printf("Enter the no of resources:\n");
scanf("%d",&r);
printf("Enter the claim for each process:");
for(i=1;i<=p;i++)
{
printf("\nFor process %d:",i);
for(j=1;j<=r;j++)
{
scanf("%d",&clm[i][j]);
}
}
printf("Enter the allocation for each process:\n");
for(i=1;i<=p;i++)
{
printf("\nFor process: ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&alloc[i][j]);
}
}

HT NO: 08E21A0594 Page 44


B.TECH 3-2 SEM (2010-2011)

printf("Enter total no of each resource:");


for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=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];
}
}
printf("\n\nAvailable resorces is:");
for(j=1;j<=r;j++)
{
printf(" ",avail[j]);
}
printf("\nClaim matrix:\t\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{

HT NO: 08E21A0594 Page 45


B.TECH 3-2 SEM (2010-2011)

printf("%d",alloc[i][j]);
}
printf("\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])
{
prc=0;
break;
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\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)
printf("\nThe system is in a safe state!!");

HT NO: 08E21A0594 Page 46


B.TECH 3-2 SEM (2010-2011)

else
printf("\nThe system is in an unsafe state!!");
getch();
}

OUT PUT:

Enter the no of processes:


2
Enter the no of resources:
3

Enter the claim for each process:

For process I : 2 4 5

For process II: 2 5 3

Enter the total no of each resource : 5 5 2

Available resource is :

Claim matrix: allocation matrix:


245 123
253 234

The system is in an unsafe state!!

HT NO: 08E21A0594 Page 47

You might also like