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

os 8-14

The document contains multiple C programs related to memory management techniques, including page replacement algorithms and file allocation methods. It covers implementations for FIFO, LRU, and contiguous allocation strategies, along with user interactions for inputting reference strings, frames, and file sizes. Additionally, it includes output results for various test cases demonstrating the functionality of the programs.

Uploaded by

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

os 8-14

The document contains multiple C programs related to memory management techniques, including page replacement algorithms and file allocation methods. It covers implementations for FIFO, LRU, and contiguous allocation strategies, along with user interactions for inputting reference strings, frames, and file sizes. Additionally, it includes output results for various test cases demonstrating the functionality of the programs.

Uploaded by

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

311521205024

PROGRAM

#include <stdio.h>
main() {
int i,j,l,rs[50],frame[10],nf,k,avail,count=0;

printf("Enter length of ref. string : ");


scanf("%d", &l);
printf("Enter reference string :\n");
for(i=1; i<=l; i++)
scanf("%d", &rs[i]);
printf("Enter number of frames : ");
scanf("%d", &nf);

for(i=0; i<nf; i++) frame[i] = -1; j=0;


printf("\nRef. str Page frames");
for(i=1; i<=l; i++)
{
printf("\n%4d\t", rs[i]); avail = 0; for(k=0; k<nf;
k++) if(frame[k] == rs[i]) avail = 1; if(avail == 0)

{
frame[j] = rs[i]; j = (j+1) % nf; count++; for(k=0; k<nf; k++)
printf("%4d", frame[k]);
}
}
printf("\n\nTotal no. of page faults : %d\n",count);
}
311521205024

OUTPUT

RESULT:
311521205024

PROGRAM

#include<stdio.h>

int findLRU(int time[], int n){


int i, minimum = time[0], pos = 0;

for(i = 1; i< n; ++i){


if(time[i] <minimum){
minimum = time[i]; pos =
i;
}
}
return pos; }

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1,
flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i<no_of_pages; ++i)
{
scanf("%d", &pages[i]); }
for(i = 0; i<no_of_frames; ++i){ frames[i] = -1;
}
311521205024

