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

OS Excuted Program

The document is an Operating System Manual from the Royal Institute of Technology & Science that outlines various CPU scheduling algorithms, file allocation strategies, and memory management techniques. It includes simulations for algorithms such as FCFS, SJF, priority scheduling, and Round Robin, as well as file allocation methods like sequential, indexed, and linked strategies. Additionally, it covers concepts like CPU utilization, turnaround time, and deadlock avoidance using the Banker's Algorithm.

Uploaded by

jwr.786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

OS Excuted Program

The document is an Operating System Manual from the Royal Institute of Technology & Science that outlines various CPU scheduling algorithms, file allocation strategies, and memory management techniques. It includes simulations for algorithms such as FCFS, SJF, priority scheduling, and Round Robin, as well as file allocation methods like sequential, indexed, and linked strategies. Additionally, it covers concepts like CPU utilization, turnaround time, and deadlock avoidance using the Banker's Algorithm.

Uploaded by

jwr.786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Operating System Manual Royal Institute Of Technology & Science

Contents

1. [Aim:] Simulate the following CPU scheduling algorithms........................................1


Simulate the FCFS cpu scheduling algorithm............................................................3
Simulate the Shortest Job First( SJF) cpu scheduling algorithm ...............................9
Simulate the priority CPU Sheduling Alogrithm ....................................................13
Simulate the ROUNDROBIN CPU scheduling algorithm .........................................17
2. [Aim:] Simulate all file allocation strategies.............................................................21
Simulate the sequential file allocation strategy ....................................................22
Simulate indexed file allocation strategy ..............................................................26
Simulate the Linked file allocation strategy ..........................................................31
Simulation of Multi-programming with variable number of tasks (MVT):..............34
Simulation of Multi-programming with Fixed number of tasks (MFT):..................37
3. [Aim:] Simulate Bankers Algorithm for Deadlock Avoidance..................................40
4. [Aim:] Simulate paging technique of memory management.....................................45
5. [Aim:] Simulate all page replacement algorithms.....................................................47
implement the FIFO page replacement algorithm .................................................47
Implementation of LRU page replacement algorithm ............................................50
Implement the LFU PAGE REPLACEMENT ALGORITHM .........................................53

1
Operating System Manual Royal Institute Of Technology & Science
PART – B [Operating System]

1. [Aim:] Simulate the following CPU scheduling algorithms


a) Round Robin
b) SJF
c) FCFS
d) Priority

Theory: Scheduling is a fundamental operating system function.

CPU scheduling is the basis of multi programming operating system. CPU scheduling
algorithm determines how the CPU will be allocated to the process. These are of two types.

1. Primitive scheduling algorithms


2. Non-Primitive scheduling algorithms

1) Primitive Scheduling algorithms: In this, the CPU can release the process even in the middle of
execution. For example: the cpu executes the process p1, in the middle of execution the cpu received a
request signal from process p2, then the OS compares the priorities of p1&p2. If the priority p1 is
higher than the p2 then the cpu continue the execution of process p1. Otherwise the cpu preempt the
process p1 and assigned to process p2.

2) Non-Primitive Scheduling algorithm: In this, once the cpu assigned to a process the processor
do not release until the completion of that process. The cou will assign to some other job only after
the previous job has finished.
Scheduling methodology:

Though put: It means how many jobs are completed by the CPU with in a time period.

Turn around time: The time interval between the submission of the process and the time of the
completion is the turn around time.

Turn around time=Finished time – arrival time

Waiting time: it is the sum of the periods spent waiting by a process in the ready queue

Waiting time=Starting time- arrival time

Response time: it is the time duration between the submission and first response

Response time=First response-arrival time

CPU Utilization: This is the percentage of time that the processor is busy. CPU utilization may
range from 0 to 100%

2
Operating System Manual Royal Institute Of Technology & Science
First-come, first-serve scheduling(FCFS):
In this, which process enter the ready queue first is served first. The OS maintains DS that is
ready queue. It is the simplest CPU scheduling algorithm. If a process request the CPU then it is
loaded into the ready queue, which process is the head of the ready queue, connect the CPU to that
process.

Shortest job First:

