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

OS Lab Manual

The document provides code to simulate various CPU scheduling algorithms including FCFS, SJF, priority, and round robin. It also includes code to simulate memory management concepts including multiprogramming with variable and fixed number of tasks. The code implements functions for fork(), wait(), exec(), and exit() system calls to demonstrate multiprocessing.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

OS Lab Manual

The document provides code to simulate various CPU scheduling algorithms including FCFS, SJF, priority, and round robin. It also includes code to simulate memory management concepts including multiprogramming with variable and fixed number of tasks. The code implements functions for fork(), wait(), exec(), and exit() system calls to demonstrate multiprocessing.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

OPERATING SYSTEMS LAB MANUAL

1) Simulate the following CPU scheduling algorithms


a)
b)
c)
d)

FCFS
SJF
Priority
Round Robin

a) FCFS:
AIM: A program to simulate the FCFS CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,ta;
float nta;
};
main()
{
struct process p[20];
int n,i,j;
float tamean=0,ntamean=0;
clrscr();
printf("\nEnter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c :: ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for Process-%c :: ",65+i);
scanf("%d",&p[i].ts);
}
for(i=0;i<n;i++)
{
if(i==0)
p[i].st=p[i].at;
else
{
p[i].st=0;
for(j=0;j<i;j++)
p[i].st=p[i].st+p[j].ts;
}
p[i].ft=p[i].ts+p[i].st;
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)(tamean/n);
1

III-I R-13

OPERATING SYSTEMS LAB MANUAL

III-I R-13

ntamean=(float)(ntamean/n);
printf("\nProcess AT ST StT FT TA NTA");
for(i=0;i<n;i++)
printf("\n%3c%12d%10d%10d%10d%10d
%15f",65+i,p[i].at,p[i].ts, p[i].st,p[i].ft,p[i].ta,p[i].nta);
printf("\n\n Mean of Turn-around time : %f",tamean);
printf("\n\n Mean of Normalized turn-around time : %f",ntamean);
getch();
}
Output:
Enter Number of Processes:: 3
Enter Arrival Time for Process-A :: 0
Enter Service Time for Process-A :: 12
Enter Arrival Time for Process-B :: 2
Enter Service Time for Process-B :: 9
Enter Arrival Time for Process-C :: 6
Enter Service Time for Process-C :: 14
Process
A
B
C

AT
0
2
6

ST
2
9
14

StT
FT
TA
NTA
0
12
12
1.000000
12
21
19
2.111111
21
35
29
2.071429

Mean of Turn-around time: 20.000000


Mean of Normalized turn-around time: 1.7275

OPERATING SYSTEMS LAB MANUAL

III-I R-13

b) SJF: AIM: A program to simulate the SJF CPU scheduling algorithm