for(i = 0; i<no_of_pages; ++i){ flag1 = flag2 = 0;


for(j = 0; j <no_of_frames; ++j){
if(frames[j] == pages[i]){ counter++; time[j] = counter;
flag1 = flag2 = 1;
break; } }
if(flag1 == 0){
for(j = 0; j <no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i]; time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){ pos = findLRU(time, no_of_frames);
counter++; faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j <no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return;

OUTPUT:

RESULT:
311521205024

Program
#include<stdio.h>
#include<conio.h>
int fr[
3], n, m;
void
display();
void main()
{
int i,j,page[20],fs[10];
int
max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;
float pr;
clrscr();
printf("Enter length of the reference string: ");
scanf("%d",&n);
printf(
"Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter no of frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
fr[i]=
-
1; pf=m;
Page 36 for(j=0;j<n;j++)
{
flag1=0; flag2=0;
for(i=0;i<m;i++)
{
if(fr[i]==page[j])
{
flag1=1; flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
311521205024

{
if(fr[i]==
-
1)
{
fr[i]=page[j]; flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<m;i++)
lg[i]=0;
for(i=0;i<m;i++)
{
for(k=j+1;k<=n;k++)
{
if(fr[i]==page[k])
{
lg[i]=k
-
j;
break;
}
}
}
found=0;
for(i=0;i<m;i++)
{
if(lg[i]==0)
{
index=i;
found =
1;
Page 37 break;
}
}
if(found==0)
{
max=lg[0]; index=0;
for(i=0;i<m;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
311521205024

fr[index]=page[j];
pf++;
}
display();
}
printf("Number of page faults :
%d
\
n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f
\
n", pr); getch();
}
void display()
{
int i; for(i=0;i<m;i++)
printf("%d
\
t",fr[i]);
printf("
\
n");
}

OUTPUT
Enter length of the reference string: 12
Enter the reference
string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1
123
124
124
124
125
125
125
325
425
425
Number of page faults : 7 Page fault rate = 58.333332

RESULT:
311521205024

PROGRAM

#include<stdio.h>

int main()

int fragments[10], blocks[10], files[10];

int m, n, number_of_blocks, number_of_files, temp, top =


0; static int block_arr[10], file_arr[10];

printf("\nEnter the Total Number Of Blocks:\t")


Scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m <number_of_blocks; m++)
{

printf("Block No.[%d]:\t", m + 1);

scanf("%d", &blocks[m]);

printf("Enter the Size of the Files:\n")


printf("File No.[%d]:\t", m + 1);

scanf("%d", &files[m]);

for(m = 0; m <number_of_files; m++)

for(n = 0; n <number_of_blocks; n++)

for(m = 0; m <number_of_files; m++)

}
311521205024

if(block_arr[n] != 1)

{
temp = blocks[n] - files[m];

if(temp >= 0)

if(top < temp)

file_arr[m] = n;
top = temp;

fragments[m] = top;

block_arr[file_arr[m]] = 1;
top = 0;

printf("\nFile Number\tFile Size\tBlock Number\tBlock


Size\tFragment"); for(m = 0; m <number_of_files; m++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]);

printf("\n");

return 0;

}
311521205024

OUTPUT :

RESULT:
311521205024

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h> void
main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10]; clrscr();
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf); do
{ printf("Enter file name to be created:");
scanf("%s",name); for(i=0;i<nf;i++)
{ if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{ strcpy(fname[j++],name); nf++;
}
else
printf("There is already %s\n",name); printf("Do you want to enter
another file(yes - 1 or no - 0):"); scanf("%d",&ch);
} while(ch==1); printf("Directory name
is:%s\n",mdname); printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]); getch();
}
311521205024

OUTPUT

RESULT
311521205024

PROGRAM
#include<stdio.h> #include<conio.h>
struct st

{
char dname[10]; char
sdname[10][10]; char
fname[10][10][10]; int
ds,sds[10]; }dir[10]; void
main()
{ int i,j,k,n; printf("enter number of
directories:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname); printf("enter
size of directories:"); scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:"); scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{printf("%s\t\t%d",dir[i].dname,dir[i].ds); for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]); printf("\n\t\t");
}
printf("\n"); }
getch();
311521205024

OUTPUT

RESULT:
311521205024

PROGRAM
#include <stdio.h>

#include <string.h>

int num=0, length[10], start[10]; char fid[20][4], a[20][4];

void directory()

{ int i;

printf("\nFile Start Length\n"); for(i=0; i<num; i++)


printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);
}
void display()

{ int i; for(i=0; i<20; i++) printf("%4d",i);


printf("\n"); for(i=0;
i<20; i++) printf("%4s", a[i]);
} main() { int i,n,k,temp,st,nb,ch,flag; char
id[4];

for(i=0; i<20; i++) strcpy(a[i], ""); printf("Disk space before


allocation:\n"); display(); do
{
printf("\nEnter File name (max 3 char) : "); scanf("%s", id); printf("Enter start block : ");
scanf("%d", &st); printf("Enter no. of blocks : ");
scanf("%d", &nb); strcpy(fid[num], id); length[num] = nb; flag = 0;

if((st+nb) > 20)


{
printf("Requirement exceeds range\n"); continue; }

for(i=st; i<(st+nb); i++) if(strcmp(a[i], "") != 0) flag = 1;


if(flag == 1)
{
printf("Contiguous allocation not possible.\n"); continue;
} start[num] = st; for(i=st; i<(st+nb); i++) strcpy(a[i],
id);; printf("Allocation done\n"); num++;
printf("\nAny more allocation (1. yes / 2. no)? : ");
scanf("%d", &ch);
}
while (ch == 1);
printf("\n\t\t\tContiguous Allocation\n");
printf("Directory:");
directory();
printf("\nDisk space after allocation:\n"); display();
311521205024

OUTPUT

RESULT
311521205024

PROGRAM
#include <stdio.h>
#include <conio.h> #include <stdlib.h> void
recursivePart(int pages[]){ int st, len, k, c, j;

printf("Enter the index of the starting block and its length:


"); scanf("%d%d", &st, &len); k = len; if
(pages[st] == 0){ for (j = st; j < (st + k); j++){
if (pages[j] == 0){ pages[j] = 1;

printf("%d ----- >%d\n", j, pages[j]);


} else {
printf("The block %d is already allocated \n", j); k++;
}}}
else
printf("The block %d is already allocated \n", st);
printf("Do you want to enter more files? \n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &c);
if (c==1)
recursivePart(pages);
else
exit(0);
return;
}int
main;
int pages[50], p, a;
for (int i = 0; i< 50; i++)
pages[i] = 0;
printf("Enter the number of blocks already allocated:
");
scanf("%d", &p);
printf("Enter the blocks already allocated: ");
for (int i = 0; i< p; i++){
scanf("%d", &a);
pages[a] = 1;

}
recursivePart(pages);
getch();
return 0;

}
311521205024

OUTPUT

RESULT
311521205024

PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h> int files[50], indexBlock[50], indBlock, n; void
recurse1(); void recurse2(); void recurse1(){

printf("Enter the index block: "); scanf("%d", &indBlock); if (files[indBlock] != 1){


printf("Enter the number of blocks and the number of files needed for the index %d on the
disk: ", indBlock); scanf("%d", &n);
}
else{ printf("%d is already allocated\n",
indBlock); recurse1();
}
recurse2();
}
void recurse2(){ int ch; int
flag = 0; for (inti=0; i<n;
i++){ scanf("%d",
&indexBlock[i]); if
(files[indexBlock[i]] == 0)
flag++;
}
if (flag == n){ for (int j=0;
j<n; j++){
files[indexBlock[j]] = 1;

}
printf("Allocated\n"); printf("File Indexed\n");
for (int k=0; k<n; k++){
printf("%d ------ > %d : %d\n", indBlock, indexBlock[k], files[indexBlock[k]]);
311521205024

}
}
else{ printf("File in the index is already
allocated\n"); printf("Enter another indexed
file\n"); recurse2();
}
printf("Do you want to enter more files?\n");
printf("Enter 1 for Yes, Enter 0 for No:
"); scanf("%d", &ch); if (ch == 1)
recurse1(); else exit(0); return;

}
Int main()
{
for(inti=0;i<50;i++)
files[i]=0;
recurse1(); return
0;
}

OUTPUT

RESULT
311521205024

Program:
#include<stdio.h>
main()
{
int t[20], n, I, j, tohm[20], tot=0; float avhm;
clrscr();
printf(“enter the no.of tracks”);
scanf(“%d”,&n);
printf(“enter the tracks to be traversed”);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”);
for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
printf("\nAverageheadermovements:%f",avhm);

getch();
311521205024

Result:
311521205024

Program:
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
311521205024

}}}
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j]; j--;
p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch();}
311521205024

OUTPUT

Result:
311521205024

Program:
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;
printf("enter the tracks");
for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])
{
temp=t[j];
311521205024

t[j]=t[j+1];
t[j+1]=temp
}
for(i=0;i<=n+2;i++)
if(t[i]==h);
j=i;break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i]; i++;
p++;
}

for(j=0;j<n+2;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
311521205024

printf("total header movements%d",sum);


printf("avg is %f",(float)sum/n);
getch();
}

OUTPUT

Result:

You might also like