0% found this document useful (0 votes)
92 views52 pages

Animation Os Lab

The document describes experiments related to operating system concepts like CPU scheduling algorithms, memory management, and file organization techniques. Some key points: 1) The first 15 experiments involve implementing various CPU scheduling algorithms like round robin, shortest job first, first come first served, and priority scheduling. 2) Experiments 5-7 simulate file allocation strategies like sequential, indexed, and linked allocation. 3) Experiments 8 simulates memory management techniques like MVT and MFT. 4) The remaining experiments involve simulating different file organization techniques, implementing the banker's algorithm for deadlock avoidance/prevention, and page replacement algorithms.

Uploaded by

Anuradha Patnala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views52 pages

Animation Os Lab

The document describes experiments related to operating system concepts like CPU scheduling algorithms, memory management, and file organization techniques. Some key points: 1) The first 15 experiments involve implementing various CPU scheduling algorithms like round robin, shortest job first, first come first served, and priority scheduling. 2) Experiments 5-7 simulate file allocation strategies like sequential, indexed, and linked allocation. 3) Experiments 8 simulates memory management techniques like MVT and MFT. 4) The remaining experiments involve simulating different file organization techniques, implementing the banker's algorithm for deadlock avoidance/prevention, and page replacement algorithms.

Uploaded by

Anuradha Patnala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 52

OPERATING SYSTEM LAB

EXPNO NAME OF THE EXPERIMENT


1 Write a program to implement RR( Round Robin) Scheduling

2 Simulate SJF(Shortest Job First) CPU Scheduling algorithm


3 Write a program the FCFS(First Come First Served) CPU Scheduling
algorithm
4 Write a program to Priority CPU Scheduling algorithm

5 Simulate Sequential file allocation strategies

6 Simulate Indexed file allocation strategies


7 Simulate Linked file allocation strategies
Simulate MVT and MFT
8 a)MVT (Multiprogramming Variable Task)
b)MFT (Multiprogramming Fixed Task)

9 Simulate Single level directory File organization techniques

10 Simulate Two level File organization techniques

11 Simulate Hierarchical File organization techniques

Write a program for Bankers Algorithm for Dead Lock Avoidance


12
Implement Bankers Algorithm Dead Lock Prevention.
13
a) FIFO (First In First Out) Page Replacement
14
b) LRU (Least Recent Used) Page Replacement
c) Optimal Page Replacement (LFU)

15 Simulate Paging Techniques of memory management


Ex. No:1 ROUND ROBIN SCHEDULING

Aim: Write a C program to implement the various process scheduling


mechanisms such
as Round Robin Scheduling.

Algorithm for RR

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue and time
quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the
CPU burst time
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices
=1.
Step 6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst
time of process(n-1 ) + the time difference in getting the CPU from
process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) +
burst time of process(n)+ the time difference in getting CPU from
process(n).
Step 7: Calculate
(g) Average waiting time = Total waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of
process Step 8: Stop the process
/* ROUND ROBIN SCHEDULING ALGORITHM */

Program:

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

void main()
{
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1; int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
clrscr();

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


printf("Enter the number of Processors \n");
scanf("%d",&n);
n1=n;
printf("\n Enter the Timeslice \n");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the process ID %d",i);
scanf("%d",&pid[i]);
printf("\n Enter the Burst Time for the process");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0; n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;

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


printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t
TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n The average Waiting Time=4.2f",awt);
printf("\n The average Turn around Time=4.2f",atat);
getch();

OUTPUT:

ROUND ROBIN SCHEDULING


Enter the number of Processors 4

Enter the Timeslice 5

Enter the process ID 1 5

Enter the Burst Time for the process 10 Enter the process ID 2 6

Enter the Burst Time for the process 15 Enter the process ID 3 7

Enter the Burst Time for the process 20 Enter the process ID 4 8

Enter the Burst Time for the process 25

ROUND ROBIN SCHEDULING ALGORITHM

Process Process BurstTim Waiting TurnaroundTi


ID e Time me

1 5 10 15 25
2 6 15 25 40
3 7 20 25 45
4 8 25 20 45

The average Waiting Time=4.2f The average Turn around Time=4.2f


Ex. No: 2 SJF SCHEDULING

Aim: Write a C program to implement the various process scheduling


mechanisms such as SJF Scheduling .

Algorithm for SJF


Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the
CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting
according to lowest to
highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround
time as its burst time.
Step 6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst
time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+
Burst time for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of
process Step 7: Stop the process
/* SJF SCHEDULING ALGORITHM */

Program:
#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
clrscr();

printf("Enter number of process\n");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of Process %d",i);
scanf("\n %d",&t[i]);
}

