0% found this document useful (0 votes)
38 views14 pages

Oper at I Ngsyst em &uni Xi NT Er Nal S: I Nternalassi Gnment-I I I

The document describes memory allocation algorithms like worst fit and first fit. It provides code snippets for implementing worst fit and first fit allocation strategies in C. The worst fit algorithm allocates memory in the block with the largest leftover space after allocation. The first fit algorithm allocates memory in the first block that has sufficient space. Both algorithms track block and file sizes and the allocated blocks.

Uploaded by

Sai Venkat
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)
38 views14 pages

Oper at I Ngsyst em &uni Xi NT Er Nal S: I Nternalassi Gnment-I I I

The document describes memory allocation algorithms like worst fit and first fit. It provides code snippets for implementing worst fit and first fit allocation strategies in C. The worst fit algorithm allocates memory in the block with the largest leftover space after allocation. The first fit algorithm allocates memory in the first block that has sufficient space. Both algorithms track block and file sizes and the allocated blocks.

Uploaded by

Sai Venkat
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/ 14

I

NTERNALASSI
GNMENT–I
II

Oper
ati
ngSy
stem &Uni
xInt
ernal
s
3rdYr
.5thSemest
erB-
Sect
ionC.S.E.

Topi
cs:
-

1.Cont
iguousMemor
yAl
l
ocat
ionTechni
ques
• BestFi
t
• WorstFi
t
• Fi
rstFi
t

2.PageRepl
acementAl
gor
it
hms
• FIFO
• LFU
• Opti
mal

3.I
PCSharedMemory
• Pr
oducer
–ConsumerPr
obl
em

Submi
tt
edTo:
Prof
.SurekhaThota,
Assi
stantProf
essorofC&IT,
RevaUniver
sit
y

Submi
tt
edBy
:
BVamsi KalyanReddy,
SRN: R17CS083,
rd
3 Yr .5thSemester
,
B-Section,CSE,
(
Cont
iguousMemor yAll
ocat
ionTechni
ques)
st
1 Program:-

CASE1:
WORSTFI
T