#include<stdio.h>
#include<conio.h>
main()
{
int sbt[10],swt[10],st[10],stt[10],sft[10],n,i,j,wt,tt,temp;
float avgwt,avgtt;
wt=0;tt=0;temp=0;st[1]=0;
clrscr();
printf("enter no.of jobs");
scanf("%d",&n);
printf("enter the burst times of jobs");
for(i=1;i<=n;i++)
scanf("%d",&sbt[i]);
for(i=1;i<=(n-1);i++)
for(j=i+1;j<=n;j++)
if((sbt[i]>sbt[j])&&(sbt[i]!=sbt[j]))
{
temp=sbt[i];
sbt[i]=sbt[j];
sbt[j]=temp;
}
for(i=1;i<=n;i++)
{
st[i+1]=st[i]+sbt[i];
sft[i]=st[i]+sbt[i];
if(i==1)
swt[i]=0;
else
swt[i]=swt[i-1]+sbt[i-1];
stt[i]=swt[i]+sbt[i];
wt=wt+swt[i];
tt=tt+stt[i];
}
avgwt=((float)wt/(float)n);
avgtt=((float)tt/(float)n);
printf("\nJOB sert
st
wt
ft
turt");
for(i=1;i<=n;i++)
printf("\nJ%d\t%d\t%d\t %d\t%d\t%d\n",i,sbt[i],st[i],sft[i],swt[i],stt[i]);
printf("\navg waiting time=%0.2f,turnover total
time=%0.2f",avgwt,avgtt);
getch();
}

Output:
enter no.of jobs3
enter the burst times of jobs4

JOB sert
st
wt
ft
turt
J1
4
0
4
0
4
J2
5
4
9
4
9
J3
6
9
15
9
15
avg waiting time=4.33,turnover total time=9.33

c) Priority:
AIM: A program to simulate the priority CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct process
{
int ts,pri,wait,ft;
}p[20];
main()
{
int n,pri1[20],i,j,temp,ft1[25];
clrscr();
printf("\n Enter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Service Time for Process-%c : ",65+i);
scanf("%d",&p[i].ts);
printf("\nEnter Priority for Process-%c : ",65+i);
scanf("%d",&p[i].pri);
}
for(i=0;i<n;i++)
pri1[i]=p[i].pri;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pri1[i]>pri1[j])
{
temp=pri1[i];
pri1[i]=pri1[j];
pri1[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri1[i]==p[j].pri)
{
if(i==0)
{
p[j].wait=0;
p[j].ft=p[j].ts;
ft1[i]=p[j].ft;
}
else
{
p[j].ft=ft1[i-1]+p[j].ts;
p[j].wait=ft1[i-1];
ft1[i]=p[j].ft;

}
}
}
}
printf("\nProcess ST
PRI
FT WT ");
for(i=0;i<n;i++)
{
printf("\nprocess-%c%10d%13d%14d
%15d",65+i,p[i].ts,p[i].pri,p[i].ft,p[i].wait);
}
getch();
}
Output:
Enter Number of Processes:: 3
Enter Service Time for Process-A : 5
Enter Priority for Process-A : 2
Enter Service Time for Process-B : 8
Enter Priority for Process-B : 1
Enter Service Time for Process -C : 6
Enter Priority for Process-C : 3
Process
process-A
process-B
process-C

ST
5
8
6

PRI
2
1
3

FT
13
8
19

WT
8
0
13

d) Round Robin:
AIM: A program to simulate the Round Robin CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,wait,ts2,ta;
float nta;
}p[20];
main()
{
int i,j,slice,n;
float tamean=0,ntamean=0;
clrscr();
printf("Enter Number of Processes :: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c : ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for process-%c : ",65+i);
scanf("%d",&p[i].ts);
}
printf("\nEnter Time Slice: ");
scanf("%d",&slice);
for(i=0;i<n+1;i++)
{
if(i==0)
p[i].ts2=n*slice;
else
p[i].ts2=p[i-1].ts2+(p[i-1].ts-slice);
if(i<n)
p[i].st=i*slice;
if(i>=1)
p[i-1].ft=p[i].ts2;
}
for(i=0;i<n;i++)
p[i].wait=(i*slice-p[i].at)+(p[i].ts2-(i+1)*slice);
for(i=0;i<n;i++)
{
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;

}
tamean=(float)tamean/n;
ntamean=(float)ntamean/n;
printf("\n Process AT ST StT FT WT TA NTA\n");
for(i=0;i<n;i++)
{
printf("Process-%c%9d%9d%12d%12d%10d%6d
%10.4f",65+i,p[i].at,p[i].ts,p[i].st,p[i].ft,p[i].wait,p[i].ta,p[i].nta);
printf("\n");
}
printf("\nturn around mean is : %f",tamean);
printf("\nnorm.turn around mean is : %f",ntamean);
getch();
}
Output:
Enter Number of Processes:: 3
Enter Arrival Time for Process -A : 0
Enter Service Time for process-A : 5
Enter Arrival Time for Process -B : 3
Enter Service Time for process-B : 7
Enter Arrival Time for Process -C : 5
Enter Service Time for process-C : 9
Enter Time Slice: 4
Process
AT
ST
Process-A
Process-B
Process-C

0
3
5

5
7
9

StT
0
4
8

FT
13
16
21

WT

TA
8
6
7

NTA
13
13
16

2.6000
1.8571
1.7778

turn around mean is : 14.000000


norm.turn around mean is : 2.078307
Experiment 2:
Multiprogramming-Memory management implementation of fork(),wait(),exec() & exit() system calls
PROGRAM :
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#define MAX_COUNT 200
#define BUF_SIZE 100
void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
fork();
pid = getpid();
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
write(1, buf, strlen(buf));
}
}
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 200
void ChildProcess(void);
void ParentProcess(void);