The criteria of this algorithm are which process having the smallest CPU burst, CPU is
assigned to that next process. If two process having the same CPU burst time FCFS is used to break
the tie.

Priority Scheduling:

These are of two types.

One is internal priority, second is external priority. The cpu is allocated to the process with the
highest priority. Equal priority processes are scheduled in the FCFS order. Priorities are generally
some fixed range of numbers such as 0 to 409. The low numbers represent high priority

Round Robin: It is a primitive scheduling algorithm it is designed especially for time sharing systems.
In this, the CPU switches between the processes. When the time quantum expired, the CPU switches
to another job. A small unit of time called a quantum or time slice. A time quantum is generally is a
circular queue new processes are added to the tail of the ready queue.

If the process may have a CPU burst of less than one time slice then the process release
the CPU voluntarily. The scheduler will then process to next process ready queue otherwise; the
process will be put at the tail of the ready queue.

3
Operating System Manual Royal Institute Of Technology & Science
/*Simulate the FCFS cpu scheduling algorithm*/

#include<stdio.h>

#include<conio.h>

struct fcfs

int num,at,bt,wt;

};

struct fcfs a[20],temp;

int i,n,j,t;

float sum1=0,sum2=0;

void main()

void read();

void sort();

void process();

void print();

clrscr();

read();

sort();

process();

print();

getch();

void read()

4
Operating System Manual Royal Institute Of Technology & Science
printf(" ENTER NO. OF PROCESS DO YOU WANT.....\n");

scanf("%d",&n);

printf("\n\tENTER ARRIVAL TIME & BUST TIME\n");

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

a[i].num=i;

printf("\nJOB(%d)",i);

scanf("%d%d",&a[i].at,&a[i].bt);

void sort()

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

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

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

temp=a[i]; a[i]=a[j]; a[j]=temp;

printf("\n\tARRAVIAL TIME & BURST TIME\n");

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

5
Operating System Manual Royal Institute Of Technology & Science
printf("\njobs[%d]",a[i].num);

printf("\t%d\t%d",a[i].at,a[i].bt);

//printf("\n");

printf("\n");

void process()

printf("\n The gantt is.....");

printf("\n");

for(i=1;i<=(n*10);i++)

printf("-");

printf("\n");

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

//printf("\nJOB %d\t\n",a[i].num);

printf("JOB %d\t",a[i].num);

printf("\n");

for(i=1;i<=(n*10);i++)

printf("-");

printf("\n");

6
Operating System Manual Royal Institute Of Technology & Science
for(i=1 ;i<=n;i++)

a[i].wt=t-a[i].at;

printf("%d\t",t);

t=t+a[i].bt;

printf("%d\n\t",t);

void print()

int i;

printf("\n\nJobs waiting time\n");

printf("\n-------------------------------\n");

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

printf("\n Job[%d]\t%d\t%d\n",a[i].num,a[i].wt,a[i].wt+a[i].bt);

sum1+=a[i].wt;

sum2+=a[i].wt+a[i].bt;

printf("avg wt=%f\n",sum1/n);

printf("avg t.at=%f\n",sum2/n);

7
Operating System Manual Royal Institute Of Technology & Science

8
Operating System Manual Royal Institute Of Technology & Science
/*Simulate the Shortest Job First( SJF) cpu scheduling algorithm */

#include<stdio.h>

#include<conio.h>

struct sjf

int num,bt,wt;

};

struct sjf a[20],temp;

int i,j,n;

float sum1=0,sum2=0;

void main()

void read();

void sort();

void process();

void print();

clrscr();

read();

sort();

process();

print();

void read()

printf("enter no of jobs\n");

9
Operating System Manual Royal Institute Of Technology & Science
scanf("%d",&n);

printf("enter arrival time burst time\n");

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

printf("job(%d)",i);

scanf("%d",&a[i].bt);

a[i].num=i;

void sort()

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

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

if(a[i].bt>a[j].bt)

temp=a[i];

a[i]=a[j];

a[j]=temp;

printf("jobs burst_time\n");

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

10
Operating System Manual Royal Institute Of Technology & Science
printf("\njob[%d]\t",a[i].num);