for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}
printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");
printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t TURNAROUND
TIME \n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d \t\t5%d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);
}
twt=0; ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME %4.2f",awt);
printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
getch();
}
}
OUTPUT:

Enter number of process 3

Enter the Burst Time of Process 04 Enter the Burst Time of Process 13 Enter

the Burst Time of Process 25

SHORTEST JOB FIRST SCHEDULING ALGORITHM


PROCESS ID BURST TIME WAITING TIME TURNAROUND TIME

1 3 0 3

0 4 3 7
2 5 7 12

AVERAGE WAITING TIME 3.33 AVERAGE TURN AROUND TIME


7.33
Ex. No: 3 FCFS SCHEDULING

Aim: Write a C program to implement the various process scheduling


mechanisms such

Algorithm for FCFS scheduling:


Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the
CPU burst time
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its
turnaround time
Step 5: for each process in the Ready Q calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst
time of process(n-1)
(b) Turna round time for Process(n)= waiting time of Process(n)+
Burst time for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of
process Step 7: Stop the process
/* FCFS SCHEDULING ALGORITHM */

Program:

#include<stdio.h>
void main ()
{
int i, n,sum,wt,tat,twt,ttat; int t[10];
float awt, atat;
clrscr ();
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0; twt=0; ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n; atat=(float)ttat/n;
printf ("\n Average Waiting Time %4.2f”, awt);
printf("\n Average Turnaround Time %4.2f",atat);
getch();
}
OUTPUT:

Enter number of processors: 3

Enter the Burst Time of the 2


process 1:
Enter the Burst Time of the 5
process 2:
Enter the Burst Time of the 4
process 3:

FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Process ID Waiting Turn Around


Time Time
1 0 2
2 2 7

3 7 11

Average Waiting Time 3.00 Average


Ex. No: 4 PRIORITY SCHEDULING

Aim: Write a C program to implement the various process scheduling


mechanisms such as Priority Scheduling.

Algorithm for Priority Scheduling:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the
CPU burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‘0’ and its burst time as its
turn around time
Step 6: For each process in the Ready Q calculate
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst
time of process(n-1)
(f) Turn around time for Process(n)= waiting time of Process(n)+
Burst time for process(n)
Step 7: Calculate
(e) Average waiting time = Total waiting Time / Number of process
(f) Average Turnaround time = Total Turnaround Time / Number of
process Step 8: Stop the process
/* PRIORITY SCHEDULING */

Program:

#include<stdio.h> #include<conio.h> void main ()


{
int i, j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt, atat;
clrscr ();
printf ("\n-----------PRIORITY SCHEDULING \n");
printf ("Enter the No of Process: ");
scanf ("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf ("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
//Sorting start

pid[i] = i;
printf ("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);

for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;

t = bt[i];
bt[i] = bt[j];
bt[j] = t;

t = pid[i];
pid[i] = pid[j];
pid[j] = t;
}
}

// Sorting finished

tat[0] = bt[0];
wt[0] = 0;

for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n \n");
printf("Pid\t Priority\tBurst time\t Waiting Time\tTurnArroundTime\n");
printf("\n \n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time:%f\n",awt,atat);
getch();
}
OUTPUT:

-----------PRIORITY SCHEDULING--------------

Enter the No of Process: 4


Enter the Burst time of Pid 0 : 2 Enter the Priority of Pid 0 : 3
Enter the Burst time of Pid 1 : 6 Enter the Priority of Pid 1 : 2
Enter the Burst time of Pid 2 : 4 Enter the Priority of Pid 2 : 1
Enter the Burst time of Pid 3 : 5 Enter the Priority of Pid 3 : 7

Pid Priority Burst time WaitingTimeTurnArroundTime

2 1 4 0 4
1 2 6 4 10
0 3 2 10 12
3 7 5 12 17

Avg.Waiting Time: 6.500000 Avg.Turn Around Time: 10.750000


Exp no:5 SEQUENTIAL FILE ALLOCATION

AIM: Write a C Program to implement Sequential File Allocation method.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each
process. Step 4: First fit algorithm searches all the entire memory block until
a hole which is
big enough is encountered. It allocates that memory block for the
requesting
process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole
which can
be allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole
and allocates it to the process.
Step 7: Analyses all the three memory management techniques and display
the best
algorithm which utilizes the memory resources effectively and
efficiently.
Step 8: Stop the program.
Program:

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}

