7. printf("%d",p[k]);
else
printf(" ");
printf("_");
} printf("|"); }
OUTPUT:
Least frequently used
Enter no of frame 3
Enter no of pages 4
Enter page sequence 4 5 8
400F
450F
458F
No of page faults 3
8. 3.Simulate all File Allocation Strategies
a) Sequential file allocation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct
{
int bno,flag;
}block;
block b[200];
void main()
{
int rnum();
int n,p[30],i,j,r,k[20][20],s,s1;
printf("n input");
printf("n enter no of files");
scanf("%d",&n);
printf("n-------------------------------");
printf("n enter memory requirements");
printf("n------------------------------");
for(i=1;i<=n;i++)
{
printf("n enter %d file requirements:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
{
s1=rnum();
j=0;
for(s=s1;s<(s1+p[i]);s++)
{
j=j+1;
b[s].bno=s;
b[s].flag=1;
k[i][j]=s;
}
}
printf("n output");
printf("n ---------------------------------");
printf("n program blocka allocated");
printf("n----------------------------------");
for(i=1;i<=n;i++)
9. {
printf("%5dt",i);
for(j=1;j<=p[i];j++)
printf("%5d",k[i][j]);
printf("'n");
}
printf("n------------------------------------");
printf("n allocated blocks:");
printf("n-----------------------------------");
for(i=1;i<=200;i++)
if(b[i].flag==1)
printf("%5dt",b[i].bno);
}
int rnum()
{
int i,k=0;
for(i=1;i<=200;i++)
{
k=rand()%200;
if(k%2==0)
k=k+10;
if(b[k].flag!=1)
break;
return k;
}
}
OUTPUT:
Enter the no of input files : 3
Input the requirements:
Enter the no of blocks needed in file 1: 2
Enter the no of blocks needed in file2 :4
Enter the no of blocks needed in file3: 3
Allocation
Allocate for file1: 8386
Allocate for file2: 77159335
Allocate for file3:924921
10. b) Indexed File allocation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct block
{
int bno,flag;
};
struct block b[100];
int rnum();
void main()
{
int p[10],r[10][10],ab[10],i,j,n,s;
printf("n INPUT");
printf("enter no of files:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("n enter the size of block %d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
{
s=rnum();
ab[i]=s;
for(j=0;j<p[i];j++)
{
s=rnum();
r[i][j]=s;
}
}
printf("n output:");
for(i=1;i<=n;i++)
{
printf("n file %d n block %d contains:",i,ab[i]);
for(j=0;j<p[i];j++)
{
printf("%6d",r[i][j]);
}
}
}
int rnum()
{
int k=0,i;
for(i=1;i<=100;i++)
{
11. k=rand()%100;
if(b[k].flag!=-1)
break;
}
return k;
}
OUTPUT:
Enter the no of files: 3
Enter the memory requirements:
Enter the 1 file requirement: 3
Enter the 2 file requirement: 2
Enter the 3 file requirement: 4
Program blocks allocated
1 70 71 72
2 115 116
3 212 213 214 215
Allocated blocks
70 71 72 115 116 212 213 214 215
12. c) Linked File Allocation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct
{
int bno,flag,bn[20];
}block;
block b[100],b1;
void main()
{
int rnum();
int p[30],kk[20],i,n,t,s1,s,r,j,c=1;
printf("n enter no of i/p files");
scanf("%d",&n);
printf("n input the requirements");
for(i=1;i<=n;i++)
{
printf("n enter the no of blocks needed for file%d",i);
scanf("%d",&p[i]);
}
t=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=p[i];j++)
{
s=rnum();
b[s].flag=1;
b[c].bno=s;
r=p[i]-1;
kk[i]=s;
t=1;
c++;
}
}
while(r!=0)
{
s1=rnum();
b[s].bn[t]=s1;
b[s1].flag=1;
b[c].bno=s1;
r=r-1;
t=t+1;
c++;
}
printf("n allocation:n");
13. c=1;
for(i=1;i<=n;i++)
{
printf("n allocated for file%d",i);
for(j=1;j<=p[i];j++)
{
if(j==1)
{
printf("%3d",b[c].bno);
c++;
}
else
{
printf("---->%3d",b[c].bno);
c++;
}
}
printf("n");
}
}
int rnum()
{
int k=0,i;
for(i=1;i<=100;i++)
{
k=rand()%100;
if(b[k].flag!=1)
break;
}
return k;
}
OUTPUT:
Enter the no of files 3
Enter the size of block 1 2 3
4 3 2
File 1 Block 83 contains 86 77 15 93
File 2 Block 85 contains 86 92 49
File 3 Block 21 contains 62 27
14. 4. Simulate all File Organization Techniques
a) Single level directory
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd,gm,count,i,j,mid,cir_x;
char fname[10][20];
gd=DETECT;
clrscr();
initgraph(&gd,&gm,"c:/turboc");
cleardevice();
setbkcolor(GREEN);
puts("n Enterno.of files do u have?");
scanf("%d",&count);
for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("Enter file %d name ",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j<=i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[i]);
}
getch();
}
}
28. Banker’s Algorithm for Deadlock avoidance and Prevention
#include<stdio.h>
#include<conio.h>
int C[4][3],A[4][3],RQ[4][3],V[3],R[3],K[4],sum=0,np,nr;
main()
{
void fun();
int i,j,count=0,pcount=0;
clrscr();
printf("nEnter the total number of resources : ");
scanf("n%d",&nr);
for(i=0;i<nr;i++)
{
printf("nEnter the no of resources int R%d : ",i+1);
scanf("%d",&R[i]);
}
printf("nEnter the no of processes to be executed : ");
scanf("%d",&np);
printf("nEnter the claim matrix:n");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&C[i][j]);
printf("nEnter the allocation matrix:n");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&A[i][j]);
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
RQ[i][j] = C[i][j] - A[i][j];
fun();
for(i=0;i<np;i++)
{
count=0;
f(K[i] == i+1)
29. continue;
for(j=0;j<nr;j++)
{
if(V[j] >= RQ[i][j])
count++;
}
if(count == nr)
{
K[i] = i+1;
for(j=0;j<nr;j++)
C[i][j] = A[i][j] = RQ[i][j] = 0;
pcount++;
count = 0;
i=-1;
fun();
}
}
if(pcount == np)
printf("nThere is no chance of deadlock.nIt is a safe state.");
else
printf("nThere is a chance of deadlock.nIt isn't a safe state.");
getch();
}
void fun()
{
int i1,j1;
for(i1=0;i1<nr;i1++)
{
for(j1=0;j1<np;j1++)
{
sum = sum + A[j1][i1];
}
V[i1] = R[i1] - sum;
sum = 0;
}
}
OUTPUT
Enter the total number of resources
2
Enter the total number of resources in R1
30. 4
Enter the total number of resources in R2
5
Enter the total number of processes to be executed
4
Enter the claim matrix
2462
Enter the allocation matrix
1011
There is a chance of deadlock
It is not a safe state