printf("\t%d\n",a[i].bt);

void process()

int t=0;

printf("the gnatt is\n");

for(i=1;i<(n*10);i++)

printf("-");

printf("\n");

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

printf("job %d\t",a[i].num);

printf("\n");

for(i=1;i<=(n*10);i++)

printf("-");

printf("\n");

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

a[i].wt=t;

printf("%d\t",t);

t=t+a[i].bt;

printf("%d\n",t);

void print()

11
Operating System Manual Royal Institute Of Technology & Science
{

int i;

printf("jobs waiting_time t.a.t\n");

printf("---- ------- ----");

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

printf("\n job[%d]\t%d\t%d",a[i].num,a[i].wt,a[i].wt+a[i].bt);

sum1+=a[i].wt;

sum2+=a[i].wt+a[i].bt;

printf("avg wt=%f\n",sum1/n);

printf("avg t.a.t=%f\n",sum2/n);

12
Operating System Manual Royal Institute Of Technology & Science
/* Simulate the priority CPU Sheduling Alogrithm */

#include<stdio.h>

#include<conio.h>

struct priority

int num,at,bt,wt,pt;

};

struct priority a[20],temp;

int i,j,n;

float sum1=0,sum2=0;

void main()

void read();

void sort();

void process();

void print();

clrscr();

read();

sort();

process();

print();

void read()

printf("enter no.of jobs\n");

scanf("%d",&n);

13
Operating System Manual Royal Institute Of Technology & Science
printf("enter priority burst time\n");

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

printf("job(%d)",i);

scanf("%d%d",&a[i].pt,&a[i].bt);

a[i].num=i;

void sort()

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

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

if(a[i].pt>a[j].pt)

temp=a[i]; a[i]=a[j]; a[j]=temp;

printf("jobs priority burst_time\n");

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

printf("\njob[%d]\t",a[i].num); printf("\t%d\t%d\n",a[i].pt,a[i].bt);

14
Operating System Manual Royal Institute Of Technology & Science
void process()

int t=0;

printf("the gantt is\n");

for(i=1;i<(n*10);i++)

printf("-");

printf("\n");

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

printf("\job%d\t",a[i].num);

printf("\n");

for(i=1;i<(n*10);i++)

printf("-");

printf("\n");

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

a[i].wt=t;

printf("%d\t",t);

t=t+a[i].bt;

printf("%d\n",t);

void print()

int i;

printf("jobs waiting time t.a.t\n");

printf("_____ _______ _____");

15
Operating System Manual Royal Institute Of Technology & Science
for(i=1;i<=n;i++)

printf("\njob[%d]\t%d\t%d",a[i].num,a[i].wt,a[i].wt+a[i].bt);

sum1+=a[i].wt;

sum2+=a[i].wt+a[i].bt;

printf("avg w.t=%f\n",(float)sum1/n); printf("avg t.a.t =%f\n",(float)sum2/n);

16
Operating System Manual Royal Institute Of Technology & Science
/*Simulate the ROUNDROBIN CPU scheduling algorithm */

#include<stdio.h>

int ttime,i,j,temp;

main()

int pname[10],btime[10],pname2[10],btime2[10];

int n,x,z;

printf("Enter the no. of process:");

scanf("%d",&n);

printf("Enter the process name and burst time for the process\n");

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

printf("Enter the process name:");

scanf("%d",&pname2[i]);

printf("Enter burst time for the process %d:",pname2[i]);

scanf("%d",&btime2[i]);

printf("PROCESS NAME \t\t BURST TIME\n");

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

printf("%d\t\t\t %d\n",pname2[i],btime2[i]);

z=1;

while(z==1)

ttime=0;

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

17
Operating System Manual Royal Institute Of Technology & Science
pname[i]=pname2[i];

btime[i]=btime2[i];

printf ("PRESS 1.ROUND ROBIN 2.EXIT\n");

scanf("%d",&x);

switch(x)

case 1:

rrobin(pname,btime,n);

break;

case 2:

exit(0);

break;

default:

printf("Invalid option");

break;

printf("\n\n If you want to continue press 1:");

scanf("%d",&z);