OUTPUT:

Enter no.of files: 2


Enter no. of blocks occupied by file1 4 Enter the starting block of file1 2
Enter no. of blocks occupied by file2 10 Enter the starting block of file2 5
Filename Start block length
1 2 4
2 5 10
Enter file name: rajesh
File name is:12803 length is:0blocks occupied
Exp no:6 INDEXED FILE ALLOCATION

AIM: Write a C Program to implement Indexed File Allocation method.

Algorithm:

Step 1: Start.
Step 2: Let n be the size of the buffer Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer Step 6: If the buffer is
full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required Step
11: Terminate the process.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
}
printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}
printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}

OUTPUT:
Enter no. of files:2
Enter starting block and size of file1: 2 5 Enter blocks occupied by file1:10
enter blocks of file1:3 2 5 4 6 7 2 6 4 7
Enter starting block and size of file2: 3 4 Enter blocks occupied by file2:5
enter blocks of file2: 2 3 4 5 6 File index length
1 2 10
2 3 5
Enter file name: venkat file name is:12803
Index is:0Block occupied are:
Exp no:7 LINKED FILE ALLOCATION

AIM: Write a C Program to implement Linked File Allocation method.

ALGORITHM:

Step 1: Create a queue to hold all pages in memory


Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the
stack
Step 6: Stop the allocation.

Program:

#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
void main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}

OUTPUT:

Enter no. of files:2 Enter file name:venkat Enter starting block:20 Enter
no.of blocks:6 Enter block numbers: 4 12
15
45
32
25
Enter file name:rajesh Enter starting block:12 Enter no.of blocks:5 Enter
block numbers:6 5
4
3
2
File start size block
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2
Exp. No: 8(a) MULTIPROGRAM VARIABLE TASK

AIM: Write a program to implement Dynamic allocation of memories in


MVT.

Algorithm:

Step1: start the process. Step2: Declare variables. Step3: Enter total
memory size. Step4: Allocate memory for os.
Step5: allocate total memory to the pages. Step6: Display the wastage of
memory.
Step7: Stop the process.

/* MVT */
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,m,n,tot,s[20];
clrscr();
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("Enter size of page%d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m; for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Allocate page %d\n",i+1);
tot=tot-s[i];
}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is=%d",tot);
getch();
}

OUTPUT:

Enter total memory size : 50 Enter no.of pages 4


Enter memory for OS 10

Enter size of page 10


Enter size of page 9
Enter size of page 9
Enter size of page 10

External Fragmentation is = 2
Exp. No: 8(b) MULTIPROGRAM FIXED TASK

AIM: Write a program to implement Dynamic allocation of memories in


MVT.

Algorithm:

Step1: start the process. Step2: Declare variables. Step3: Enter total
memory size. Step4: Allocate memory for os.
Step5: allocate total memory to the pages. Step6: Display the wastage of
memory.
Step7: Stop the process.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int ms,i,ps[20],n,size,p[20],s,intr=0;
clrscr();
printf("Enter size of memory:");
scanf("%d",&ms);
printf("Enter memory for OS:");
scanf("%d",&s);
ms-=s;
printf("Enter no.of partitions to be divided:");
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
{
printf("Enter process and process size");
scanf("%d%d",&p[i],&ps[i]);
if(ps[i]<=size)
{
intr=intr+size-ps[i];
printf("process%d is allocated\n",p[i]);
}
else
printf("process%d is blocked",p[i]);
}
printf("total fragmentation is %d",intr);
getch();
}

OUTPUT:

Enter total memory size : 50 Enter memory for OS :10

Enter no.of partitions to be divided:4

Enter size of page : 10 Enter size of page : 9 Enter size of page : 9 Enter size
of page : 8

Internal Fragmentation is = 4
Exp. No: 9 SINGLE LEVEL DIRECTORY FILE ORGANIZATION
TECHNIQUES

Single Level Directories: It is the simplest of all directory structures,


in this the directory system having only one directory, it consisting of all files.
Sometimes it is said to be the root directory. The following dig. Shows single
level directory that contains four files (A, B, C, D). It has the simplicity and
ability to locate files quickly. It is not used in the multi-user system, it is used on
the small embedded system.