/* child process prototype */


/* parent process prototype */

void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;

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


printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}

Experiment 3:
Simulate the MVT and MFT.
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)
{
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;
}
}
}
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();
}

Output:

1
2
3
4
5

Enter time: 100


Enter no. of jobs: 5
Enter job information 1.jobid 2.jobsize
20
25
15
30
15
Total memory space available which is not allocated is: 10

Jobs which are not allocated: 5 15


Enter jobid to deallocate: 1
Deallocated job: 1 20
Remaining memory is: 15
Jobs which are not allocated are:
Do you want to deallocate 1.Yes 2.No : 1
Enter jobid to deallocate: 4
Deallocated job: 4 30
Remaining memory is 45
Jobs which are not allocated are:
Do you want to deallocate 1.Yes 2.No : 2
Allocated jobs are
2
25
3
15
5
15
Multiple Programming with Fixed Number of Tasks (MFT):
#include<stdio.h>
#include<conio.h>
void main()
{
int tms,element,nb,i,j,t,index,frag,ch,count=0;
static int jobs[20][20],sz[20][20],nj,s;
clrscr();
printf("enter total memory space"); /* reading memory */
scanf("%d",&tms); /* reading choices */
printf("enter choice\n1.equal partition 2.unequal partition\n");
scanf("%d",&ch);
if(ch==1)
{
printf("enter size of each block");
scanf("%d",&s);
nb=tms/s;
for(i=0;i<nb;i++)
scanf("%d",&sz[i][0]);
}
else
{
printf("enter no. of blocks");
scanf("%d",&nb);
printf("enter size of %d blocks");
for(i=0;i<nb;i++)

scanf("%d",sz[i][0]);
}
printf("enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("enter job information 1.jobid 2.job size\n");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
frag=0;
for(j=0;j<nj;j++)
{
if(sz[j][0]>=element && sz[i][0]<=t)
{
if(sz[j][1]!=1)
{
t=sz[j][0];
index=j;
}
}
}

if(sz[index][1]!=1)
{
sz[index][1]=1;
jobs[i][2]=2;
frag=frag+(t-element);
count++;
}
printf("total internal fragmentation : %d", frag);
printf("no. of free blocks: %d" , nb-count);
printf("the jobs which are not allocated");
if(count==nj)
printf(" 0");
for(i=0;i<nj;i++)
{
if(jobs[i][2]!=2)
printf("jobid ------%d\tjob size-----%d\n",jobs[i][0],jobs[i][1]);
}
getch();
}

Output:
Enter total memory space: 100
enter choice 1. equal partition 2. unequal partition 2
enter no. of blocks: 3
enter size of 3 blocks: 50 25 25
enter no. of jobs 4
enter job information 1.jodid 2. jobsize
1 25
2 30
3 26
4 20
5 25
total internal fragmentation : 9
no. of free blocks : 0
the jobs which are not allocated :
job id----4 jobsize----20
jobid----5
jobsize----25

Experiment 4:
Simulate Bankers algorithm for deadlock avoidance
Aim: To simulate bankers algorithm for deadlock avoidance
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],c[10][10],r[10],av[10],ca[10][10],i,j,k,n,m,temp=0,tem,ch;
clrscr();
printf("enter no processes");
scanf("%d",&m);
printf("enter no of resources");
scanf("%d",&n);
printf("\nenter claim\n");

for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&c[i][j]);
printf("\nenter alocation matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nenter resourse vector\n");
for(i=0;i<n;i++)
scanf("%d",&r[i]);
k=0;
do
{
printf("claim\tallocation\tca\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",c[i][j]);printf("\t");
for(j=0;j<n;j++)
printf("%3d",a[i][j]);printf("\t\t");
for(j=0;j<n;j++)
{
ca[i][j]=c[i][j]-a[i][j];
printf("%3d",ca[i][j]);
}
printf("\n");
}
temp=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
temp=temp+a[j][i];
av[i]=r[i]-temp;temp=0;
}
printf("\nresource vector: ");
for(i=0;i<n;i++)
printf("%3d",r[i]);
printf("\navailable vector: ");
for(i=0;i<n;i++)
printf("%3d",av[i]);
if(k==0)
printf("\n****initial state****\n");
else
printf("\n***p%d runs to completion*****\n",tem+1);