rrobin(int pname[],int btime[],int n)

int tslice;

18
Operating System Manual Royal Institute Of Technology & Science
j=0;

printf("\n\t ROUND ROBIN SCHEDULING \n\n");

printf("Enter the time slice:\n");

scanf("%d",&tslice);

printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");

while(j<n)

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

if(btime[i]>0)

if(btime[i]>=tslice)

ttime+=tslice;

btime[i]=btime[i]-tslice;

printf("\n%d\t\t %d \t\t
%d",pname[i],btime[i],ttime);

if(btime[i]==0)

j++;

else

ttime+=btime[i];

btime[i]=0;

19
Operating System Manual Royal Institute Of Technology & Science
printf("\n%d\t\t %d \t\t
%d",pname[i],btime[i],ttime);

20
Operating System Manual Royal Institute Of Technology & Science
2. [Aim:] Simulate all file allocation strategies
a) Sequential
b) Indexed
c) Linked

Theory:

Files are normally stored on the disks. So the main problem is how to allocate space to those
files. So that disk space is utilized effectively and files can be accessed quickly. Three major
strategies of allocating disc space are in wide use. Sequential, indexed and linked.

a) Sequential allocation :

In this allocation strategy, each file occupies a set of contiguously blocks on the disk. This
strategy is best suited. For sequential files. The file allocation table consists of a single entry for each
file. It shows the filenames, staring block of the file and size of the file. The main problem of this
strategy is, it is difficult to find the contiguous free blocks in the disk and some free blocks could
happen between two files.

b) Indexed allocation :

Indexed allocation supports both sequential and direct access files. Te file indexes are not
physically stored as a part of the file allocation table. Whenever the file size increases, we can easily
add some more blocks to the index. In this strategy, the file allocation table contains a single entry
for each file. The entry consisting of one index block, the index blocks having the pointers to the
other blocks. No external fragmentation.

c) Linked allocation :

It is easy to allocate the files, because allocation is on an individual block basis. Each block
contains a pointer to the next free block in the chain. Here also the file allocation table consisting of a
single entry for each file. Using this strategy any free block can be added to a chain very easily.
There is a link between one block to another block, that’s why it is said to be linked allocation. We
can avoid the external fragmentation.

21
Operating System Manual Royal Institute Of Technology & Science
/* Simulate the sequential file allocation strategy */

#include<stdio.h>

#include<conio.h>

#include<process.h>

struct sequence

char n[20];

int i;

s[20];

int create(int);

int del(int);

void display(int);

void main()

int x=0,j=0;

clrscr();

while(1)

printf("1.creation\n2.delete\n3.display\n4.exit");

printf("\nenter one option");

scanf("%d",&x);

switch(x)

case 1: j=create(j);

22
Operating System Manual Royal Institute Of Technology & Science
break;

case 2: j=del(j);

break;

case 3: display(j);

break;

case 4: exit(1);

default :printf("wrong option");

int create(int j)

int m,v;

j++;

w:printf("\nenter the file name:");

scanf("%s",&s[j].n);

m=1;

while(m<j)

v=strcmp(s[j].n,s[m].n); if(v==0)

printf("file is already exist\nplease enter another name");

goto w;

m++;

23
Operating System Manual Royal Institute Of Technology & Science
printf("\nenter field:");

scanf("%d",&s[j].i);

return(j);

int del(int j)

j--;

return(j);

void display(int j)

int l;

printf("filename\tfield");

for(l=1;l<=j;l++)

printf("\n%s\t\t%d\n",s[l].n,s[l].i);

24
Operating System Manual Royal Institute Of Technology & Science

25
Operating System Manual Royal Institute Of Technology & Science
/* Simulate indexed file allocation strategy */

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct file

char n[20];

int fld,ind;

s[20];

int no,i=-1,a,b,f,j=-1,fe,t;

char tem[20];

void create();

void display();

void del();

void main()

clrscr();

while(1)

printf("\n\nmenu");

printf("\n1.create\n2.display\n3.delete\n4.exit");

printf("enter ur choice:");

scanf("%d",&no);

switch(no)