Algorithm:

Step 1: Start the program.


Step 2: Get the name of the directories.
Step 3: get the number of files.
Step 4: Get the name of the each file.
Step 5: Now each file is in the form of fill circle
Step 6: Every file is connected with respective directory
Step 7: Display the connected graph along with name using graphics
Step 8: Stop the program.

Program:

#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1) (*root)->ftype=1;
else (*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;ilink[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How many users");
else
printf("hoe many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else (*root)->nc=0;
if((*root)- >nc==0)
gap=rxlx;
else
gap=(rx-lx)/(*root)- >nc;
for(i=0;inc; i++)
create(&((*root)->link[i]) ,lev+1,(*root)- >name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;inc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;inc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:
Exp. No: 10 TWO LEVEL DIRECTORY FILE ORGANIZATION
TECHNIQUES
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{

if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}
OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Enter name of directory -- DIR1
Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR2

Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1


Enter name of the file -- A1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1


Enter name of the file -- A2
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR2


Enter name of the file -- B1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 5
Directory Files
DIR1 A1 A2
DIR2 B1

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory – DIR


Directory not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 3
Enter name of the directory – DIR1
Enter name of the file -- A2

File A2 is deleted

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice – 6
Exp. No: 11 HIERARCHICAL DIRECTORY FILE ORGANIZATION
TECHNIQUES

#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element
node; void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) :",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 forfile :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name); scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14); if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:
Enter Name of dir/file (under root): ROOT
Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under ROOT):USER 1
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1):1
Enter Name of dir/file (under USER 1):SUBDIR
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR):2
Enter Name of dir/file (under USER 1):
JAVA Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA): 0
Enter Name of dir/file (under SUBDIR):VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 0

Enter Name of dir/file (under ROOT):USER2


Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER2):2
Enter Name of dir/file (under ROOT):A
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under USER2):SUBDIR 2
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR 2):2

Enter Name of dir/file (under SUBDIR2):PPL


Enter 1 for Dir /2 for file:1
No of subdirectories /files (for PPL):2
Enter Name of dir/file (under PPL):B
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under PPL):C
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under SUBDIR):AI
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for AI): 2
Enter Name of dir/file (under AI):D
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under AI):E
Ex. No: 12 & 13 BANKER’S ALGORITHM

AIM: To implement deadlock avoidance & Prevention by using Banker’s


Algorithm.

Deadlock avoidance & Dead Lock Prevention Banker’s Algorithm:

When a new process enters a system, it must declare the maximum


number of instances of each resource type it needed. This number may
exceed the total number of resources in the system. When the user request a
set of resources, the system must determine whether the allocation of each
resources will leave the system in safe state. If it will the resources are
allocation; otherwise the process must wait until some other process release
the resources.

Data structures
 n-Number of process, m-number of resource types.
 Available: Available[j]=k, k – instance of resource type Rj is
available.
 Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
 Allocation: If Allocation [i, j]=k, Pi allocated to k instances of
resource Rj
 Need: If Need[I, j]=k, Pi may need k more instances of resource type
Rj, Need[I, j]=Max[I, j]-Allocation[I, j];

Safety Algorithm
1. Work and Finish be the vector of length m and n respectively,
Work=Available and Finish[i] =False.
2. Find an i such that both
 Finish[i] =False
 Need<=Work
If no such I exists go to step 4.
3. work=work+Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.

Resource request algorithm


Let Request i be request vector for the process Pi, If request i=[j]=k,
then process Pi wants k instances of resource type Rj.
1. if Request<=Need I go to step 2. Otherwise raise an error condition.
2. if Request<=Available go to step 3. Otherwise Pi must since the
resources are available.
3. Have the system pretend to have allocated the requested resources to
process Pi by modifying the state as follows;
Available=Available-Request I; Allocation I =Allocation+Request I;
Need i=Need i-Request I;

If the resulting resource allocation state is safe, the transaction is completed


and process Pi is allocated its resources. However if the state is unsafe, the
Pi must wait for Request i and the old resource-allocation state is restored.

ALGORITHM:

1. Start the program.


2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. or not if we allow the request.
10. stop the program.

/* BANKER’S ALGORITHM */
Program:

#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
clrscr();
printf("\n ENTER THE NO. OF PROCESSES:");
scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t"); for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0) printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else
printf("\n DEADLOCK OCCURED");
getch();
}
OUTPUT:

//TEST CASE 1:

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3 PROCESS 1