if(k<n)
{
temp=0;
for(i=0;i<m;i++)
{
lab:for(j=0;j<n;j++)
{
if(ca[i][j]==0)
temp++;
}
if(temp==n)
{
i++; temp=0;goto lab;
}
else
{
for(j=0;j<n;j++)
{
if(ca[i][j]<=av[j])
tem=i;
else
{
if(i>m)
{
printf("\nunshafe state");goto end;
}
else
{i++;goto lab;}
}
}
}
for(j=0;j<n;j++)
{
a[tem][j]=0;c[tem][j]=0;ca[tem][j]=0;
}
break;
}
}
else
{
printf("\nprocesses are completed");
goto end;
}
k++;

printf("continue press ZERO");


scanf("%d",&ch);
}while(ch==0);
end: getch();
}
Output:
enter no processes 3
enter no of resources 3
enter claim
322
613
314
enter allocation matrix
100
612
211
enter resource vector
936
claim
allocation
3 2 2
1 0 0
6 1 3
6 1 2
3 1 4
2 1 1

ca
2 2 2
0 0 1
1 0 3

resource vector: 9 3 6
available vector: 0 1 3
****initial state****
continue press ZERO 0
enter allocation matrix
100
612
211
enter resource vector
936
claim
allocation
ca
3 2 2
1 0 0
2 2 2
6 1 3
6 1 2
0 0 1
3 1 4
2 1 1
1 0 3
resource vector: 9 3 6
available vector: 0 1 3
****initial state****
continue press ZERO 0

claim
allocation
ca
3 2 2
1 0 0
2 2 2
0 0 0
0 0 0
0 0 0
3 1 4
2 1 1
1 0 3
resource vector: 9 3 6
available vector: 6 2 5
***p2 runs to completion*****
continue press ZERO 0
claim
allocation ca
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
3 1 4
2 1 1
1 0 3
resource vector: 9 3 6
available vector: 7 2 5
***p1 runs to completion*****
continue press ZERO0
claim allocation ca
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
resource vector: 9 3 6
available vector: 9 3 6
***p3 runs to completion*****
processes are completed