26
Operating System Manual Royal Institute Of Technology & Science
{

case 1: create();

break;

case 2: display();

break;

case 3: del();

break;

case 4: exit(0);

default: printf("wrong choice");

void create()

i++;

printf("\nenter the name of the recoed:");

scanf("%s",&s[i].n);

printf("\nenter the index no:");

scanf("%d",&s[i].ind);

printf("\nenter the field no:");

scanf("%d",&s[i].fld);

j++;

void display()

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

27
Operating System Manual Royal Institute Of Technology & Science
{

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

if(s[b].ind>s[b+1].ind)

t=s[b].ind;

s[b].ind=s[b+1].ind;

s[b+1].ind=t;

strcpy(tem,s[b].n);

strcpy(s[b].n,s[b+1].n);

strcpy(s[b+1].n,tem);

t=s[b].fld;

s[b].fld=s[b+1].fld;

s[b+1].fld=t;

else

continue;

printf("\n ---------------------------------");

printf("\n\t Index Recordname FieldNo");

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

printf("\n\t%d\t",s[i].ind);

printf("\t%s",s[i].n);

printf("\t%d",s[i].fld);

28
Operating System Manual Royal Institute Of Technology & Science
}

i--;

printf("\n -----------------------------------\n");

void del()

int de,index=-1,k=0,l;

if(i!=-1)

printf("enter index no to be deleted");

scanf("%d",&de);

index=de;

while(s[k].ind!=de)

k++;

printf("\n\t\t\t%d",k);

for(l=k;l<=j;l++)

s[l]=s[l+1];

i--;

j--;

printf("\nindex no %d file is deleted",index);

29
Operating System Manual Royal Institute Of Technology & Science

30
Operating System Manual Royal Institute Of Technology & Science
/* Simulate the Linked file allocation strategy */

#include<stdio.h>

#include<stdlib.h>

typedef struct

int bno,flag,next;

block;

block b[200],b1;

void main()

int rnum();

int i,n,s,s1,p[30],r,k[20];

clrscr();

printf("\nEnter number of programs:");

scanf("%d",&n);

printf("\nEnter the memory block request");

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

printf("\nEnter program requirement");

scanf("%d",&p[i]);

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

s=rnum();

31
Operating System Manual Royal Institute Of Technology & Science
b[s].bno=0;

b[s].flag=1;

k[i]=0;

r=p[i]-1;

while(r!=0)

s1=rnum();

b[s].next=s1;

b[s1].flag=1;

b[s1].bno=0;

s=s1;

r=r-1;

b[s1].next=NULL;

printf("\n Starting blocks for program");

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

printf("\n%5d%5d",i,k[i]);

printf("\n allocated blocks");

for(i=1;i<=200;i++)

if(b[i].flag==1)

printf("\n%5d%5d",b[i].bno,b[i].next);

int rnum()

32
Operating System Manual Royal Institute Of Technology & Science
{

int k,i;

for(i=1;i<=200;i++)

k=rand()%200;

if(b[i].flag!=1)

break;

return k;

33
Operating System Manual Royal Institute Of Technology & Science
/*Simulation of Multi-programming with variable number of tasks (MVT):*/

#include<stdio.h>

#include<conio.h>

main()

static int jobs[20][20],flag[10];

int ch;

static int i,k,nj,nb,tms;clrscr();

printf("Enter time"); /* reading time */

scanf("%d",&tms);

printf("Enter no. of jobs"); /* reading no of jobs */

scanf("%d",&nj);

printf("Enter job information 1.jobid 2.jobsize");

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

scanf("%d%d",&jobs[i][0],&jobs[i][1]);

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

if(tms>=jobs[i][1]){tms=tms-jobs[i][1];

nb=nb+1;flag[i]=1;}

printf("Total memory space available which is not allocated is:%d\n",tms);

printf("Jobs which are not allocated:");

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

if(flag[i] == 0)

printf("%d\t%d\n",jobs[i][0],jobs[i][1]);

if(nb!=nj)

34
Operating System Manual Royal Institute Of Technology & Science
{

while(1)

printf("enter jobid to deallocate:");

scanf("%d",&k);

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

if(jobs[i][0] == k)

if(flag[i] ==1)

tms=tms+jobs[i][1];

flag[i]=2;

printf("Deallocated job %d\t%d\n", jobs[i][0],jobs[i][1]);

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

if(tms>=jobs[i][1])

if(flag[i] == 0)

tms=tms-jobs[i][1];

flag[i]=1;

35
Operating System Manual Royal Institute Of Technology & Science
}

printf("Remaining memory is: %d",tms);

printf("Jobs which are not allocated are:");

for( i=0;i<nj;i++) /* dellocating mamory*/

if(flag[i]==0)

printf("%d\t%d\n", jobs[i][0],jobs[i][1]);

printf("Do you want to deallocate 1.Yes 2.No");

scanf("%d",&ch);

if(ch ==2)break;

printf("Allocated jobs are:");

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

if(flag[i]==1)

printf("%d\t%d\n",jobs[i][0],jobs[i][1]);

getch();}

36
Operating System Manual Royal Institute Of Technology & Science
/*Simulation of Multi-programming with Fixed number of tasks (MFT):*/

MFT ALGORITHM*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define MAX 10

int main()

int ma,bs,ps,tmp,sbn[MAX]={0},ebn[MAX]={0},count=0,i=0,k,ifrag[MAX]={0};

char ch;

clrscr();

printf("\nEnter total memory available (in MB)");

scanf("%d",&ma);

printf("\nEnter size of each block(in MB)");

scanf("%d",&bs);

while(ma)

printf("\nDo u have a program(y/n)");

fflush(stdin);

scanf("%c",&ch);

if((ch!='y') && (ch!='Y'))

printf("\nMemory available %d MB",ma);

break;

37
Operating System Manual Royal Institute Of Technology & Science
printf("\nEnter the size of program(inMB)");

scanf("%d",&ps);

if(ps>ma)

printf("\nMemory required %d\nBUt\nMemory available:%d",ps,ma);

break;

count++;

if(!i)

sbn[i]=0;

ebn[i]=(ceil((float)ps/bs))-1;

else

sbn[i]=ebn[i-1]+1;

ebn[i]=sbn[i]+(ceil((float)ps/bs))-1;

tmp=((ceil((float)ps/bs)*bs));

ifrag[i]=tmp-ps;

i++;

ma-=tmp;

printf("\nMemory allocated%dMB\nMemory available%dMB",tmp,ma);

printf("\nBlocls\tnum of blocks\tInternal fragmentation(inMB)");

for(k=0;k<count;k++)

printf("\n\n%d-%d\t\t%d\t\t%d",sbn[k],ebn[k],ebn[k]-sbn[k]+1,ifrag[k]);

38
Operating System Manual Royal Institute Of Technology & Science
}

