Escolar Documentos
Profissional Documentos
Cultura Documentos
Os Lab Programs
Os Lab Programs
NGALGORITHMS
Exper
iment-
7
AI
M:Wri
teacprogr
am t
oimpl
ementROUNDROBI
NSCHEDULI
NG
RRDescr i
ption:
Timeshar ingsy stem usedt her oundr obinalgor i
thm.Useofsmal lti
me
quant um al lows r ound r obi nt o provide good r esponse t i
me.RR
schedul ingal gor it
hm isapr eempt iv
eal gori
thm CPUsel ectst heprocess
from t her eadyqueue.Toi mpl ementRR schedul ing,r eadyqueuei s
mai ntainedasaFI FOqueuei fthepr ocess.Newpr ocessesar eaddedt o
thet ailoft her eadyqueue.
TheCPUschedul i
ngpi ckst hef i
rstpr ocessfrom t her eadyqueue, set
sa
ti
mert oi nterruptaf ter1timequant um anddi spat cheshepr ocess.With
theRRal gorit
hm,t hepr i
nci paldesi gni ssuei st hel engt hoft het i
me
quant um orsl i
cet obeused.
I ft het imequant um i sv er yshor t
,t hen shortpr ocesseswi llmov e
through t he sy st
em r el
ativ el
y qui ckly.Iti ncreases t he pr ocessing
ov erheadi nvolvedi nhandl ingt hecl ockinterruptandper formingt he
schedul inganddi spatchingf unct i
on.Thus,v eryshor tt i
mequant um
shoul dbeav oi ded.
Al
gor
it
hm:
Step1:Start
2:Decloar eexeCount ,
i,
rounds:=1,f
ini
sh=0,tot
alwait
ing=0
3:Repeatst eps4t o10Whi le(noti
sAllFi
nished(p,
n))
4:Print“AfterRound#” ,rounds++
5:Repeatst eps6t o10ForI :=0tonst ep1
6:Repeatst eps7t o9ForexeCount :
=1toexeCount <=qAND
p[
i]
.r
emainingTime! =0
7:Setp[i].
remai ningTi
me- -
8:Setf i
nish:=finish+1
9:if
(p[i
].
remai ningTime=0)Then→ Setp[ i].
fi
nishTi
me:=fi
nish
[EndFor–exeCount ]
10:Pr i
nt“Remai ningti
me' ,
p[i
].
remaini
ngTi me
[EndFor–I ]
[EndWhi le]
11:Repeatst eps12, 13ForI :
=0t onstep1
12:Printp[i
].
fi
nishTime,p[i
].
fini
shTi
me- p[i
].
burstTi
me/ /thi
siswait
ing
ti
mesubt racti
on
13:Settotalwai
ting+=p[i
].
fi
nishTime-p[
i].
burst
Time/ /addingtot
al
waiti
ng
[EndFor ]
14:Printtot
alwaiti
ng,tot
alwai t
ing/
n/ /
nf orno.ofprocesses
15:Stop
Program:
#include<st di o.h>
i
ntmai n()
{
i
ntcount ,
j,n,time,remai n,
flag=0,time_quantum;
i
ntwai t_
time=0, turnaround_ time=0,at[
10],
bt[10]
,
rt[10]
;
pri
nt f("
EnterTot al Process:\ t")
;
scanf ("
%d" ,&n) ;
remai n=n;
for(count=0; count<n;count ++)
{
printf("
Ent erAr rivalTimeandBur stTimef orProcessProcessNumber
%d: "
,count +1) ;
scanf ("
%d" ,&at[
count ]);
scanf ("
%d" ,&bt[count]);
rt[
count ]=bt [count ]
;
}
pri
nt f("
EnterTi meQuant um: \t
");
scanf ("
%d" ,&t i
me_ quant um) ;
pri
nt f("
\n\nPr ocess\ t
|Turnar oundTime|Wai ti
ngTime\ n\
n");
for(ti
me=0, count =0;remain! =0;)
{
if
(rt[count ]<=time_ quantum &&r t[
count]
>0)
{
time+=r t[count ]
;
rt[count]=0;
flag=1;
}
elsei f
(rt
[count ]>0)
{
rt[count]-=t i
me_ quant um;
time+=t ime_ quant um;
}
if(rt[
count ]
==0&&f lag==1)
{
remai n--
;
pr i
ntf(
"P[%d]\t
|\t%d\t|\
t%d\n",
count+1,t
ime-at
[count],
ti
me-at
[count
]-
bt[count ]);
wai t
_time+=t i
me- at[
count]
-bt[
count];
turnaround_ t
ime+=t ime-at
[count]
;
flag=0;
}
if(count ==n-
1)
count =0;
el seif(at[
count+1]<=time)
count ++;
el se
count =0;
}
print f
("\nAverageWai t
ingTime=%f \n",
wait_t
ime*1.0/n)
;
print f
("AvgTur naroundTi me=%f "
,t
urnaround_t
ime* 1.
0/n);
retur n0;
}
Exper
iment-
8
AI
M: Wr iteacpr ogr am t oi mpl ementSJFSCHEDULI NGALGORI THMS
Descri
pt ion:
Thisal gor i
thm associ ateswi theachpr ocesst hel engthoft helatt
er ’
s
nextCPUbur st.Whent heCPUi sFr ee,iti sassi gnedt ot heprocessof
ther eadyQueuewhi chhassmal l
estnextCPUbur st.Iftwopr ocesshav e
thesamel engt h,FCFSschedul ingi susedt obr eakt hefile.
SJFSchedul i
ngal gorithm i susedf requent l
yi nl ongt erm scheduling.
SJFal gorithm maybeei therpr eempt iveornon- preempt i
v e.
A pr eempt i
v e SJF al gor it
hm wi llpr eemptt he cur r
ent l
y executi
ng
process,wher e as a non- preempt ive SJF al gor it
hm wi l
lal l
ow t he
current lyrunni ngpr ocesst of i
nishi tsCPUbur st.
SJF al gorithm i s opt imalal gor i
thm.I tgi ves t he mi nimum av er
age
waitingt imef oragi vensetofpr ocesses.SJFal gorithm cannotbe
implement edatt hel ev elofshor tt erm CPUSchedul i
ng.Ther eisnoway
toknowt hel engt hoft henextCPUbur st.
Al
gorithm:
St
ep1: St art
2: Decl arewt :
=0, tt
:=0,i //wt-
forwai t
ingt ime, tt-turnaroundt ime,i-
i
ndexr epeat ing
3: Repeat st ep4ForI :
=0t onst ep1
4: Pr i
ntp[ i]
.pid //pri
ntingeachpr ocessi dinGant tchart
[
Endf or]
5: RepeatSt eps6t o10Fori =0t onst ep1
6: pr i
ntwt / /
priti
ngwai t
ingt ime
7: Setp[ i]
.wat iti
ngt i
me: =wt
8: RepeatSt eps9, 10whi l
ep[ i]
.Remai ningTi me>0
//eachpr ocessr emai ningti
mei si niti
all
ybur stt ime
9: Setp[ i]
.Remai ningTi me:=p[ i
].
Remami ningTi me- 1
10: SetWt :
=Wt +1/ /incr easewai tingt i
meby1
[EndWhi le]
[EndFor ]
11: pr i
ntWt/ /l
astpr ocesswai t
ingt i
me
12: Setp[ i
].waitingTi me: =Wt
13: Repeatst eps14t o16ForI :=0t onst ep1
14: setp[ i].
turnAr oundTi me:=(p[i].
wai ti
ngTi me+p[ i]
.burstTi
me) -
p[
i]
.Arriv
alTime / /Eachprocesst ur
naroundtime
15:Settt:=tt
+p[i]
.TurnaroundTime/ /cal
culat
ingt
otalt
urnaroundt
ime
16:Setwt :=wt+p[i]
.waiti
ngTime / /calcul
ati
ngtot
alwaiti
ngtime
[
EndFor ]
17:Print“Avgt ur
nar oundtime: “
,t
t/n
18:Print“Avgwai ti
ngt i
me: “
,wt/
n
19:Stop.
Pr
ogr
am:
#include<st dio.h>
voidmai n( )
{
intbt [20] ,
p[20] ,
wt [
20],
tat
[20],
i,
j,
n,t
otal
=0,pos,t
emp;
floatav g_wt ,av g_t
at;
pr i
ntf("Ent ernumberofpr ocess:"
);
scanf ("%d" ,
&n) ;
printf("\nEnt erBur stTime:\n"
);
for (i
=0; i
<n; i
++)
{
printf("p%d: ",i
+1);
scanf (
"%d" ,&bt[i
]);
p[i
]=i +1;
//
containsprocessnumber
}
//sor ti
ngbur stti
mei nascendi ngorderusingsel
ect
ionsor
t
for (i
=0; i
<n; i
++)
{
pos=i ;
for(j=i+1;j<n;j++)
{
if(bt[j]
<bt [pos])
pos=j ;
}
temp=bt [
i];
bt[i
]=bt [pos] ;
bt[pos] =temp;
temp=p[ i
];
p[i
]=p[ pos] ;
p[pos] =temp;
}
wt [0]=0;
/ /waitingt i
mef orfirstprocesswi ll
bezero
//calculatewai tingt i
me
for(i
=1; i
<n;i
++)
{
wt [i
]=0;
for(j=0;j
<i;
j++)
wt[i]
+=bt[j]
;
total+=wt [i
];
}
av g_wt =(fl
oat )
total/n;
//averagewai tingt ime
total=0;
pr i
ntf("\nProcess\ t
Bur stTime
\tWai ti
ngTi me\ t
TurnaroundTi
me"
);
for(i
=0; i
<n;i
++)
{
tat[i]
=bt[i
]+wt [i
];
//calculat
et urnaroundt i
me
total+=tat[i
];
pr i
ntf("\
np%d\ t\t
%d\ t\t
%d\ t
\t\t%d",p[i]
,
bt[i
],wt[
i]
,
tat[
i]
);
}
avg_ tat=(f
loat)total/n;
//averaget urnar oundt i
me
printf("
\n\nAv erageWai tingTime=%f ",
av g_wt )
;
printf("
\nAv erageTur naroundTi me=%f \n",avg_tat)
;
}
Exper
iment-
9
AIM: Wr i
teacpr ogram t oi mpl ementFCFSSCHEDULI NG
Descr ipti
on:
FirstCome Fi rstSer v ed (FCFS)i s a Non- Preempt i
v e
schedul ing
algorithm.FI FO ( FirstI nFi rstOut )strat egyassi gnspr iori
tyt o
pr ocessi n
theor deri nwhi cht heyr equestt hepr ocessor .Thepr ocesst hat requests
theCPUf ir
sti sal l
ocat edt heCPUf i
rst.Thi siseasi lyimpl ement ed wi t
ha
FIFOqueuef ormanagi ngt het asks.Ast hepr ocesscomei n,theyar eput
at
theendoft hequeue.Ast heCPUf inisheseacht ask,itr emov esi tf r
om
the startofthequeueandheadsont ot henextt ask.
FCFSi sthesi mpl estschedul ingmet hodCPUi sal locatedtot hepr ocess
i
nt he or derofar r
iv al.The pr ocesst hatr equest st he CPU f irstis
all
ocat edt heCPUf irst.
FCFSi sal socal ledFI FO. FCFSi snon- preempt i
veschedul ingal gorit
hm
i
mpl ement ati
onoft heFCFSpol i
cyi seasi lymanagedwi t
haFI FOQueue
Whenpr ocessent erst her eadyQueue, it
sprocesscont rolatt heheadof
theQueue.Ther unni ngpr ocessi sremov edfrom t hequeue.
FCFSi snotsui tablef orr eal timesy st ems.
Performancemat ri
ci sav eragewai ti
ngt ime.Av eragewai ti
ngf ort heFCFS
i
sof t
enqui tel ong.
Al
gor
ithm
St
ep1: Start
2: Declar
ewt :
=0, I //Waiti
ngTi meandi f
orindex
3: Repeatstep4f orI
:=0tonst ep1
4: Pri
ntp[i]
.pid //pr
intingeachpr ocessi
nGanttchart
[
Endf or]
5: Repeatsteps6t o10ForI:=0t onst ep1
6: Pri
ntwt/ /pr ocesseachwai ti
ngt i
me
7: Setp[i
].
wai ti
ngTime:=wt-
p[i].
arri
val
Ti me /
/updateeach
processwai ti
ngt i
me
8: RepeatSt ep9,10whi l
ep[i
].burst
Time>0l oop
9: p[
i]
.burstti
me- - //
pr ocesseachburstti
meunti
l
reachest o0
10: Setwt :
=wt +1
[EndWhi le]
[EndFor ]
11: Pr intwt/ /l
astpr occesswai tingt i
me
12: Setp[ i
].waitingTime: =wt
13: Repeatst ep14f orI:=0t onst ep1
14: Setwt :=wt +p[i].
waitingTime/ /sum ofallwait
ingti
mes
[Endf or ]
15: pr i
nt“ Av eragewai t
ingt ime:“ ,
wt /n
16: St op.
Pr
ogr am:
#i
ncl ude<st di o.h>
i
ntmai n()
{
i
ntn, bt[20] ,
wt [20],tat[
20],avwt =0,avtat=0,i
,j
;
pri
nt f("
Ent ert ot alnumberofpr ocesses( maximum 20):
")
;
scanf ("%d" ,&n);
pri
nt f("
\ nEnt erPr ocessBur stTi me\ n" )
;
for(i
=0; i<n; i
++)
{
pr i
ntf( "P[%d] :
",i
+1) ;
scanf ("%d" ,
&bt [i
]);
}
wt[0]=0;
// wai ti
ngt imef orfirstprocessi s0
/ /
cal cul atingwai t
ingt i
me
for(i
=1; i<n; i
++)
{
wt [i
]=0;
for(j=0; j<i;j
++)
wt [
i]+=bt [j
];
}
printf (
"\ nPr ocess\ t\tBurstTime\ t
Wai t
ingTime\t
TurnaroundTi
me"
);
/ /
cal cul atingt urnaroundt ime
for(i
=0; i<n; i
++)
{
tat[i]
=bt [i
]+wt [i
];
av wt +=wt [
i];
av tat+=t at[i
];
pr i
ntf( "\nP[ %d] \t\t%d\t\t%d\ t\
t%d" ,i
+1,bt[
i]
,
wt[i
],
tat
[i
])
;
}
av wt /=i;
avt
at/=i
;
pri
ntf
("\
n\nAv
erageWait
ingTi
me:%d",
avwt);
pri
ntf
("\
nAver
ageTurnar
oundTime:
%d",av
tat)
;
retur
n0;
}
Exper
iment-
10
AI
M: Wr i
teacpr ogram t oimpl ementPRI ORITYSCHEDULI NG
Description:
CPUi sal l
ocatedt ot hehi ghestpr i
or i
tyoft hepr ocessf ormt her eady
Queue.Eachpr ocesshasapr iori
tynumber .Iftwoormor epr ocesses
hav et hesamepr iority,thenFCFSal gorithm i sappl iedforsol i
ngt het i
e.
Inourexampl es, l
ownumber shav ethehi ghpr iority.
Prior i
ty schedul ing i s preempt i
ve ornon- pr eempt i
ve pr i
ority oft he
processcanbedef inedei theri nter
nallyorext ernally.Internall
ydef i
ned
prior i
tyconsi derst het i
mel imits,numberofopenf il
es,useofmemor y
anduseofI /O dev ices.Ext ernalpr i
ori
t i
esar esetbyusi ngext ernal
par amet eroft hepr ocess, likei mportanceofapr ocess, castofpr ocess
etc.
Apr eempt i
vepr iorityschedul ingalgor i
thm wi l
lpr eemptt heCPUi fthe
prior i
tyoft henewl yar rivedpr ocessi shi ghert hant hepr i
orit
yoft he
curr entlyr unning pr ocesses.I nt his,cur rentexecut i
ng pr ocess wi l
l
changet hest atef rom r unningt oready .
I nnon- preempt iv
eschedul ingal gori
thm, cur rentlyexecut ingpr ocesswi l
l
notchangest ate.
Al
gori
thm f orPr i
or i
tySchedul ing(Process* p,
integern)
St
ep1: Star t
2: Decl ar
ewt :
=0, I//Wai tingTimeandi forindex
3: Repeatst ep4f orI:=0t onstep1
4: Printp[i]
.pid/ /pri
nt i
ngeachpr ocessinGant tchart
[
Endf or]
5: Repeatst eps6t o10ForI :
=0t onst ep1
6: Printwt/ /pr ocesseachwai ti
ngt i
me
7: Setp[ i
].
wai ti
ngTi me: =wt -
p[i
].
arri
valTime/ /updateeachpr
ocess
wai
ti
ngt ime
8: RepeatSt ep9, 10whi lep[i
].
burstTi
me>0l oop
9: p[i
].burstti
me- -/ /
pr ocesseachbur st
timeunt i
lreachest
o0
10: Setwt :
=wt +1
[
EndWhi le]
[
EndFor ]
11: Pr i
ntwt/ /
lastpr occesswai t
ingtime
12: Setp[ i]
.waitingTi me: =wt
13: Repeatst ep14f orI:=0tonst ep1
14:
Setwt:=wt
+p[
i]
.wai
tingTi
me//sum ofal
lwai
ti
ngt
imes
[
Endfor]
15:
pri
nt“Aver
agewait
ingtime:
“,wt/
n
16:
Stop.
Pr
ogr
am:
#include<st dio. h>
i
ntmai n()
{
intbt[20] ,p[20] ,
wt [20],
tat[
20],
pr[20],
i,j
,
n,total
=0,pos,
temp,
avg_wt
,avg_
tat
;
printf("Ent erTot alNumberofPr ocess:");
scanf ("%d" ,
&n) ;
printf("\nEnt erBur stTimeandPr i
ority
\n");
for(i
=0; i<n; i
++)
{
print f("\nP[ %d] \n",
i
+1) ;
print f("Bur stTi me:");
scanf ("%d" ,&bt [i
])
;
print f("Prior i
ty:")
;
scanf ("%d" ,&pr [i
])
;
p[i]=i+1; //containsprocessnumber
}
//sortingbur stt i
me, priori
tyandpr ocessnumberi nascendi
ngorder
usingsel ect i
onsor t
for(i
=0; i<n; i
++)
{
pos=i ;
for(j=i+1; j<n;j++)
{
i
f(pr [j]
<pr [pos] )
pos=j ;
}
temp=pr [
i];
pr[i]=pr [pos] ;
pr[pos] =temp;
temp=bt [
i];
bt[i]=bt [pos] ;
bt[pos] =temp;
temp=p[ i
];
p[i]=p[ pos] ;
p[pos] =temp;
}
wt[0]=0; / /wai tingtimef orfi
rstpr ocessi szero
//calcul atewai t
ingt i
me
f
or(i
=1;i
<n;i
++)
{
wt[
i]
=0;
f
or(j
=0;j
<i
;j
++)
wt[
i]
+=bt[
j]
;
t
otal
+=wt
[i
];
}
avg_wt =total/
n; / /
av eragewai ti
ngtime
tot
al=0;
pri
ntf("\nProcess\t Bur stTi me \ tWait
ingTi me\ t
TurnaroundTi
me"
);
for
( i
=0;i<n;
i++)
{
tat[i
]=bt[i
]+wt[i
]; //calculateturnar
oundt ime
total+=tat[i
];
printf("
\nP[%d]\t\
t%d\ t\t %d\ t
\t\t
%d",p[i
],
bt[i
],
wt[i
],
tat
[i
]);
}
avg_tat=total/n; //averaget urnaroundtime
pri
ntf("\n\nAverageWai t
ingTi me=%d",avg_wt )
;
pri
ntf("\nAverageTur naroundTi me=%d\ n",avg_tat
);
ret
urn0;
}
PAGEREPLACEMENTALGORI THMS
Exper
iment-
11
AI
M:Si
mul
atet
heMult
ipr
ogr
ammi ngwithafi
xednumberoft
asks(
MFT)
Descr
ipt
ion:
I fweacceptt hatmul ti
programmi ngisagoodidea,wenextneedto
decidehowt oor ganisetheav ailabl
ememor yinordert
omake
eff
ect i
veuseoft her esource.
Onemet hodistodi vi
det hememor yint
ofixedsi
zedparti
ti
ons.These
partit
ionscanbeofdi ff
erentsizesbutonceapar ti
ti
onhastakenona
certainsizet
heni tremainsatt hatsize.
Ther eisnoprovisionforchangi ngitssi
ze.
Al
gor
ithm:
St
ep1: Start
2: Declare*processes, *blocks,totalfrags,i
,,j
,np,memory,
nb
3: I
nputnp //no.ofprocesses
4: Setprocesses=r eadProcesses( processes,np)
5: I
nputmemor y
6: I
nputnb //no.ofblocks
7: i
f(nb<np)Then
8: Pr i
nt“ Errormessage”
9: Return1
[EndI f]
10: Setblocks=al locateMemor yBlocks( bl
ocks,nb,
memory/nb)
11: Repeat st eps12t o17f ori=0,j=0t onb,npstep1
12: Setf ragment :=blocks[i]
-processes[i]
13: Pr i
ntpr ocesses[ i
],
blocks[ i]
14: If(f
ragment <0)Then: pri
nt“ Fai
led”
16: Else:Pr i
ntfragment
[
EndI f]
17: I
f(fragment >0)Then: totalf
rags+=fragment
[EndFor ]
18: Print“Totalfragment ation”,
totalfrags
19: Stop
Pr
ogr
am:
#i
nclude<stdi
o.h>
main()
{
i
ntms,i,
ps[20]
,n,si
ze,
p[20]
,
s,i
ntr
=0;
printf("
Ent ersizeofmemor y:
");
scanf ("%d" ,
&ms) ;
printf("
Ent ermemor yforOS: "
);scanf(
"%d",
&s);
ms- =s;
printf("
Ent erno.ofpartiti
onst obedi v
ided:"
);
scanf ("%d" ,
&n);
size=ms/ n;
for(i=0;i
<n;i++)
{
printf("
Ent erprocessandpr ocesssize:")
;
scanf ("%d%d" ,&p[
i]
,&ps[i])
;
i
f(ps[ i
]<=size)
{
i
nt r=intr
+si ze-
ps[i
];
printf("
process%di sallocated\n",
p[i
])
;
}
else
printf("
process%di sblocked\ n",
p[i
])
;
}
printf("
total fr
agment ationis:%d",i
ntr
);
}
Out put:
Exper
iment-12
AIM:Simul atetheMul ti
programmi ngwithav ar
iabl
enumberoft asks(
MVT)
Descripti
on:
Mul t
iprogrammi ngwi thv ari
abletasks,Paging,LogicaltoPhysical
address t ranslat
ion,I ntr
oduces ext er
nal fragmentat
ion,Compact i
on,
Frames, PageTabl e,Hardwar esupporforpaging.Abovement i
onedarekey
point
soft hislecturehandout .Vir
tualUni
versit
yhandoutforintr
oduct
ionto
operatingsy st
em areindet ai
landexplanatory.
Algorithm
Step1: St ar
t
2: Declarebl ocks,processes, fragment saspoi nters
3: Declarei ,
j
,totalfr
agment s:=0,np,nb //i
,jf
orindexand
np-no.ofpr ocesses, nbno.ofbl ocks I
nputnp,nb/ /
no.ofprocessesand
no.ofbl ocks
4: Setblocks: =callallocateMemor y Bl
ocks()
5: Setpr ocesses: =callallocatePr ocesses()
6: Setf r
agment s:=all
ocat ewi thsi zeofno.ofpr ocesses
7: RepeatSt eps8t o13 ForI:
=0t onpst ep1 / /no.of
processt imes
8: RepeatSt eps9t o13 Forj :
=0t onbst ep1 / /no.ofbl
ocks
ti
mes
9: I
fpr ocesses[ i]
<=blocks[ j]Then
10: Setf ragment s[i]
:=blocks[ j
]-pr
ocesses[ i
] /
/all
ocationand
subtract ionforr emai ningspace
11: Sett otalfragment s+=f r
agment s[i
]
12: Pr i
nt“ Fragment#” ,
i+1,”Sizeis: “,
fr
agment s[i
] / /Eachfragment
anditssi ze
13: Exitloop/ /i
nnerl ooponl y
[EndI f]
[EndForj ]
[EndFori ]
14: Pr i
nt“ Tot alfr
agment ation: “
,totalf
ragment s
15: Stop
Progr am:
#include<st dio.h>
mai n()
{
i
nti ,
m, n,t
ot ,
s[20];
printf
("Ent ertotalmemor ysize:")
;scanf("
%d",
&tot
);
printf
("Ent erno.ofpages: "
);
scanf ("
%d" ,
&n);
printf
("Ent ermemor yforOS: ");
scanf ("
%d" ,
&m) ;
for(i
=0;i<n;i++)
{
printf
("Ent ersizeofpage%d: ",
i+1);
scanf ("
%d" ,
&s[i]
);
}
tot=tot-m;
for(i
=0;i<n;i++)
{
i
f(tot>=s[i])
{
printf
("Allocat epage%d\ n",
i+1);
tot=tot-s[
i];
}
else
printf
("processp%di sblocked\ n",
i+1);
}
printf
("Exter nalFragment ati
oni s=%d" ,
tot
);
}
Out
put
:
Exper
iment-13
AI
M: Simul ateBanker sAlgori
thm forDeadLockAv oidance.
Descri
ption:
Banker salgorit
hm isusedt odetectt heoccurrenceofdead-l
ockand
prevents.
I tfr
om occur rencethrought hesafetyalgor
ithm embeddedinitwhich
dealswi ththeinformati
onaboutsaf esequence.
Whi chwhenf oll
owedcr eatesnopr oblem tothef l
owofexecutionof
thepr ocesseswi t
houtanydead- l
ockoccur rence.
Thus,t heef f
ici
encyoft heCPUi ncreasesandf orthesamer eason
everyCPUi sinbuil
twithabanker ’
salgorit
hm.
Al
gor
ithm: // f
orBanker 'sdeadl ockav oidance
Step 1: St art
2:Decl are**Max, **
need, **all
oc, *avail
,*compl eted,
*safeSequence
3:Decl arep,r,
i,
j,
count :
=0
4:I
nputp //
no.ofpr ocesses
5:I
ni ti
alizeallCompl eted[]arrayt o0/ /allarefalse
6:I
nputr //
no.ofr esources
7:SetMax :
=ProcessRequi r
ement (Max,p,r
) / /all
ocat
e2-
D
arrayandi nput
8:Setal loc:
=Pr ocessRequi rement (al
loc,p,r) / /all
ocat
e2-
D
arrayandi nput
9:Allocat eAvail[
]arraydy nami cally
10: Allocatesaf eSequence[ ]arraydy nami call
y
11: RepeatSt ep12ForI :=0t orst ep1
12: Inputav ail
[i
]
[EndFor ]
13: Allocateneed[ ][
]2-Dar rayusingf orloop
14: Repeat st ep15, 16forI :
=0t opst ep1
15: Repeatst ep16f orj:=0torst ep1
16: Setneed[ i
][
j]
=Max[ i
][
j]-
all
oc[i]
[j
]
[EndFor ]
[
EndFor ]
17:Callprint
Mat rix(
Max, al
loc,need, p,
r) //f
orpr
int
ingof
3-
arr
ays
18:Repeatst eps19- 26ForI :
=0t opst ep1
19: Repeatst ep20- 26forj :
=0t orst ep1
20: i
f (
av ai
l[
j]>=need[ i]
[j
])then
21: Setcount ++
[EndI f
]
22: If(
count ==randcompl eted[ i
]==0)Then
23: Setcompl eted[i
]=1/ / settrue
24: Setsaf eSequence[ i
]=i+1
25: Setav ail
[j
]+=all
oc[ i]
[j
]
26: Setcount :=0
[
EndI f]
[Endfor ]
[Endfor]
27:RepeatSt ep28- 30f orI:
=0t opst ep1
28: I f(Compl eted[i
]==0)Then
29: Print“Syst em isunsaf est ate”
30: Retur n1
[EndI f
]
[
EndFor ]
31:Print“System isinsaf estate”
32:Repeatst ep33f orI :
=0t opst ep1
33: Pr intsafeSequence[ i]
[EndFor ]
34:Stop
Pr
ogram:
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
i
ntmain(
)
{
i
ntMax[
10]
[10]
,
need[
10]
[10]
,
all
oc[
10]
[10]
,
avai
l
[10]
,
compl
eted[
10]
,
saf
eSeque
nce[
10];
i
ntp,r
, i
,j,pr ocess, count ;
count=0;
pri
ntf
("Ent erthenoofpr ocesses: ")
;
scanf("
%d" ,&p) ;
for
(i=0; i<p; i++)
compl eted[i
]=0;
pri
ntf
("\n\ nEnt erthenoofr esour ces: "
);
scanf("
%d" ,&r);
pri
ntf
("\n\ nEnt ertheMaxMat r
ixforeachpr ocess:"
);
for
(i=0; i <p; i++)
{
pri
ntf("\ nForpr ocess%d: ",
i +1);
for(j=0; j<r ;
j++)
scanf (
"%d" ,
&Max[ i]
[j
])
;
}
pri
ntf
("\n\ nEnt ertheallocat i
onf oreachpr ocess:"
);
for
(i=0; i <p; i++)
{
pri
ntf
("\nForpr ocess%d: ",
i+1) ;
for
(j=0; j <r ;
j++)
scanf ("%d" ,&all
oc[i]
[j])
;
}
pri
ntf
("\n\ nEnt ertheAv ailableResour ces: "
);
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]-all
oc[i][
j]
;
do
{
printf("\
nMaxmat ri
x:\t
Al l
ocati
onmat ri
x:
\n")
;
for (
i=0; i <p;i++)
{
f
or(j =0; j
<r ;
j++)
pr
intf
("%d",Max[i]
[j
])
;
pr
int
f("
\t\
t")
; f
or(j=0;j<r;j
++)
pr
int
f("
%d" ,al
loc[
i]
[j
])
;pri
ntf
("\
n")
;
}
process=- 1;
for(i=0; i<p; i++)
{
i
f(compl eted[ i
]==0) //
ifnotcompleted
{
process=i ;
for(j
=0; j<r ;j++)
{
i
f(av ail[
j]<need[ i]
[j
])
{
pr ocess=- 1;break;
}
}
}
i
f(process! =-1)br eak;
}
i
f(process! =- 1)
{
pri
nt f
("\nPr ocess%dr unst ocompleti
on!
",pr
ocess+1)
;
safeSequence[ count ]=process+1;
count ++;
for(j=0; j <r ;j
++)
{
avail
[j]+=al loc[ process][
j]
;
all
oc[pr ocess] [
j]=0;
Max [pr ocess] [j]=0;
compl eted[ process]=1;
}
}
}
whi
l
e(count!
=p&&pr
ocess!
=-1)
;
i
f(
count==p)
{
pri
ntf("
\nThesy st
em isinasafestat
e!!
\n"
);
pri
ntf("
SafeSequence: <");
for
(i =0;i <p;i
++)
printf
("%d",safeSequence[
i]
);
printf
(">\n"
);
}
el
seprintf
("\
nThesy stem i
sinanunsafestat
e!!
")
;
}
OUTPUT
Exper
iment-
14
Al
gor
it
hm://forBanker 'sdeadl ockpr ev ent i
on
Step1: Start
2:Decl ar e**Max, *
* need, **alloc, * av ail,*compl eted,
*safeSequence
3:Decl ar ep,r,
i
,j,
count :
=0
4: Inputp //no.ofpr ocesses
5:Initi
al i
zeal lCompl eted[]arr ayt o0 / /al
laref alse
6:Inputr //
no. ofr esour ces
7:SetMax :=ProcessRequi rement (Max, p,r
) / /allocat
e2-D
arrayandi nput
8:Setal l
oc:=Pr ocessRequi rement (alloc, p,
r) / /
al l
ocate2-
D
arrayandi nput
9:Al l
ocat eAv ail[
]ar raydy nami cal ly
10: Allocatesaf eSequence[ ]ar raydy nami cally
11: RepeatSt ep12ForI :
=0t orst ep1
12: Inputav ai l[
i]
[EndFor ]
13: Allocateneed[ ]
[]2- Darrayusi ngf orl oop
14: Repeat st ep15, 16f orI :
=0t opst ep1
15: Repeatst ep16f orj :=0t orst ep1
16: Setneed[ i][
j]=Max[ i][j]-al
loc[i
][
j]
[
EndFor ]
[EndFor ]
17: CallprintMat ri
x(Max, alloc, need, p, r)/ /forprintingof3-
array s
18: Repeatst eps19- 26ForI :=0t opst ep1
19: Repeatst ep20- 26f orj :=0t orst ep1
20: if(
av ail
[j]>=need[ i][j
])t hen
21: Setcount ++
[EndI f]
22: If(count==randcompl et
ed[i
]==0)Then
23: Setcompl et
ed[i
]=1 //sett
rue
24: SetsafeSequence[i]
=i+1
25: Setav ail
[j
]+=al
loc[i
][
j]
26: Setcount :=0
[EndIf
]
[Endf or
]
[
Endf or ]
27:
RepeatStep28-30f orI:
=0t opstep1
28: If(
Compl eted[i
]==0)Then
29: Pri
nt“System i
sunsafestat
e”
30: Return1
[EndIf]
[EndFor]
31: Pri
nt“System i
si nsafest
ate”
32: Repeatstep33f orI:
=0topstep1
33: Pri
ntsafeSequence[
i]
[
EndFor]
34:
Stop
PROGRAM:
#include<st dio.h>
#include<coni o.h>
voidmai n()
{
i
nt
all
ocat ed[15][15],
max[15][
15],
need[15][
15]
,
avai
l
[15]
,
tres[
15]
,
wor
k[15]
,
flag[
15]
;
i
ntpno, r
no,i,
j,
prc,count
,t
,t
otal
;
count =0; /
/clrscr()
;
print
f("\nEnt ernumberofpr ocess:")
;
scanf ("
%d" ,
&pno) ;
print
f("\nEnt ernumberofr esources:"
);
scanf ("
%d" ,
&rno) ;
for(i
=1;i<=pno; i
++)
{
fl
ag[i
]=0;
}
pri
ntf(
"\nEnt ert otalnumber sofeachr esour
ces:"
);
for
(i
=1; i
<=r no;i
++)scanf ("%d",&tr
es[
i]
);
pri
ntf(
"\nEnt erMaxr esour cesforeachprocess:
");
for
(i
=1; i
<=pno; i++)
{
pri
ntf(
"\nforpr ocess%d: "
,i);
for
(j
=1; j
<=r no;j
++)
scanf("%d",
&max[ i]
[j
]);
}
pri
ntf(
"\nEnt eral l
ocatedr esourcesforeachprocess:
")
;
for
(i
=1; i
<=pno; i++)
{
pri
ntf(
"\nforpr ocess%d: "
,i);
for
(j
=1; j
<=r no;j
++)
scanf("%d",
&allocat ed[i
][
j]
);
}
pri
ntf(
"\nav ail
abl eresources:\n")
;
for
(j
=1; j
<=r no;j
++){
avai
l[j
]=0;total=0;
for
(i
=1; i
<=pno; i++)
{
tot
al+=all
ocat ed[ i
][
j]
;
}
avai
l[j
]=tr
es[ j
]-
total;
work[j
]=avail[
j]
;
pri
ntf(
"%d\ t",
wor k[j
]);
}
do
{
for
(i
=1; i
<=pno; i++)
{
for
(j
=1; j
<=r no;j
++)
{
need[i]
[j
]=max[ i][
j]
-all
ocated[
i]
[j
];
}
}
pri
ntf("
\nAl locatedmat ri
xMaxneed"
);
for(
i=1;
i<=pno; i
++)
{
pri
ntf("
\n")
;
for(
j=1;
j<=r no;j++)
{
pri
ntf("
%4d" ,all
ocat ed[i
][
j]
);
}
pri
ntf("
|"
);
for(
j=1;
j<=r no;j++)
{
pri
ntf("
%4d" ,max[ i
][j
]);
}
pri
ntf("
|"
);for (
j=1;j
<=r no;j
++)
{
pri
ntf("
%4d" ,need[i][
j]
);
}
}
prc=0;for(
i=1;i<=pno; i
++)
{
i
f(fl
ag[i
]==0)
{
prc=i;f
or(j
=1; j
<=r no;j++)
{
i
f(work[j
]<need[ i
][
j])
{
prc=0;break;
}
}
}
i
f(prc!
=0)br eak;
}if(prc!=0)
{
printf("
\nPr ocess%dcompl eted"
,i
);
count ++; pri
ntf("\
nAv ail
ablemat ri
x:"
);
for(j=1;
j<=r no;j
++)
{
wor k[j]
+=allocated[prc][
j]
;
all
ocat ed[prc][j
]=0;max[prc][
j]
=0;fl
ag[prc]
=1;pri
ntf
("%d"
,wor
k[j
])
;
}
}
}
whi le(
count !=pno&&pr c!
=0);
i
f(count ==pno)
printf("
\nThesy stem isinasaf estat
e!!
");
elsepr intf(
"\nThesy stem isinanunsaf estat
e!!
")
;
get ch();
}
Out put:
FI
LEALLOCATI
ONSTRATEGI
ES
Exper
iment-15
AI
M:Si mulatethepager eplacementalgor
it
hm FIFO
Descri
ption:
Thesi mplestpage-replacementalgori
thm isaFIFO al
gori
thm.The
fi
rst-i
n,f i
rst
-out( FIFO) page repl
acemental gor
it
hm isal ow-
overheadal gori
thm t hatrequi
resli
tt
lebookkeepi
ngont hepartof
the operat
ingsystem.
Theideai sobviousfrom t
hename–t heoperat
ingsyst
em keeps
tr
ackofal lthepagesi nmemor yinaqueue,wi t
hthemostr ecent
arr
ivalattheback,andtheoldestarr
ivali
nfr
ont.Whenapageneeds
tober epl
aced,t
hepageatt hefrontofthequeue(theol
destpage)is
sel
ected.
Whil
eFIFO ischeapandi nt
uit
ive,i
tper
for
mspoorl
yinpr
act
ical
appl
icat
ion.Thus,
iti
srar
elyusedinit
sunmodi
fi
edf
orm.
Al
gor
it
hm:
Step1:Start
Step2:Declareframe,avail
abl
e,count
=0,
n//
nfornumberofpages
Step3:Enterpages
Step4:Enterpagenumber s
Step5:Enternumberoff rames
Step6:Printpages
Step7:Printpagenumber s
Step8:Printnumberoff r
ames
Step9:Printref
erencestri
ng,pagefr
ames
Step10:countnoofpagef aul
ts
Step11:printpagefaul
ts
Step12:stop
Pr
ogram:
#i
ncl
ude<stdi
o.h>
i
ntmain()
{
i
nti,j
,
n, a[50],fr
ame[ 10],
no,k,
avai
l,
count=0;
pri
ntf("ENTERTHENUMBEROFPAGES: "
);
scanf("%d" ,&n);
pri
ntf("\nENTERTHEPAGENUMBER: ")
;
for(
i=1; i
<=n; i
++)
scanf (
"%d" ,&a[i]
);
pri
ntf("\nENTERTHENUMBEROFFRAMES: "
);
scanf("%d" ,&no) ;
for(
i=0; i
<no; i
++)
frame[ i
]=- 1;
j
=0;
pri
ntf("\trefst ring\tpagef r
ames\ n"
);
for(
i=1; i
<=n; i
++)
{
printf("%d\ t\t",
a[i
])
;
av ail
=0;
for(k=0; k<no; k++)
i
f(frame[ k]
==a[i])
av ail
=1;
i
f( avail==0)
{
frame[ j]
=a[i
];
j=(j+1) %no;
count ++;
for(k=0; k<no;k++)
pri
nt f("
%d\ t"
,f
rame[k]
);
}
printf("\n");
}
pri
ntf("PageFaul tIs%d" ,
count)
;
ret
urn0;
}
Exper
iment-16
AI
M: Si mul atet hepager epl acemental gorithm LRU
Description:
I ft heopt imalal gorithm i snotf easibl e,perhapsanappr oximat i
onof
theopt i
mal algorit
hm i spossi bl
e.
Thekeydi st i
ncti
onbet weent heFI FOandOPTal gori
thms(othert han
l
ooki ngbackwar dv er susf orwardi nt ime)ist hattheFIFOal gori
thm
usest het imewhenapagewasbr oughti nt
omemor y,
Wher east heOPTal gor it
hm usest het imewhenapagei stobeused.
I fweuset herecentpastasanappr oxi mationoft henearfuture,then
wecanr epl acet hepaget hathasnotbeenusedf ort
hel ongest
per i
odoft ime.
Thi sappr oachi st hel east-recent l
y-used( LRU)al gori
thm
Al
gor i
thm:
St ep1: Star t
St ep2: Decl areframe, available,
count =0,n/ /
nf ornumberofpages
St ep3: Enterpages
St ep4: Enterpagenumber s
St ep5: Enternumberoff rames
St ep6: Printpages
St ep7: Printpagenumber s
St ep8: Printnumberoff r
ames
St ep9: Printreferencest ri
ng, pagef rames
St ep10: countnoofpagef aults
St ep11: printpagef aul t
s
St ep12: stop
Program:
#i
nclude<st dio.h>
main()
{
i
ntq[ 20],p[50],c=0,c1,d,f
,i,
j,k=0,n,r,
t,
b[20],
c2[ 20];
pri
ntf("Ent ernoofpages: ");
scanf ("
%d" ,
&n) ;
pri
ntf("\nEnt ert hereferencest ring:"
);
for (
i=0;i
<n;i++)
scanf("%d" ,&p[i]
);
printf(
" \
nEnt ernooff rames:
")
;
scanf ("%d",
&f );
q[k]=p[k];
printf(
" \
n\t%d\ n" ,
q[k]
);
c++;
k++;
for(i
=1; i
<n;i
++)
{
c1=0;
for (
j=0;j
<f;j++)
{
if
(p[i
]!
=q[ j]
)
c1++;
}
if(c1==f)
{
c++;
if
(k<f)
{
q[k]
=p[ i
];
k++;
for(
j=0; j
<k;j++)
print f
("\t%d",
q[j
])
;
print f
("\n");
}
else
{
for(
r=0; r<f;
r++)
{
c2[r]=0;
for(j=i-1;
j<n;j
--
)
{
if(q[r]!
=p[j]
)
c2[r
]++;
el
se
break;
}
}
for
(r=0;r
<f ;
r++)
b[r
]=c2[r];
for
(r=0;r
<f ;
r++)
{
for(
j=r;
j<f;
j++)
{
if
(b[r
]<b[j])
{
t=b[r
];
b[r]
=b[j];
b[j
]=t;
}
}
}
for
(r=0;r
<f ;
r++)
{
i
f(c2[r]
==b[ 0]
)
q[r]
=p[i];
pri
ntf("
\ t
%d" ,
q[r
])
;
}
pri
ntf("
\n");
}
}
}
pri
ntf
("\
nThenoofpagef
aul
tsi
s%d"
,c)
;
}
Exper
iment-17
pri
ntf
("Enterno.off
il
es:
")
;
scanf
("%d" ,
&n);
for
(i
=0;i<n;
i++)
{
pri
ntf
("Ent erthest art
ingblockandsizeoffil
e%d:
",
i+1)
;
scanf
("%d%d" ,&sb[i]
,
&s[i]
);
pri
ntf
("Ent ertheblocksoccupi edbyfil
e%d:"
,i
+1);
scanf
("%d" ,
&m[ i
])
;
pri
ntf
("ent er blocksoff i
l
e%d: "
,i
+1)
;
for
(j
=0;j<m[ i]
;j
++)
scanf ("%d",
&b[ i
][
j]
);
}
pri
ntf
("File\tI
ndex\ tl
ength\n")
;
for
(i
=0;i<n;i++)
printf("
%d\ t%d\ t%d\n",
i+1,
sb[
i]
,m[i
])
;
pri
ntf
("Ent erfil
ename: "
);
scanf
("%d" ,
&x);
pri
ntf
("Filenamei s:%d\n",
x);
i
=x-1;
pri
ntf
("l
engt his:
%d" ,
sb[i
]);
pri
ntf
("Blockoccupi edare:")
;
for
(j
=0;j<m[ i]
;j
++)
pri
ntf
("%3d" ,
b[i
][
j]
);
OUTPUT:
Exper
iment-19
Pr
ogram:
#i
nclude<stdi
o.h>
main()
{
i
ntn,m[20],
i
,j,
sb[20]
,
s[20]
,
b[20]
[20]
,
x;
pri
ntf
("Enterno.off
il
es:
")
;
scanf
("%d" ,
&n);
for
(i
=0;i<n;
i++)
{
pri
ntf
("Enterstartingblockandsi zeoff
il
e%d:",
i
+1)
;
scanf
("%d%d" ,&sb[ i
],
&s[i
]);
pri
ntf
("Enterblocksoccupi edbyf il
e%d:
",
i+1)
;
scanf
("%d" ,
&m[ i
]);
pri
ntf
("enterblocksoff i
le%d: ",
i+1);
for
(j
=0;j<m[i]
;j
++)
scanf
("%d" ,
&b[i]
[j
]);
}
pri
ntf
("\nFil
e\tindex\ t
lengt h\n");
for
(i
=0;i<n;i
++)
{
pri
ntf
("%d\ t
%d\ t%d\ n"
,i
+1, sb[i]
,
m[ i]
);
}
pri
ntf
("\nEnterfil
ename: ");
scanf
("%d" ,
&x);
pri
ntf
("fil
enamei s:%d\n",x) ;
i
=x-1;
pri
ntf
("Indexis:%d" ,
sb[i
]);
pri
ntf
("Blockoccupi edar e:" )
;
for
(j
=0;j<m[i]
;j
++)
print
f("
%3d" ,b[i
][
j]
);
}
OUTPUT:
Exper
iment-20
str
uctfil
e
{
charfname[10];
i
ntstart,
si
ze,bl
ock[
10]
;
}
f[
10];
mai
n()
{
i
nti
,j
,
n;
pri
ntf("Enterno.off i
les:"
);
scanf (
"%d",
&n);
for
(i=0;i<n;
i++)
{
pri
ntf("Enterfil
ename: ");
scanf (
"%s",f[
i]
.fname) ;
pri
ntf("Enterst art
ingbl ock:"
);
scanf (
"%d" ,
&f[i
].star t
);
f[
i]
.block[0]=f[
i].st
ar t;
pri
ntf("Enterno. ofbl ocks:");
scanf (
"%d" ,
&f[i
].size);
pri
ntf("Enterblocknumber s:"
);
for
(j=1;j<=f[
i]
.size;j++)
{
scanf (
"%d" ,
&f[i
].block[ j
]);
}
}
pri
ntf("Fil
e\tstart\tsize\tblock\n"
);
for
(i=0;i<n;
i++)
{
pri
ntf("%s\t%d\ t
%d\ t
",f
[i]
.f
name, f[
i]
.st
art
,f
[i
].
size)
;
for
(j=1;j<=f[
i]
.size-1; j
++)
pri
ntf("%d---
>",f[i
].
bl ock[j]
);
pri
ntf("%d---
>",f[i
].
bl ock[j]
);
pri
ntf("\n")
;
}
}