Experiment 5:
Simulate Bankers Algorithm for Deadlock prevention
Aim: To simulate Bankers Algorithm for Deadlock Prevention
Program:
#include<stdio.h>
#include<conio.h>
int max[10][10],alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0;
main( )
{
clrscr( );
printf("\n\nSIMULATION OF DEADLOCK PREVENTION");
printf("Enter no. of processes, resources");
scanf("%d%d",&p,&r);
printf("Enter allocation matrix");

for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter max matrix");
for(i=0;i<p;i++)
/*reading the maximum matrix and availale matrix*/
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("enter available matrix");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun();
/*calling function*/
if(flag==0)
{
if(finish[i]!=1)
{
printf("\n\n Failing :Mutual exclusion");
for(j=0;j<r;j++)
{ /*checking for mutual exclusion*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("\n By allocating required resources to process %d dead lock is prevented ",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("\n\n daed lock is prevented by allocating needed resources");
printf(" \n \n failing:Hold and Wait condition ");
for(j=0;j<r;j++)
{
/*checking hold and wait condition*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun( );
printf("\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT DEADLOCK");
}

}
getch( );
}
fun( )
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else
break;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
}

Output:
SIMULATION OF DEADLOCK PREVENTION
Enter no. of processes, resources 3, 2
enter allocation matrix 2 4 5
4 5
Enter max matrix4 3 4
5 6 1
Enter available matrix2

5
Failing : Mutual Exclusion
by allocating required resources to process dead is prevented
Lack of no preemption
deadlock is prevented by allocating needed resources
Failing : Hold and Wait condition
BY AVOIDING ONE OF THE ABOVE CONDITION, YOU CAN PREVENT DEADLOCK

Experiment 6:
Simulate all Page Replacement Algorithms
a) FIFO
b) LRU
c) LFU
a) FIFO:
AIM: A program to simulate FIFO Page Replacement Algorithm

FIFO PAGE REPLACEMENT ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k=0,flg1,flg2,x=5,y,pfc=0;
clrscr();
printf("\n enter page reference string:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
fp[i]='x';
clrscr();
printf("\n page replacement technique :: FIFO algorithm:");
printf("\n .............................................");
printf("\n F-Page Fault \t H- Page Hit \n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
if(fp[j]==prs[i]){
ps='H';
flg1=1;
break;
}
if(flg1==0)
{
flg2=0;
for(j=0;j<fs;j++)
if(fp[j]=='x')
{
fp[j]=prs[i];
pfc++;
flg2=1;
break;
}
if(flg2==0)
{
pfc++;
fp[k]=prs[i];
k++;

if(k==fs)
k=0; }
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("--");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n \n\n\n\n Total page Faults=%d",pfc);
getch();
}

OUTPUT
enter page reference string :
232152453252
enter the frame size:
3
page replacement technique:: FIFO algorithm
................................................................................
F-page fault H-page hit
2 3 2 1 5 2 4 5 3 2 5 2
---------------------------2 2 2 2 5 5 5 5 3 3 3 3
x 3 3 3 3 2 2 2 2 2 5 5
x x x 1 1 1 4 4 4 4 4 2
--------------------------F FHFF F F H F H F F
total page faults=9

LFU PAGE REPLACEMENT ALGORITHM


#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("\n ENTER THE PAGE REFERENCE STRING:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='X';
ru[i]=0;
}
clrscr();
printf("\n PAGE REPLACEMENT TECHNIQUE ::LFU ALGORITHM \n");
printf("\n .......................................... \n");
printf("F-Page Fault \t H-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=5)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
(ru[j])++;
flg1=1;
break;
}
}
if(flg1==0)

{
pfc++;
flg2=0;

for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=1;
flg2=1;
break;
}
}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>=ru[j])
{
if(ru[min]>ru[j])
min=j;
else
{
for(k=0;k<i;k++)
{
if(prs[k]==fp[min])
break;
if(prs[k]==fp[j])
{
min=j;
break;
}}}}}
fp[min]=prs[i];
ru[min]=1;
}
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);

y++;
gotoxy(x,y);
printf("-----");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c(%d)",fp[j],ru[j]);
}

y++;
gotoxy(x,y);
printf("-----");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n TOTAL PAGE FAULTS =%d",pfc);
getch();
}

OUTPUT :
enter the page referrence string :232152453252
enter frame size :3

2
3
2
1
5
2
4
5
3
2
5
2
----------------------------------------------------------------------------------------------------2(1)
2(1)
2(2)
2(2)
2(2)
2(3) 2(3) 2(3) 2(3) 2(4) 2(4) 2(5)
X(0)
3(1)
3(1)
3(1)
5(1)
5(1) 5(2) 5(2) 5(2) 5(2) 5(3) 5(3)
X(0)
X(0)
X(0) 1(1)
1(1)
1(1) 4(1) 4(1) 3(1) 3(1)
3(1) 3(1)
----------------------------------------------------------------------------------------------------F
F
H
F
F
H
F
H
F
H
H
H
TOTAL PAGE FAULTS = 6

LRU PAGE REPLACEMENT ALGORITHM


#include<dos.h>
#include<stdio.h>

#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("enter the page reference string:");
gets(prs);
printf("enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='x';
ru[i]=0;
}
clrscr();
printf("PAGE REPLACEMENT TECHNIQUE::LRU algorithm\n");
printf("-----------------------------------\n");
printf("F-Page fault\tH-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
ru[j]=i;
flg1=1;
break;
}
}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=i;
flg2=1;

break;
}
}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>ru[j])
min=j;
}
fp[min]=prs[i];
ru[min]=i;
}
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("- -");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);
getch();
}
OUTPUT:
enter the page referrence string :232152453252
enter frame size :3
2 3 2 1 5 2 4 5 3 2 5 2
--------------------------------------------2 3 3 3 5 5 5 5 5 5 5 5
X X 2 2 2 2 2 2 3 3 3 3
X X X 1 1 1 4 4 4 2 2 2
---------------------------------------------

F F F F F H F H F F H H
TOTAL NO OFPAGE FAULTS=8

Experiment 7:
Simulate all file allocation strategies
a. Sequential
b. Indexed
c. Linked
a. Sequential file Allocation
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int st[20],b[20],b1[20],ch,i,j,n,blocks[20][20],sz[20];
char F[20][20],S[20];
clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter Starting block of %d::",i+1);
scanf("%d",&st[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
b1[i]=(sz[i]*1024)/b[i];
for(i=0;i<n;i++)
{
for(j=0;j<b1[i];j++)
blocks[i][j]=st[i]+j;

}
do
{
printf("\nEnter the Filename ::");
scanf("%s",S);
for(i=0;i<n;i++)
{
if(strcmp(S,F[i])==0)
{
printf("\nFname\tStart\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t",F[i],st[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue ::(Y:n)");
scanf("%d",&ch);
if(ch!=1)
break;
}while(1);
}
/*Input and Output;Enter no. of Files ::2
Enter file 1 name ::x.c
Enter file1 size(in kb)::4
Enter Starting block of 1::100
Enter blocksize of File1(in bytes)::512
Enter file 2 name ::y.c
Enter file2 size(in kb)::2
Enter Starting block of 2::500
Enter blocksize of File2(in bytes)::256
Enter the Filename ::y.c
Fname Start Nblocks Blocks
--------------------------------------------y.c 500 8
500->501->502->503->504->505->506->507->
--------------------------------------------Do U want to continue ::(Y:n) n
*/

b. Index file Allocation Method


PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n\nEnter blocks for file%d",i+1);
for(j=0;j<b1[i];j++)
{
printf("\n Enter the %dblock ::",j+1);
scanf("%d",&blocks[i][j]);
}
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",&S);
for(i=0;i<n;i++)
{

if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue ::(Y:n)");
scanf("%d",&ch);
}while(ch!=0);
}
Input and Output:
Enter no. of Files ::2
Enter file 1 name ::x.c
Enter file1 size(in kb)::1
Enter blocksize of File1(in bytes)::512
Enter file 2 name ::y.c
Enter file2 size(in kb)::1
Enter blocksize of File2(in bytes)::512
Enter blocks for file1
Enter the 1block ::1000
Enter the 2block ::1001
Enter blocks for file2
Enter the 1block ::2000
Enter the 2block ::2001
Enter the Filename ::x.c

Fname Fsize Bsize Nblocks Blocks


--------------------------------------------x.c 1
512 2
1000->1001->
--------------------------------------------Do U want to continue ::(Y:n)1
Enter the Filename ::y.c
Fname Fsize Bsize Nblocks Blocks
--------------------------------------------y.c 1
512 2
2000->2001->
--------------------------------------------Do U want to continue ::(Y:n)0
c. Linked file Allocation Method
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
int sb[20],eb[20],x;
clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n Enter Starting block of file%d::",i+1);

scanf("%d",&sb[i]);
printf("\n Enter Ending block of file%d::",i+1);
scanf("%d",&eb[i]);
printf("\nEnter blocks for file%d::\n",i+1);
for(j=0;j<b1[i]-2;)
{
printf("\n Enter the %dblock ::",j+1);
scanf("%d",&x);
if(x>sb[i]&&x<eb[i])
{
blocks[i][j]=x;
j++;
}
else
printf("\n Invalid block::");
}
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",&S);
for(i=0;i<n;i++)
{
if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
printf("%d->",sb[i]);
for(j=0;j<b1[i]-2;j++)
printf("%d->",blocks[i][j]);
printf("%d->",eb[i]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue (Y:n)::");
scanf("%d",&ch);
}while(ch!=0);
}
/*Input and Output;Enter file1 size(in kb)::1
Enter blocksize of File1(in bytes)::512

Enter file 2 name ::sudee


Enter file2 size(in kb)::1
Enter blocksize of File2(in bytes)::1024
Enter Starting block of file::1100
Enter Ending block of file::1600
Enter blocks for file1::
Enter the 1block ::102
Enter the 2block ::104
Enter Starting block of file::2200
Enter Ending block of file::2500
Enter blocks for file2::
Enter the 1block ::201
Enter the Filename ::daya
Fname Fsize Bsize Nblocks Blocks
--------------------------------------------daya 1
512 2
100->102->104->600->
--------------------------------------------Do U want to continue ::(Y:n)1
Enter the Filename ::sudee
Fname Fsize Bsize Nblocks Blocks
--------------------------------------------sudee 1
1024 1
200->201->500->
--------------------------------------------Do U want to continue ::(Y:n)0

You might also like