getch();

return 1;

39
Operating System Manual Royal Institute Of Technology & Science
3. [Aim:] Simulate Bankers Algorithm for Deadlock Avoidance.

Theory:

Deadlock: A process request the resources, the resources are not available at that time, so the
process enter into the waiting state. The requesting resources are held by another waiting process,
both are in waiting state, this situation is said to be Deadlock.

A deadlocked system must satisfied the following 4 conditions. These are:

(i) Mutual Exclusion: Mutual Exclusion means resources are in non-sharable mode only, it means
only one process at a time can use a process.
(ii) Hold and Wait: Each and every process is the deadlock state, must holding at least one resource
and is waiting for additional resources, that are currently being held by another process.

P P P

(iii) No Preemption: No Preemption means resources are not released in the middle of the work,
they released only after the process has completed its task.

(iv) Circular Wait: If process P1 is waiting for a resource R1, it is held by P2, process P2 is waiting
for R2, R2 held by P3, P3 is waiting for R4, R4 is held by P2, P2 waiting for resource R3, it is held by
P1.

P1 R1 P2 R2 P3 R4 P2 R3

Deadlock Avoidance: It is one of the method of dynamically escaping from the deadlocks. In this
scheme, if a process request for resources, the avoidance algorithm checks before the allocation
of resources about the state of system. If the state is safe, the system allocate the resources to the
requesting process otherwise (unsafe) do not allocate the resources. So taking care before the
allocation said to be deadlock avoidance.

