Lab Da-4 Dr. Priya M Rajat Srivastava 19BCE2055
Lab Da-4 Dr. Priya M Rajat Srivastava 19BCE2055
DR. PRIYA M
RAJAT SRIVASTAVA
19BCE2055
a) Consider a memory hole of size 1kb initially. When a sequence of memory request arrives as
following, illustrate the memory allocation by various approaches and calculate the total amount
memory wasted by external fragmentation and internal fragmentation in each approach.
Code -
#include <iostream>
int main()
int c,i,j,k,n,l,m[10],p[10],po[20],flag,z,y,temp,temp1;
for(i=1;i<=n;i++)
po[i]=i;}
switch(c)
case 1:
for(i=1;i<=j;i++)
flag=1; for(k=1;k<=n;k++)
if(p[i]<=m[k])
m[k]=m[k]-p[i]; break;
else
flag++;}}
if(flag>n)
can't be allocated";
break; case 2:
for(y=1;y<=n;y++)
for(z=y;z<=n;z++)
if(m[y]>m[z])
}}
for(i=1;i<=j;i++)
flag=1; for(k=1;k<=n;k++)
if(p[i]<=m[k])
else
flag++;
if(flag>n)
can't be allocated";
break; case 3:
for(y=1;y<=n;y++)
for(z=y;z<=n;z++)
if(m[y]<m[z])
}}
for(i=1;i<=j;i++)
flag=1; for(k=1;k<=n;k++)
if(p[i]<=m[k])
m[k]=m[k]-p[i]; break;
else
flag++;
if(flag>n)
can't be allocated";
break;
}return 0; }
OUTPUT-
(b) Write a program to implement the page replacement algorithms.
CODE-
void getData()
scanf("%d",&n);
for(i=0; imax) {
max=near[j];
repindex=j; } }
p[repindex]=in[i];
pgfaultcnt++;
dispPages();
dispPgFaultCnt(); }
int min=9999;
int repindex;
if(least[j]<min)
min=least[j];
repindex=j;
p[repindex]=in[i];
pgfaultcnt++;
dispPages();
else
dispPgFaultCnt();
int main()
{
int choice;
getData();
while(1)
{ printf("\nPage Replacement
Algorithms\n1.FIFO\n2.Optimal\n3.LRU\n4.Exit\nEnter your
choice:");
scanf("%d",&choice);
switch(choice)
case 1:
fifo();
break;
case 2:
optimal();
break;
case 3:
lru();
break;
default:
return 0;
break;
OUTPUT-
c) Write a program that implements the FIFO, LRU, and optimal pager replacement algorithms. First,
generate a random page-reference string where page numbers range from 0 to 9. Apply the random
page reference string to each algorithm, and record the number of page faults incurred by each
algorithm. Implement the replacement algorithms so that the number of page frames can vary from 1
to 7. Assume that demand paging is used.
Code-
#include <stdio.h>
/* c program which implement FIFO, LRU, and optimal page replacement algorithms */
void fifo();
void optimal();
void lru();
void main()
int i, ch;
scanf("%d", &n);
scanf("%d", &pg[i]);
do
printf("\n\tMENU\n");
printf("\n1)FIFO");
printf("\n2)OPTIMAL");
printf("\n3)LRU");
printf("\n4)Exit");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
case 1: fifo();
break;
case 2: optimal();
break;
case 3: lru();
break;
getchar();
void fifo()
{
f = 0;
r = 0;
s = 0;
flag = 0;
count = 0;
scanf("%d", &psize);
fr[i] = -1;
while (s<n)
flag = 0;
num = pg[s];
if (num == fr[i])
s++;
flag = 1;
break;
if (flag == 0)
if (r<psize)
{
fr[r] = pg[s];
r++;
s++;
count++;
else
if (f<psize)
fr[f] = pg[s];
s++;
f++;
count++;
else
f = 0;
printf("\n");
printf("%d\t", fr[i]);
getchar();
void optimal()
{
int count[10], i, j, k, l, m, p, r, fault, fSize, flag, temp, max, tempflag = 0;
fault = 0;
k = 0;
scanf("%d", &fSize);
count[i] = 0;
fr[i] = -1;
flag = 0;
temp = pg[i];
if (temp == fr[j])
flag = 1;
break;
//if the page is not already exist and frame has empty slot
fault++;
fr[k] = temp;
k++;
fault++;
count[l] = 0;
tempflag = 0;
if (fr[m] == pg[r])
{
if (count[m] == 0)
count[m] = r;
tempflag = 1;
if (tempflag != 1)
count[m] = n + 1;
p = 0;
max = count[0];
if (count[l]>max)
max = count[l];
p = l;
fr[p] = temp;
printf("\n");
printf("%d\t", fr[l]);
}
printf("\nTotal number of faults=%d", fault);
getchar();
void lru()
fault = 0;
dist = 0;
k = 0;
scanf("%d", &f);
flag = 0;
temp = pg[i];
if (temp == fr[j])
flag = 1;
count[j] = i;
break;
}
//if the page is not already exist and frame has empty slot
fault++;
fr[k] = temp;
count[k] = i;
k++;
fault++;
least = count[0];
if (count[m]<least)
least = count[m];
p = m;
fr[p] = temp;
count[p] = i;
p = 0;
printf("\n");
printf("%d\t", fr[x]);
}
getchar();
}
File system and Disk Management
Code-
b) Consider a file of size 1 MB. The size of a disk block is 512Bytes. Assume any number of available
free blocks in the disk contiguously or non-contiguously. Implement the following algorithms to
perform file allocation. Determine the efficiency of each file allocation strategies.
i. Sequential
ii. Indexed
iii. Linked
Code-
#include<stdio.h>
#include<conio.h>
int main()
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
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");
printf("blocks occupiedare:");
for(i=0;i<n;i++) { printf("fileno%d",i+1);
for(j=0;j<b[i];j++) printf("\t%d",c[i][j]);
printf("\n")
;
}
getch(); }
Code-
#include<stdio.h>
#include<conio.h>
int main()
int n,m[20],i,j,ib[20],b[20][20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ib[i]);
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,ib[i],m[i]);
printf("blocks occupiedare:");
for(i=0;i<n;i++)
{ printf("fileno%d",i+1);
for(j=0;j<m[i]; j++)
printf("\t%d--->%d\n",ib[i],b[i][j]);
printf("\n");
getch();