LAB PLAN
Session 2025-2026
S. N. Name of Experiment
1 Write a Program to implement the FCFS (First Come First Served)
2 Write a Program to implement the SJF (Shortest Job First)
3 Write a Program to implement the Priority Scheduling
4 Write a Program to implement the RR (Round Robin) Scheduling
5 Write a Program implementation of producer consumer problem
6 Write a Program Implementation of Dining of philosopher problem
7 Write a Program to implement the Banker’s Algorithm for Dead Lock
Avoidance and Dead Lock Prevention.
8 Write a Program to implement the FIFO (First In First Out) Page
Replacement.
9 Write a Program to implement the LRU (Least Recent Used) Page
Replacement
10 Write a Program to implement the Paging Memory Allocation Technique
11 Write a Program to implement the Segmentation Memory Allocation
Technique
Experiment No.1
FCFS SCHEDULING
Aim: Write a C program to implement the FCFS SCHEDULING POLICY.
FCFS SCHEDULING ALGORITHM: - Implementation in C Language
#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 process 1: 2
Enter the Burst Time of the process 2: 5
Enter the Burst Time of the process 3: 4
FIRST COME FIRST SERVE SCHEDULING ALGORITHM
Process ID Waiting Time Turn Around Time
1 0 2
2 2 7
3 7 11
Average Waiting Time 3.00
Average Turnaround Time 6.67
Experiment No.2
SJF SCHEDULING
Aim: Write a C program to implement SJF SCHEDULING mechanism
SJF SCHEDULING ALGORITHM :- Implementation in C Language
#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 4
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
Experiment No.3
PRIORITY SCHEDULING
Aim: Write a C program to implement PRIORITY SCHEDULING mechanisms.
PRIORITY SCHEDULING: Implementation in C Language
#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
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 WaitingTime\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\[Link] Time: %f\[Link] 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 WaitingTime TurnArroundTime
----------------------------------------------------------------------------------------
2 1 4 0 4
1 2 6 4 10
0 3 2 10 12
3 7 5 12 17
[Link] Time: 6.500000
[Link] Around Time: 10.750000
Experiment No.4
ROUND ROBIN SCHEDULING
Aim:Write a C program to implement the ROUND ROBIN SCHEDULINGmechanism.
ROUND ROBIN SCHEDULING ALGORITHM:-Implementation in C Language
#include<stdio.h>
#include<conio.h>
int z[10],b[10],n,m[50],r,q,e=0,avg=0,i,j;
float f;
main()
{
clrscr();
printf("\n\tJOB SCHEDULING ALGORITHM[RR]");
printf("\n\t*******************************************************\n");
printf("\nEnter how many jobs:");
scanf("%d",&n);
printf("\nEnter burst time for corresponding job...\n");
for(i=1;i<=n;i++)
{
printf("\nProcess %d: ",i);
scanf("%d",&b[i]); z[i]=b[i];
}
printf("\nENTER THE TIME SLICE VALUE:");
scanf("%d",&q);
rr();
average();
getch();
return 0;
}
rr()
{
int max=0;
max=b[1];
for(j=1;j<=n;j++)
if(max<=b[j])
max=b[j];
if((max%q)==0)
r=(max/q);
else
r=(max/q)+1;
for(i=1;i<=r;i++)
{
printf("\nround %d",i);
for(j=1;j<=n;j++)
{
if(b[j]>0)
{
b[j]=b[j]-q;
if(b[j]<=0)
{
b[j]=0;
printf("\nprocess %d is completed",j);
}
else
printf("\nprocess %d remaining time is %d",j,b[j]);
}
}
delay(1000);
}
return 0;
}
average()
{
for(i=1;i<=n;i++)
{
e=0;
for(j=1;j<=r;j++)
{
if(z[i]!=0)
{
if(z[i]>=q)
{
m[i+e]=q; z[i]-=q;
}
else
{
m[i+e]=z[i]; z[i]=0;
}
}
else
m[i+e]=0;
e=e+n;
}
}
for(i=2;i<=n;i++)
for(j=1;j<=i-1;j++)
avg=avg+m[j];
for(i=n+1;i<=r*n;i++)
{
if(m[i]!=0)
{
for(j=i-(n-1);j<=i-1;j++)
avg=m[j]+avg;
}
}
f=avg/n;
printf("\nTOTAL WATING:%d",avg);
printf("\n\nAVERAGE WAITING TIME:%f\n",f);
for(i=1;i<=r*n;i++)
{ if(m[i]!=0)
if(i%n==0){
printf("P%d",(i%n)+(n)); }
else
printf("P%d",(i%n));
for(j=1;j<=m[i];j++)
printf("%c",22);
}
printf("\n");
getch();
return 0;
}
OUTPUT
JOB SCHEDULING ALGORITHM[RR]
*******************************************************
Enter how many jobs:4
Enter burst time for corresponding job...
Process 1: 10
Process 2: 15
Process 3: 20
Process 4: 25
ENTER THE TIME SLICE VALUE:5
round 1
process 1 remaining time is 5
process 2 remaining time is 10
process 3 remaining time is 15
process 4 remaining time is 20
round 2
process 1 is completed
process 2 remaining time is 5
process 3 remaining time is 10
process 4 remaining time is 15
round 3
process 2 is completed
process 3 remaining time is 5
process 4 remaining time is 10
round 4
process 3 is completed
process 4 remaining time is 5
round 5
process 4 is completed
TOTAL WATING:130
AVERAGE WAITING TIME:32.000000
P1▬▬▬▬▬P2▬▬▬▬▬P3▬▬▬▬▬P4▬▬▬▬▬P1▬▬▬▬▬P2▬▬▬▬▬P
3▬▬▬▬▬P4▬▬▬▬▬P2▬▬▬▬▬P3▬▬▬▬▬P4▬▬▬▬▬P3▬
▬▬▬▬P4▬▬▬▬▬P4▬▬▬▬▬
EXPERIMENT NO. 5
PRODUCER CONSUMER PROBLEM
AIM:
To write a C program to implement producer consumer problem.
ALGORITHM:
Step 1: start
Step 2: display the menu and read
Step 3: If choice=1 then do the following steps
a) Get the process to be produced.
b) Check whether the process already exists.
If yes
display the message
Else
Produce the process and display the process list.
Step 4: If choice=2 then do the following steps
a) Get the process to be consumed
b) Check whether the process is already produced
If yes consume the process
Else
Display the waiting list
step 5: stop.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct prod
{
int s;
char wait[20][20];
};
static struct prod se={0};
char produce[20][20],consume[20];
int flag,i,j,z=1;
void main()
{
int ch;
void producer();
void consumer();
do
{
printf("\n\t\t MENU");
printf("\n\t\t ****");
printf("\n [Link]");
printf("\n [Link]");
printf("\n [Link]");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
producer();
break;
case 2:
consumer();
break;
case 3:
exit(0);
break;
}
}
while(ch!=3);
}
void producer()
{
flag=0;
printf("\n enter the producer process name:");
scanf("%s",&produce[++se.s]);
for(i=0;i<se.s;i++)
{
if(strcmp(produce[i],produce[se.s])==0)
{
printf("\n process already exist");
getch();
flag=1;
se.s--;
break;
}
}
for(i=0;i<se.s;i++)
{
if(strcmp([Link][i],produce[se.s])==0)
{
j=1;
printf("\n process %s now consumed",produce[se.s]);
se.s--;
flag=2;
break;
}
}
if(flag==1)
return;
else if(flag==2)
{
for(i=j;i<z;i++)
strcpy([Link][i],[Link][i+1]);
z--;
}
else if(flag==0)
{
printf("list of produced process\n");
for(i=1;i<se.s;i++)
printf("%s\n",produce[i]);
}
}
void consumer()
{
flag=0;
printf("\n enter the consumer process name:");
scanf("%s",&consume);
for(i=1;i<se.s;i++)
{
if(strcmp(produce[i],consume)==0)
{
printf("\n process %s now consumed",produce[i]);
j=1;
flag=1;
break;
}
}
for(i=0;i<z;i++)
{
if(strcmp(produce[i],consume)==0)
{
printf("\n process already exists");
flag=2;
break;
}
}
if(flag==1)
{
for(i=1;i<se.s;i++)
strcpy(produce[i],produce[i+1]);
se.s--;
}
else if(flag==0)
{
strcpy([Link][++z],consume);
z++;
printf("list of waiting process\n");
for(i=1;i<z;i++)
printf("%s\n",[Link][i]);
}
}
Output:
EXPERIMENT 6
DINING PHILOSOPHERS PROBLEM C PROGRAM
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<dir.h>
char fn2[20];
main()
{
int c;
clrscr();
do{
printf("\n\t\tMain Menu\n-------------------------------\n");
printf("[Link] a File\[Link] a File\[Link]\n");
scanf("%d",&c);
switch(c)
{
case 1:
copy_file();
break;
case 2:
move_file();
break;
case 3:
exit(0);
}
}while(c<=3);
getch();
return 0;
}
copy_file()
{
FILE *f1,*f2;
char ch,s[10],fn1[20];
int a;
printf("\nAre u see the privious files(1/0)?");
scanf("%d",&a);
if(a==1)
print_file();
printf("Enter the source file name:");
scanf("%s",&fn1);
printf("Enter the Destination file name:");
scanf("%s",&fn2);
f1=fopen(fn1,"r");
if(f1==NULL)
printf("Can't open the file");
else {
f2=fopen(fn2,"w");
while((ch=getc(f1))!=EOF)
putc(ch,f2);
printf("One File Copied");
fclose(f2);
}
fclose(f1);
return 0;
}
move_file()
{
FILE *f1,*f2;
char ch,s[10],fn1[20];
int a;
printf("\nAre u see the privious files(1/0)?");
scanf("%d",&a);
if(a==1)
print_file();
printf("Enter the source file name:");
scanf("%s",&fn1);
printf("Enter the Destination file name:");
scanf("%s",&fn2);
f1=fopen(fn1,"r");
if(f1==NULL)
printf("Can't open the file");
else {
f2=fopen(fn2,"w");
while((ch=getc(f1))!=EOF)
putc(ch,f2);
printf("One File moved");
fclose(f2);
remove(fn1);
}
fclose(f1);
return 0;
}
print_file()
{
struct ffblk ffblk;
int d,p=0;
char ch;
d=findfirst("*.*",&ffblk,0);
while(!d)
{
printf("%s\n",ffblk.ff_name);
d=findnext(&ffblk);
p=p+1;
if(p>=20)
{
printf("Press any key to continue");
getchar();
p=0;
}
}
return 0;
}
OUTPUT:
Philos name Right fork Left fork
-------------------------------------------------------------------
0 0 1
1 1 2
2 2 3
3 3 4
4 4 0
--------------------------------------------------------------------
Enter the Two Eating Philosophers number:1 3
Round 1
--------------------------
Philosopherre 1 is eating with rhf=1 and lhf=2.
Philosopherre 3 is eating with rhf=3 and lhf=4.
Round 2
------------------------
Philosopherre 0 is eating with rhf=0 and lhf=1.
Philosopherre 2 is eating with rhf=2 and lhf=3.
Round 4
------------------------
Philosopherre 2 is eating with rhf=2 and lhf=3.
Philosopherre 4 is eating with rhf=4 and lhf=0.
Philos name Right fork Left fork
-------------------------------------------------------------------
0 0 1
1 1 2
2 2 3
3 3 4
4 4 0
--------------------------------------------------------------------
Enter the Two Eating Philosophers number:1 4
Round 1
--------------------------
Philosopherre 1 is eating with rhf=1 and lhf=2.
Philosopherre 4 is eating with rhf=4 and lhf=0.
Round 2
------------------------
Philosopherre 0 is eating with rhf=0 and lhf=1.
Philosopherre 2 is eating with rhf=2 and lhf=3.
Round 4
------------------------
Philosopherre 2 is eating with rhf=2 and lhf=3.
Philosopherre 4 is eating with rhf=4 and lhf=0.
Experiment No.7
BANKER’S ALGORITHM
AIM: To implement deadlock avoidance & Prevention by using Banker’s Algorithm.
Bankers Algorithm Description: To determine if a sequence is a safe sequence.
Program:-
#include<stdio.h>
char res[10]={'A','B','C','D','E','F','G','H','I','J'};
void disp(int m,int n,int alloc[5][5],int max[5][5],int avble[5],int need[5][5]);
void safesq(int m,int n,int alloc[5][5],int avble[5],int need[5][5]);
void resreq(int m,int n,int alloc[5][5],int avble[5],int need[5][5]);
void main()
{
int i,j,ch,n,m,work1[5];
int avble[5],alloc[5][5],max[5][5],need[5][5];
printf("\nENTER THE NUMBER OF PROCESSES : \t");
scanf("%d",&n);
printf("\nENTER THE NUMBER OF RESOURCES : \t");
scanf("%d",&m);
printf("\nENTER THE MAXIMUM INSTANCES OF EACH RESOURCE\n");
for(i=0;i<m;i++)
{
printf("\n\tRESOURCE %c:\t",res[i]);
scanf("%d",&avble[i] );
}
printf("\nENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A
RESOURCE\n");
for(i=0;i<n;i++)
{
printf("\n\tFOR PROCESS P%d \n",i);
for(j=0;j<m;j++)
{
printf("\n\tRESOURCE %c : \t",res[j]);
scanf("%d",&max[i][j]);
}
}
printf("\nENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED
TO");
printf(" A PROCESS.\n");
for(i=0;i<n;i++)
{
printf("\n\tFOR PROCESS P%d \n",i);
for(j=0;j<m;j++)
{
printf("\n\tRESOURCE %c : \t",res[j]);
scanf("%d",&alloc[i][j]);
}
}
for(i=0;i<m;i++)
{
work1[i]=0;
for(j=0;j<n;j++)
work1[i]+=alloc[j][i];
avble[i]=avble[i] - work1[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(1)
{
printf("\n\n\tMENU:\n\t1]DISPLAY DATA\n\t2]GENERATE SAFE
SEQUENCE");
printf("\n\t3]RESOURCE REQUEST\n\t4]EXIT\n\tENTER YOUR
CHOICE:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
disp(m,n,alloc,max,avble,need);
break;
case 2:
safesq(m,n,alloc,avble,need);
break;
case 3:
resreq(m,n,alloc,avble,need);
break;
case 4:
exit(0);
default:
printf("\n\tINVALID CHOICE ENTERED.\n");
}
}
}
void disp(int m,int n,int alloc[5][5],int max[5][5],int avble[5],int need[5][5])
{
int i,j;
printf("\n\t\tALLOCATION\tMAX\tNEED\t AVAILABLE");
printf("\t\t");
for(i=0;i<4;i++)
{
for(j=0;j<m;j++)
printf("%c ",res[j]);
printf(" ");
}
for(i=0;i<n;i++)
{
printf("\n\tP%d\t",i);
for(j=0;j<m;j++)
printf("%d ",alloc[i][j]);
printf(" ");
for(j=0;j<m;j++)
printf("%d ",max[i][j]);
printf("\t");
for(j=0;j<m;j++)
printf("%d ",need[i][j]);
printf(" ");
if(i==0)
{
for(j=0;j<m;j++)
printf("%d ",avble[j]);
}
}
}
void safesq(int m,int n,int alloc[5][5],int avble[5],int need[5][5])
{
int i,j,k=0,l,work[5],work1[5],fin[5],flag=0,flag1=0,safesq[6];
for(i=0;i<m;i++)
work[i]=avble[i];
for(i=0;i<n;i++)
fin[i]=0;
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
flag1=0;
if(fin[i]==0)
{
for(j=0; j<m; j++)
{
if(need[i][j] > work[j])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<m;j++)
work[j]=work[j]+alloc[i][j];
fin[i]=1;
safesq[k]=i;
k++;
}
}
}
}
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
printf("\n\tFOR THE GIVEN REQUIREMENT THE SYSTEM IS");
printf(" NOT IN A SAFE STATE.\n");
flag=1;
break;
}
}
if(flag==0)
{
printf("\n\tTHE SAFE SEQUENCE IS:\t");
for(i=0;i<n;i++)
printf("P%d ",safesq[i]);
}
}
void resreq(int m,int n,int alloc[5][5],int avble[5],int need[5][5])
{
int i,j,num,alloc1[5][5],avble1[5],req[5],flag=0,flag1=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
alloc1[i][j] = alloc[i][j];
for(j=0;j<m;j++)
avble1[j]=avble[j];
printf("\n\tENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES:\t");
scanf("%d",&num);
printf("\n\tENTER THE INSTANCE REQUIREMENT OF PROCESS P%d\n",num);
for(i=0;i<m;i++)
{
printf("\n\tRESOURCE %c:\t",res[i]);
scanf("%d",&req[i]);
}
for(i=0;i<m;i++)
{
if(req[i]>need[num][i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(i=0;i<m;i++)
{
if(req[i] > avble1[i])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
avble1[i]=avble1[i]-req[i];
alloc1[num][i]=alloc1[num][i]+req[i];
need[num][i]=need[num][i]-req[i];
}
safesq(m,n,alloc1,avble1,need);
}
else
{
printf("\n\tTHE PROCESS P%d HAS TO WAIT AS RESOURCES ");
printf("ARE NOT AVAILABLE.\n");
}
}
else
printf("\n\t REQUIREMENT EXCEEDS MAXIMUM CLAIM.\n");
}
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
Experiment No.8
FIFO PAGE REPLACEMENT ALGORITHM
AIM: To implement page replacement algorithms
FIFO (First In First Out)
FIFO Page Replacement Algorithm
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults : %d ",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}
OUTPUT:
Enter [Link] frames....4
Enter number of reference string 6
Enter the reference string..
564123
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
[Link] pages faults...6
Experiment No.9
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
Program:-
#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;
}
}
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++)
{
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]);
}
OUTPUT:
Enter [Link] Frames....3
Enter [Link] reference string..6
Enter reference string..6 5 4 2 3 1
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
[Link] page faults...6
Experiment No.10
PAGING
Aim: To implement the Memory management policy- Paging.
Algorithm:
Step 1: Read all the necessary input from the keyboard.
Step 2: Pages - Logical memory is broken into fixed - sized Partitions.
Step 3: Frames – Physical memory is broken into fixed – sized Partitions.
Step 4: Calculate the physical address using the following
Physical address = ( Frame number * Frame size ) + offset
Step 5: Display the physical address.
Step 6: Stop the process.
Program:-
#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d [Link] frames\n",frame);
printf("\nThe Logical memory is divided into %d [Link] pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
getch();
// clrscr();
printf("\n\nPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
// clrscr();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
scanf("%d",&baddr);
printf("\nEnter theLogical Address: ");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
clrscr();
info();
assign();
cphyaddr();
getch();
}
OUTPUT:
MEMORY MANAGEMENT USING PAGING
Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 [Link] frames
The Logical memory is divided into 4 [Link] pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2
PAGE TABLE
PageAddress FrameNo. PresenceBit
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013
Experiment No.11
SEGMENTATION
AIM:
To implement the memory management policy-segmentation.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of segments.
Step 3: get the base address and length for each segment.
Step 4: Get the logical address.
Step 5: check whether the segment number is within the limit, if not display the
error
message.
Step 6: Check whether the byte reference is within the limit, if not display the error
message.
Step 7: Calculate the physical memory and display it.
Step 8: Stop the program.
/*MEMORY SEGMENT TABLE*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int sost;
void gstinfo();
void ptladdr();
struct segtab
{
int sno;
int baddr;
int limit;
int val[10];
}st[10];
void gstinfo()
{
int i,j;
printf("\n\tEnter the size of the segment table: ");
scanf("%d",&sost);
for(i=1;i<=sost;i++)
{
printf("\n\tEnter the information about segment: %d",i);
st[i].sno = i;
printf("\n\tEnter the base Address: ");
scanf("%d",&st[i].baddr);
printf("\n\tEnter the Limit: ");
scanf("%d",&st[i].limit);
for(j=0;j<st[i].limit;j++)
{
printf("Enter the %d address Value: ",(st[i].baddr + j));
scanf("%d",&st[i].val[j]);
}
}
}
void ptladdr()
{
int i,swd,d=0,n,s,disp,paddr;
clrscr();
printf("\n\n\t\t\t SEGMENT TABLE \n\n");
printf("\n\t [Link]\tBASE ADDRESS\t LIMIT \n\n");
for(i=1;i<=sost;i++)
printf("\t\t%d \t\t%d\t\t%d\n\n",st[i].sno,st[i].baddr,st[i].limit);
printf("\n\nEnter the logical Address: ");
scanf("%d",&swd);
n=swd;
while (n != 0)
{
n=n/10;
d++;
}
s = swd/pow(10,d-1);
disp = swd%(int)pow(10,d-1);
if(s<=sost)
{
if(disp < st[s].limit)
{
paddr = st[s].baddr + disp;
printf("\n\t\tLogical Address is: %d",swd);
printf("\n\t\tMapped Physical address is: %d",paddr);
printf("\n\tThe value is: %d",( st[s].val[disp] ) );
}
else
printf("\n\t\tLimit of segment %d is high\n\n",s);
}
else
printf("\n\t\tInvalid Segment Address \n");
}
void main()
{
char ch;
clrscr();
gstinfo();
do
{
ptladdr();
printf("\n\t Do U want to Continue(Y/N)");
flushall();
scanf("%c",&ch);
}while (ch == 'Y' || ch == 'y' );
getch();
}
OUTPUT:
Enter the size of the segment table: 3
Enter the information about segment: 1
Enter the base Address: 4
Enter the Limit: 5
Enter the 4 address Value: 11
Enter the 5 address Value: 12
Enter the 6 address Value: 13
Enter the 7 address Value: 14
Enter the 8 address Value: 15
Enter the information about segment: 2
Enter the base Address: 5
Enter the Limit: 4
Enter the 5 address Value: 21
Enter the 6 address Value: 31
Enter the 7 address Value: 41
Enter the 8 address Value: 51
Enter the information about segment: 3
Enter the base Address: 3
Enter the Limit: 4
Enter the 3 address Value: 31
Enter the 4 address Value: 41
Enter the 5 address Value: 41
Enter the 6 address Value: 51
SEGMENT TABLE
[Link] BASE ADDRESS LIMIT
1 4 5
2 5 4
3 3 4
Enter the logical Address: 3
Logical Address is: 3
Mapped Physical address is: 3
The value is: 31
Do U want to Continue(Y/N)
SEGMENT TABLE
[Link] BASE ADDRESS LIMIT
1 4 5
2 5 4
3 3 4
Enter the logical Address: 1
Logical Address is: 1
Mapped Physical address is: 4
The value is: 11
Do U want to Continue(Y/N)