40
Operating System Manual Royal Institute Of Technology & Science
Banker’s Algorithm: It is the deadlock avoidance algorithm, the name was chosen because the bank
never allocates more than the available cash.

Available: A vector of length ‘m’ indicates the number of available resources of each type.
If available[j]=k, there are ‘k’ instances of resource types Rj available.

Allocation: An nxm matrix defines the number of resources of each type currently allocated to
each process. If allocation[i,j]=k, then process Pi is currently allocated ‘k’ instances of resources
type Rj. Max: An nxm matrix defines the maximum demand of each process. If max[i,j]=k, then Pi
may request at most ‘k’ instances of resource type Rj.

Need: An nxm matrix indicates the remaining resources need of each process. If need[I,j]=k, then Pi
may need ‘k’ more instances of resource type Rj to complete this task. There fore,
Need[i,j]=Max[i,j]-Allocation[I,j]

/* Simulate the banker algorithm for dedlock avoidence */

#include<stdio.h>

#include<conio.h>

void main()

int work[5],avl[5],alloc[10][10],l,i;

int need[10][10],n,m,I,j,avail[10],max[10][10],k,count,fcount=0,pr[10];

char finish[10]={'f','f','f','f','f','f','f','f','f','f'};

clrscr();

printf("\n enter the no of process");

scanf("%d",&n);

printf("\n enter the no of resources");

scanf("%d",&m);

printf("\n enter the total no of resources");

for(i=1;i<=m;i++)

scanf("%d",&avail[i]);

printf("\n enter the max resources req by each pr alloc matrix");


41
Operating System Manual Royal Institute Of Technology & Science
for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

scanf("%d",&max[i][j]);

printf("\n process allocation matrix");

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

for(j=1;j<=m;j++)

scanf("%d",&alloc[i][j]);

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

for(j=1;j<=m;j++)

need[i][j]=max[i][j]-alloc[i][j];

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

k=0;

for(j=1;j<=m;j++)

k=k+alloc[i][j];

avl[i]=avl[i]-k;

work[i]=avl[i];

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

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

count=0;

for(j=1;j<=m;j++)

42
Operating System Manual Royal Institute Of Technology & Science
if((finish[i]=='f')&&(need[i][j]<=work[i]))

count++;

if(count==m)

for(l=1;l<=m;l++)

work[l]=work[l]+alloc[i][l];

finish[i]='t';

pr[k]=i;

break;

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

if(finish[i]=='t')

fcount++;

if(fcount==n)

printf("\n the system is in safe state");

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

printf("\n %d",pr[i]);

else

printf("\n the system is not in safe state");

getch();

43
Operating System Manual Royal Institute Of Technology & Science

44
Operating System Manual Royal Institute Of Technology & Science
4. [Aim:] Simulate paging technique of memory management.

Theory:
Paging is an efficient memory management scheme because it is non-contiguous memory allocation
method. The basic idea of paging is the physical memory (main memory) is divided into fixed sized
blocks called frames, the logical address space is divided into fixed sized blocks, called pages, but
page size and frame size should be equal. The size of the frame or a page is depending on operating
system.

In this scheme the operating system maintains a data structure that is page table, it is used for
mapping purpose. The page table specifies the some useful information, it tells which frames are there
and so on. The page table consisting of two fields, one is the page number and other one is frame
number. Every address generated by the CPU divided into two parts, one is page number and second
is page offset or displacement. The pages are loaded into available free frames in the physical
memory.

/* Simulate the paging technique of memory management */

#include<stdio.h>

#include<conio.h>

void main()

int ps,ms,np,nf,pt[20],I,i,page,offset,id,ph_add;

clrscr();

printf("\n Enter page size, memorysize, no of pages");

scanf("%d%d%d",&ps,&ms,&np);

nf=ms/ps;

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

printf("\n Enter the size of local address");

scanf("%d",&id);

page=id%ps;

ph_add=pt[page]*ps+offset;

45
Operating System Manual Royal Institute Of Technology & Science
printf(" \n physical address is %d",ph_add);