MAXIMUM VALUE FOR RESOURCE 1:3 MAXIMUM VALUE FOR
RESOURCE 2:2 MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1 ALLOCATED FROM RESOURCE
2:0 ALLOCATED FROM RESOURCE 3:0 PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6 MAXIMUM VALUE FOR
RESOURCE 2:1 MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5 ALLOCATED FROM RESOURCE
2:1 ALLOCATED FROM RESOURCE 3:1 PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3 MAXIMUM VALUE FOR
RESOURCE 2:1 MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2 ALLOCATED FROM RESOURCE
2:1 ALLOCATED FROM RESOURCE 3:1 PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4 MAXIMUM VALUE FOR
RESOURCE 2:2 MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0 ALLOCATED FROM RESOURCE
2:0 ALLOCATED FROM RESOURCE 3:2 ENTER TOTAL VALUE OF
RESOURCE 1:9 ENTER TOTAL VALUE OF RESOURCE 2:3 ENTER
TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 100 222 936 112
P2 613 511 102
P3 314 211 103
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


P2 010 623
P1 401 723
P3 620 934
P4 514 936

THE ABOVE SEQUENCE IS A SAFE SEQUENCE

//TEST CASE:2

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3 PROCESS 1


MAXIMUM VALUE FOR RESOURCE 1:3 MAXIMUM VALUE FOR
RESOURCE 2:2 MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1 ALLOCATED FROM RESOURCE
2:0 ALLOCATED FROM RESOURCE 3:1 PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6 MAXIMUM VALUE FOR
RESOURCE 2:1 MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5 ALLOCATED FROM RESOURCE
2:1 ALLOCATED FROM RESOURCE 3:1 PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3 MAXIMUM VALUE FOR
RESOURCE 2:1 MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2 ALLOCATED FROM RESOURCE
2:1 ALLOCATED FROM RESOURCE 3:2 PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4 MAXIMUM VALUE FOR
RESOURCE 2:2 MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0 ALLOCATED FROM RESOURCE
2:0 ALLOCATED FROM RESOURCE 3:2

ENTER TOTAL VALUE OF RESOURCE 1:9 ENTER TOTAL VALUE OF


RESOURCE 2:3 ENTER TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 101 221 936 110
P2 613 511 102
P3 314 212 102
P4 422 002 420

AVAIL BEFORE AVAIL AFTER DEADLOCK OCCURED


Ex. No: 14(a) FIFO PAGE REPLACEMENT ALGORITHM

AIM: To implement page replacement algorithms FIFO (First In First Out)

ALGORITHM:

FIFO:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue

/* FIFO Page Replacement Algorithm */

Program:

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames ");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}

OUTPUT:
FIFO PAGE REPLACEMENT ALGORITHM

Enter no.of frames4


Enter number of reference string.. 6
Enter the reference string.. 5 6 4 1 2 3
The given reference string:
...................................... 5 6 4 1 2 3
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1

No.of pages faults6


Ex. No:14(b)LRU PAGE REPLACEMENT ALGORITHM
AIM: To implement page replacement algorithm LRU (Least Recently
Used)

LRU (Lease Recently Used)


Here we select the page that has not been used for the longest period of
time.

ALGORITHM:

Step 1: Create a queue to hold all pages in memory


Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the
stack

/* LRU Page Replacement Algorithm */

Program:

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();

void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames ");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n ");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
OUTPUT:
LRU PAGE REPLACEMENT ALGORITHM

Enter no.of Frames 3


Enter no.of reference string 6
Enter reference string.. 6 5 4 2 3 1
LRU PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1
Reference NO 6->
6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
Ex. No: 14(c) OPTIMAL(LFU) PAGE REPLACEMENT
ALGORITHM
AIM: To implement page replacement algorithms Optimal (The page which is not
used for longest time)
ALGORITHM:

Optimal algorithm
Here we select the page that will not be used for the longest period of
time.

OPTIMAL:
Step 1: Create a array
Step 2: When the page fault occurs replace page that will not be used for the
longest
period of time
/*OPTIMAL(LFU) page replacement algorithm*/

Program:

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n ");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n ");
printf("\nThe given string");
printf("\n \n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}

OUTPUT:

OPTIMAL PAGE REPLACEMENT ALGORITHM

Enter no.of Frames 3


Enter no.of reference string 6

Enter reference string.. 6 5 4 2 3 1


OPTIMAL PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1

Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6

You might also like