#incl
ude<st dio.h>
#defi
nemax25
voidmai n()
{
i
ntf rag[max],b[max],
f[
max],i
,
j,
nb,nf,
temp,hi
ghest=0;
stati
ci ntbf[max],f
f[
max];
printf(
"\n\tMemor yManagementScheme-Wor stFi
t")
;
printf(
"\nEnterthenumberofbl ocks:"
);
scanf (
"%d" ,
&nb);
printf(
"Enterthenumberoff il
es:")
;
scanf (
"%d" ,
&nf);
printf(
"\nEnterthesizeoftheblocks:-
\n");
for(i
=1; i
<=nb;i
++)
{
printf(
"Block%d: "
,i
);
scanf (
"%d" ,
&b[i]
);
}
printf(
"Enterthesizeofthefil
es:-\n")
;for(
i=1;
i
<=nf;i
++)
{pr i
ntf("
Fil
e%d: ",
i)
;
scanf("%d",
&f[
i]
);
}
for(i
=1;i<=nf;
i++)
{

for(j=1;j
<=nb;
j++)
{if(bf[j
]!
=1)//i
fbf [j
]isnotall
ocat
ed
{temp=b[ j
]-f
[i
];
i
f(temp>=0)
i
f(highest<t
emp)
{
ff[
i]
=j;
highest=t
emp;
}
}
}
frag[i]
=highest;
bf[ff[i
]]
=1;
hi
ghest
=0;
}
pri
ntf
("\
nFil
e_no:\
tFi
le_si
ze:\t
Block_
no:\t
Block_si
ze:
\tFragement
");
for
(i
=1;i
<=nf;
i++)
pri
ntf
("\
n%d\t\t%d\t
\t%d\t
\t%d\t\
t%d",
i
,f
[i
],
ff[
i]
,b[
ff
[i
]]
,
frag[i
])
;}

Out
put
:

CASE2:
FIRSTFI
T
#incl
ude<st dio.h>
#defi
nemax25
voidmai n()
{
i
ntf rag[max] ,b[max],
f[
max],i
,
j,
nb,nf,
temp;stat
ic
intbf[max] ,
ff
[max];
printf(
"\n\tMemor yManagementScheme-Fi rstFi
t"
);
printf(
"\nEnt erthenumberofbl ocks:"
);
scanf (
"%d" ,
&nb);
printf(
"Ent erthenumberoff il
es:")
;
scanf (
"%d" ,
&nf);
printf(
"\nEnt erthesizeoftheblocks:-
\n")
;
for(i
=1; i
<=nb; i
++)
{
printf("
Block%d:",
i)
;
scanf ("%d",
&b[i
])
;
}
printf(
"Ent erthesizeofthefil
es:-\n")
;for
(i
=1;i
<=nf
;i
++)
{pr i
ntf("
File%d: ",
i)
;
scanf ("%d",
&f[
i]
);
}
f
or(i
=1;i
<=nf ;
i++)
{
for(j=1;j
<=nb;
j++)
{if(bf[j
]!
=1)
{temp=b[j]
-f[
i]
;
i
f(t
emp>=0)
{
f
f[i
]=j;
break;
}
}
}
frag[i]
=temp;bf[ff
[i
]]
=1;
}
print
f("
\nFile_no:\
tFi
le_si
ze:\tBlock_no:\
tBl
ock_size:
\tFr
agement
");
for
(i
=1;i
<=nf;
i++)
pri
ntf
("\
n%d\ t
\t%d\ t\t
%d\t\
t%d\ t
\t%d",i
,f
[i
],
ff[
i]
,
b[ff
[i
]]
,
frag[i
])
;}

Out
put
:
CASE3:
BESTFI
T

#incl
ude<st dio.h>
#definemax25
voidmai n()
{
intfrag[max] ,b[max],
f[max] ,i
,
j,
nb,nf,
temp,l
owest =10000;
stati
ci ntbf [max],
ff[max] ;
printf(
"\nEnt erthenumberofbl ocks:")
;
scanf (
"%d" ,
&nb);
printf(
"Ent erthenumberoff il
es:")
;
scanf (
"%d" ,
&nf);
printf(
"\nEnt erthesizeoft heblocks:-
\n");
for(i
=1; i
<=nb; i
++)
{
pr i
ntf("
Block%d: ",i
);scanf("
%d" ,
&b[i
]);
}
printf(
"Ent erthesizeoft hefil
es:-\n")
;for(
i=1;
i<=nf;
i
++)
{pr i
ntf("Fil
e%d: ",
i)
;
scanf ("%d",
&f[i
])
;
}
for(i
=1;i<=nf ;
i++)
{
for(j=1;j
<=nb;j
++)
{if(bf[j
]!
=1)
{temp=b[ j]-
f[i
];
if
(temp>=0)
if
(lowest >temp)
{ff[i]
=j;
lowest=temp;
}
}
}
frag[i]
=lowest;
bf [
ff[i
]]
=1;
lowest =10000;
}
printf(
"\nFi l
eNo\ tFi
leSi ze\ t
BlockNo\ tBl
ockSi ze\t
Fragment
");
for(i
=1; i
<=nf&&f f[i
]!
=0;i
++)
print
f("\n%d\ t\t%d\t\t
%d\ t
\t%d\t\t
%d" ,
i
,f[
i]
,
ff[i
],
b[f
f[
i]
],
frag[
i
]);}

Out
put
:
2ndPr
ogr
am:
1.FI
FOPageRepl
acementAl
gor
it
hm
#include<st dio.h>
i
ntn, nf; int
i
n[100] ; int
p[50];
i
nthi t=0;
i
nti ,
j,
k;
i
ntpgf aul tcnt=0;
i
nti sHit (i
ntdat a)
{
hi t=0;
for(j=0;j<nf;j++)
{
if(p[ j
]==data)
{ hit
=1;
br eak;
} }
returnhi t;
}
i
ntmai n( )
{
pr i
ntf("\
nEnt erlengt
hofpagerefer
encesequence:
");
scanf (
"%d",&n);
pr i
ntf("\
nEnt erthepageref
erencesequence:
");
f or(i=0;i<n;i
++)
scanf ("%d" ,
&in[i
]);
printf
("\nEnt ernooff rames: "
);
scanf("%d" ,&nf )
; pgf aultcnt=0;
for(i
=0; i
<nf ;i++) p[i]=9999;
for(i
=0; i
<n; i++) {
pri
ntf(
"\nFor%d: ",
in[i
])
;
i
f(isHit(in[
i]
)==0)
{
f
or (
k=0; k<nf-1;k++)
p[k]=p[k+1];
p[k]=i
n[i]; pgfault
cnt++;
f
or( k=0; k<nf;k++)
{
i
f(p[k]!
=9999)
printf("%d",p[
k]);
}

}
el
se
pr
int
f("
Nopagef
aul
t"
);
}
pr
int
f("
\nTot
alnoofpagefaul
ts:
%d",
pgfault
cnt)
;
pr
int
f("
\nTot
alnoofpagehit
s:%d\n\
n",
n-pgfaul
tcnt
);
}
Out
put
:

2.LFUPageRepl
acementAl
gor
it
hm
#include<st dio. h>
i
ntn, nf; i
nt
i
n[100] ; int
p[50] ;
i
nthi t=0;
i
nti ,j
,k;
i
ntpgf aul tcnt =0;
i
nti sHit( i
ntdat a)
{
hi t=0;
for(j=0; j<nf; j
++)
{
if(p[ j
]==dat a)
{
hit=1;
break;
} }
returnhi t ;
}
i
ntget Hit I
ndex( intdat a)
{
inthi tind;
for(k=0; k<nf ; k++)
{
if(p[ k]==dat a)
{ hiti
nd=k;
br eak;
} }
returnhi t i
nd;
}
i
ntmai n( )
{
intusedcnt [100]; i ntleast ,
repin,sofarcnt=0,
bn;
printf("
\ nEnt erl engt hofpager eferencesequence: ")
;
scanf (" %d" ,
&n) ;
pr int f
( "\nEnt ert hepager eferencesequence: ")
;
for(i=0; i
<n; i++) scanf ("%d" ,
&in[i
]);
printf("
\ nEnt ernooff rames: "); scanf ("%d",&nf);
pgf aultcnt =0; for (
i=0;i<nf;i++) p[i
]=9999;
for (i=0; i
<nf ; i
++)
usedcnt [i]
=0;
for (i=0; i
<n; i
++)
{
pr int f
( "\nFor%d: " ,
i
n[i]
);
i
f(isHi t
(in[i])) {
inthi t
ind=get HitIndex(in[i
]);
usedcnt [hi t
ind]++;
printf("
Nopagef ault
!");
} else {
pgfaul
tcnt++; i
f(bn<nf
)
{ p[bn]=in[i
];
usedcnt [
bn]
=usedcnt [bn]+1;
bn++; }
el
se {
l
east=9999;
for(
k=0;k<nf;k++)
i
f(usedcnt[
k]<least
)
{
l
east=usedcnt
[k]
;
repin=k;
} p[r
epin]
=in[i
];
sofar
cnt =0;
for(
k=0;k<=i;k++) i
f(i
n[i
]==i
n[k])
sofarcnt=sofarcnt+1;
usedcnt[r
epin]=sof
ar cnt;
}
for(k=0; k<nf;k++)
{
if(
p[k]!
=9999)
pri
ntf(
"%d" ,
p[k]);
}

} }
pri
ntf
("\
nTot
alnoofpagefaul
ts:
%d" ,
pgfault
cnt)
;
pr
int
f("
\nTot
alnoofpagehi
ts:%d\n\
n",
n-pgfaul
tcnt)
;
}

Out
put
:
3.Opt
imal
PageRepl
acementAl
gor
it
hm

#include<st di
o.h>
i
ntn, nf;int
i
n[100] ;int
p[50];
i
nthi t=0; int
i
,j,
k;
i
ntpgf aultcnt=0;
i
nti sHi t
(intdat a)
{
hit=0;
f
or (j
=0; j
<nf;j++)
{
if(p[j
]==data)
{ hit
=1;
break;
} }
returnhi t;
}
i
ntget HitIndex(intdata)
{
inthi ti
nd;
for(k=0; k<nf ;k++)
{
if(p[
k]==data)
{
hi
ti
nd=k;
br
eak;
}
}
returnhi ti
nd;
}
i
ntmai n( )
{
printf("
\nEnterlengt hofpager eferencesequence:
");
scanf ("%d" ,&n);
printf("
\nEnterthepager eferencesequence: ")
;
for(i=0; i<n; i
++) scanf ("%d",&in[i
])
;
printf("\nEnt ernooff rames: "); scanf ("%d",
&nf)
;
pgf aultcnt =0;
for(i=0;i<nf;
i++)
p[i
]=9999; i ntnear[
50] ;
for(i=0;i<n;i
++)
{
printf("
\nFor%d: ",i
n[i
]);
i
f(isHi t
( i
n[i])==0) {
for(j=0;j<nf;j++)
{ i
ntpg=p[ j]
;
intfound=0;
for(k=i; k<n; k++) {
if
( pg==in[k])
{
near [j
]=k;
found=1;
break; }
else
found=0;
}
i
f (
!found)
near [j
]=9999;
}
i
ntmax =-9999; i
nt
repindex; for (
j=0;
j
<nf ;j++) {
if
( near[j
]>max)
{
max=near [
j];
r
epindex=j ;
}
}
p[repindex] =in[i
];
pgf ault
cnt ++;
for( k=0; k<nf;k++)
{
i
f(
p[k]
!=9999)
pri
ntf
("%d"
,p[
k])
;
}
}
el
se
pr
int
f("
Nopagef
aul
t"
);
}
pri
ntf
("\
nTot
alnoofpagefaul
ts:
%d" ,
pgfault
cnt)
;
pr
int
f("
\nTot
alnoofpagehi
ts:%d\n\
n",
n-pgfaul
tcnt)
;
}

Out
put
:

(
ProducerandConsumerPr
obl
em)
3rdPr
ogr
am:
-
#i
ncl
ude<st
dio.
h>v
oid
producer ();
voidconsumer ();intwait(int)
;
i
ntsi gnal( i
nt );i
nt
mut ex=1, full=0,empt y=3,x=0;
mai n()
{intn;
printf (
"\n1. PRODUCER\ n2.CONSUMER\ n3.EXI
T\ n")
;
whi le(1)
{
printf("
\nENTERYOURCHOI CE\n");
scanf ("
%d" ,
&n) ;swi t
ch(n)
{
case1:
if(
(mut ex==1) &&(empty!
=0))producer (
);
elsepr i
ntf("BUFFERI SFULL");break;
case2:
if(
(mut ex==1) &&(ful
l
!=0)
)consumer ()
;
elsepr i
ntf("BUFFERI SEMPTY" );break;
case3: exit
(0);br eak;
}
}
}
i
ntwai t(ints)
{return(--s);
}
i
ntsi gnal(ints)
{
retur n(++s) ;
}
voidpr oducer ()
{
mut ex=wai t(
mut ex);
full=signal (ful
l);
empt y =wai t(
empt y);
x++;
printf (
"\nPr oducerpr oducest heit
em%d" ,
x);
mut ex=si gnal(mut ex);
}
voidconsumer ()
{
mut ex=wai t(
mut ex);
full=wai t(full
);
empt y =signal(empt y);
printf("\nConsumerconsumesi tem%d",x);
x--;
mut ex=si gnal(mut ex);
}
Out
put
:

You might also like