printf(" \n no of frames+%d \n pages=%d \n offset %d",nf,page,offset);

getch();

46
Operating System Manual Royal Institute Of Technology & Science
5. [Aim:] Simulate all page replacement algorithms
a) FIFO
b) LRU
c) LFU

Theory:

a) FIFO (First in First Out) algorithm: FIFO is the simplest page replacement algorithm, the
idea behind this is, “Replace a page that page is oldest page of main memory” or “Replace the
page that has been in memory longest”. FIFO focuses on the length of time a page has been in
the memory rather than how much the page is being used.

b) LRU (Least Recently Used): the criteria of this algorithm is “Replace a page that has been
used for the longest period of time”. This strategy is the page replacement algorithm looking
backward in time, rather than forward.

c) LFU (Least Frequently Used): The least frequently used algorithm “select a page for
replacement, if the page has not been used for the often in the past” or “Replace page that page
has smallest count” for this algorithm each page maintains as counter which counter value
shows the least count, replace that page. The frequency counter is reset each time is page is
loaded.

/*implement the FIFO page replacement algorithm */

#include<stdio.h>

int main()

int i,j,n,a[50],frame[10],no,k,avail,count=0;

printf("\n ENTER THE NUMBER OF PAGES:\n");

scanf("%d",&n);

printf("\n ENTER THE PAGE NUMBER :\n");

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

scanf("%d",&a[i]);

printf("\n ENTER THE NUMBER OF FRAMES :");

scanf("%d",&no);

47
Operating System Manual Royal Institute Of Technology & Science
for(i=0;i<no;i++)

frame[i]= -1;

j=0;

printf("\tref string\t page frames\n");

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

printf("%d\t\t",a[i]);

avail=0;

for(k=0;k<no;k++)

if(frame[k]==a[i])

avail=1;

if (avail==0)

frame[j]=a[i];

j=(j+1)%no;

count++;

for(k=0;k<no;k++)

printf("%d\t",frame[k]);

printf("\n");

printf("Page Fault Is %d",count);

return 0;

48
Operating System Manual Royal Institute Of Technology & Science

49
Operating System Manual Royal Institute Of Technology & Science
/*Implementation of LRU page replacement algorithm */

#include<stdio.h>

#include<conio.h>

int fr[3];

void main()

void display();

int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];

int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;

clrscr();

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;

50
Operating System Manual Royal Institute Of Technology & Science
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++)

fs[i]=0;

for(k=j-1,l=1;l<=frsize-1;l++,k--)

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

if(fr[i]==p[k])

fs[i]=1;

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

51
Operating System Manual Royal Institute Of Technology & Science
if(fs[i]==0)

index=i;

fr[index]=p[j];

pf++;

display();

printf("\n no of page faults :%d",pf);

getch();

void display()

int i;

printf("\n");

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

printf("\t%d",fr[i]);

52
Operating System Manual Royal Institute Of Technology & Science
/*Implement the LFU PAGE REPLACEMENT ALGORITHM */

#include<stdio.h>

main()

int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no of pages:");

scanf("%d",&n);

printf("Enter the reference string:");

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

scanf("%d",&p[i]);

printf("Enter no of frames:");

scanf("%d",&f);

q[k]=p[k];

printf("\n\t%d\n",q[k]);

c++;

k++;

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

c1=0;

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

if(p[i]!=q[j])

c1++;

if(c1==f)

53
Operating System Manual Royal Institute Of Technology & Science
c++;

if(k<f)

q[k]=p[i];

k++;

for(j=0;j<k;j++)

printf("\t%d",q[j]);

printf("\n");

else

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

c2[r]=0;

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

if(q[r]!=p[j])

c2[r]++;

else

break;

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

b[r]=c2[r];

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

54
Operating System Manual Royal Institute Of Technology & Science
for(j=r;j<f;j++)

if(b[r]<b[j])

t=b[r];

b[r]=b[j];

b[j]=t;

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

if(c2[r]==b[0])

q[r]=p[i];

printf("\t%d",q[r]);

printf("\n");

printf("\nThe no of page faults is %d",c);

55
Operating System Manual Royal Institute Of Technology & Science

56

You might also like