Você está na página 1de 8

ProcessStatesandMemoryManagement

LearningObjectives
Attheendofthislabyoushouldbeableto:
Demonstratethreemainstatesofprocesses
Identifyallowedanddisallowedprocessstatetransitions
Showentryinandexitfromwaitingstateontwotypesofevents
Demonstratethreeplacementstrategiesusedinmemorymanagement
Demonstratevirtualmemoryactivityaspartofmemorymanagement

TutorialExercises
InitialPreparation
ThistutorialusestheCPUOSSimulatorsoftwarewhichsimulatesthefunctionsofatypical
modernCPUandatypicalOperatingSystem(OS).Tostartthesimulator,doubleclickonthe
simulatoriconorname.ThisstartsuptheCPUsimulatorsmainwindow.NextgototheOS
simulatorsmainwindowbyfirstselectingtheAdvancedtabandthenclickingontheOS0
button.
Pleasereadtheinstructionscarefullybeforeyouattempttheexercisesusingthesimulator.If
atanypointthesimulatorcrashes,restartitandcarryonfromwhereyouleft.

LO1:InvestigatingProcessStatesandStateTransitions
Inthissectionwelllookatdifferentprocessstatesandthetransitionsfromonestateto
another.Someofthesetransitionsareallowedandsomearenot,i.e.illegaltransitions.
IntheOSSimulatorwindowselecttheViewstab.ClickontheVIEWPROCESSSTATESbutton.
YoullseetheProcessStateswindowdisplayed.Thiswindowshowsagraphicalrepresentation
oftheReadyQueuewheretheprocessisinReadyState.Italsoshowsarepresentationofthe
CPUwheretheprocessisinRunningState.AlsoshownistherepresentationoftheWaiting
QueuewheretheprocessisinWaitingState.
InthiswindowchecktheStayontopandAnimatecheckboxes.BackintheOSSimulator
windowselecttheProgramtabandloadForeverLoopingprogram.Now,selecttheProcesstab
andcreateasingleprocessfromthisprogrambyclickingontheCREATENEWPROCESSbutton.
IntheProcessStatewindowyoullseeasingleprocessattheheadoftheReadyQueue.This
processisrepresentedbyacolouredbox.Itsprocessnumberisdisplayedonthebox.Nowwe
arereadytoinvestigateprocessstates.Todothis,followtheactionsinthetablebelowand
makeanoteoftheresultsofyouractions.
Notethattheactionswillinvolvedragginganddroppingtheprocessboxintovariousareas.If
thedragginganddroppingactionresultsinafailurethenthefollowingmessageisdisplayedat
1
thebottomofthewindow:***ERROR:Illegalstatetransition!Thismeansthetransitionisnot
allowed.
Finally,beforeyoustart,intheOSSimulatorwindowchecktheSuspendonstatechangecheck
boxintheREADYPROCESSESareaonlyandslidetheCPUSpeedslidertothefastestposition.
TheSuspendonstatechangesuspendsthesimulatoranddisplaysamessagewhenevera
processchangesitsstate.YoullneedtoclickontheRESUMEbuttontocontinue.Nowexecute
theactionsbelowandfillinthethreecolumnsontheright:

Actiontotake
Resultant
State
Success
(YorN)
Fail
(YorN)
1.Dragprocesstothewaitingqueue(i.e.putinwaitingstate)
2.DragprocesstotheProcessBin(i.e.terminateit)
3.DragprocesstotheCPU0box(i.e.runit)
4.Dragprocesstothewaitingqueue
5.DragprocesstotheCPU0box
6.DragprocesstotheProcessBin
7.DragprocesstotheReadyQueue(i.e.putitinreadystate)
8.ClickontheRESUMEbuttoninOSControltab
9.Dragprocesstothereadyqueue
10.ClickontheRESUMEbuttoninOSControltab
11.DragprocesstoProcessBin

Next,basedontheresultsabove,fillinonlythelegalstatechangesinthetablebelow:

FromState ToState



2
ThestatetransitiondiagrambelowshowsprocessstatesandALLthetransitionsbetween
them.Sometransitionsarenotlegal.Removethearrowsthatrepresenttheillegaltransitions,
i.e.theonesthatarenotallowedbytheOS:

RUNNING
READY
WAITING

LO2:InvestigatingProcessStatesWaitingQueueandEvents
IntheOSSimulatorselecttheProgramtabandloadtheprogramOSQueuesDemo.This
programrunsontheCPUforashortwhileandthenwaitsfor3secondsbeforerunningonthe
CPUagain.Thisrepeatsforever.Yourtaskistoobservethisactivity.First,youneedtocreatea
singleprocessofthisprogramusingtheCREATENEWPROCESSbuttonintheProcesstab.
Next,checkALLthreeSuspendonstatechangecheckboxes(therearethreeoftheminOS
simulator).Next,starttheschedulerbyclickingontheSTARTbutton.Makeanoteinthetable
belowofthesequenceofthenextsixstatesoftheprocess(Note:Youllneedtoclickonthe
RESUMEbuttonintheProcesstabaftereachdisplayofthemessagewindowjustafterthe
statechange).Thestaringstateandtheexpectedendingstatearefilledinforyou.
States
1.RUNNING
2.
3.
4.
5.
6.READY

NOTE:Ifyouneedtorestart,youcancleartheprocessbyusingtheKILLbutton(iftheprocess
isrunning)ortheREMOVEbutton(iftheprocessisinReadyQueueorinthewaitingqueue).
3
TheaboveexampleisademonstrationofaprocessgoingintotheWAITINGstateasaresultof
suspendingitselffor3seconds(thatshowitwasprogrammed).Next,wearegoingtolookat
anothermethodofsuspendingaprogram.IfaRUNNINGprogramiswaitingonaninputtheOS
willforceittogointotheWAITINGstateuntilitreceivestheinput.Todemonstratethis,dothe
following:
LoadtheprogramWaitOnReadDemo.Thencreateasingleprocessofthisprogram.Makesure
allthreeSuspendonstatechangecheckboxesareNOTchecked.AlsomakesuretheCPU
Speedsliderisinthefastestspeedposition.Next,intheCPUSimulatorwindow,selectthe
AdvancedtabandclickontheINPUTOUTPUTbutton.YoushouldnowseetheConsole
window.InthiswindowclickontheSHOWKBDbuttonwhichwillbringupasmallwindow
representingakeyboard.ChecktheStayontopcheckboxontheConsolewindow.Backinthe
OSSimulatorclickontheSTARTbuttontostartthescheduler.
Waituntila>characterisdisplayedintheConsolewindow.Nowchecktoseewhatstatethe
processisin(lookattheOSsimulatorwindow)andmakeanoteofthisbelow:

UsingthesmallKeyboardwindowclickonanybuttonandobservewhathappensintheOS
simulator.Makeanotebelowofwhatstatetheprocessgoesintoimmediatelyafterakeyboard
buttonisclicked:

Theabovewillberepeatedasmanytimesasyoulikeuntilyouentera*(star)characterto
terminatethisprogram.
TheabovedemonstrationwasabouttheprocessbeingsuspendedbytheOSassoonasitwaits
foraninput,i.e.itstaysintheWAITINGstate.Assoonasaninputissupplieditstartsrunning
againuntilitonceagainwaitsforanotherinput.TheOSmakessurethatallprocesseswaiting
forinputsareputintothewaitingstate.Oncetheinputisprovided,i.e.theinput/outputevent
iscompleted,awaitingprocesscomesoutofthewaitingstate.

LO3:LookingatMemoryPlacementPolicies
Theaboveexercisesconcentratedontheprocessstatesandtransitionsbetweenthem.The
followingexercisesareaboutmemorymanagement.
OnceaprocessiscreatedtheOSneedstofindspaceinprimarymemory(i.e.theRAM)forthe
processsdataandcode.Therearethreemainmethodsemployed:1)Firstfit,2)Bestfitand3)
Worstfit.Todemonstratehowtheseworkfollowtheinstructionsbelow:
First,makesurenoprogramsareinmemory.Removeanyprocesseswhichmayhappentobein
anyofthequeues(usetheREMOVEbuttons)andalsoremovealltheloadedprogramsby
selectingtheprogramandclickingontheREMOVEbuttonintheProgramtab.Wearenow
startingfromscratch!
4
IntheOSSimulatorwindow,selecttheViewstabandclickontheVIEWMEMORYbutton.
YoullseetheMainMemorywindowdisplayed.Now,makesuretheFramesdropdownlistis
setto1.ClickontheADDbuttonrepeatedlyuntilallmemoryisallocated(needs10clicks).We
willnowproceedtomakeholesinthememory.Pleasefollowtheinstructionsbelowexactly
intheorderlisted:
UsingtheFrameNodropdownlistsetitto2thenclicktheREMOVEbuttontoremoveit.
SettheFrameNoto3andremoveit.
SettheFrameNoto5andremoveit.
SettheFrameNoto7andremoveit.
SettheFrameNoto8andremoveit.
SettheFrameNoto9andremoveit.

Youshouldnowhaveonlyframes0,1,4and6allocatedwithgapsoffreememoryspacesof
varioussizesbetweenthem.
Now,setthePlacementPolicydropdownlisttoFirstFit.LoadtheprogramForeverLoopingif
itsnotalreadyloaded.Createasingleprocessofit.ObserveinwhichgapinMainMemoryits
memoryisallocated.NowremovetheprocessbyclickingontheREMOVEbuttonintheREADY
PROCESSESarea(Note:Donotremovetheprogram,justtheprocess).
Next,setthePlacementPolicydropdownlisttoBestFit.Createanewprocessandobservein
whichgapinMainMemoryitsmemoryisallocated.Onceagainremovethisprocess.
Finally,setthePlacementPolicydropdownlisttoWorstFit.Createanewprocessandobserve
inwhichgapinMainMemoryitsmemoryisallocated.Againremovethisprocess.
Now,beforeyoucontinueclearthememorybyclickingtheRESETbutton.
Fillinthetablebelow,againsteachofthethreemethods,explaininghoweachmethodworks:
FirstFit
placement
method

BestFit
placement
method

WorstFit
placement
method

5
LO4:LookingatVirtualMemoryandSwapping
WhenaprocessiscreateditisalsoallocatedsomememoryfromfreememoryspaceinRAM.If
thismemoryrunsoutoffreespacethentheprocesssmemorywillbeswappedontoa
secondarystoragelikeaharddrive.Inthiscase,theharddrivewillberegardedasanextension
oftheprimarymemory(i.e.virtualmemory).However,whenthisprocessisscheduledtorun
nextthenitsswappedoutmemorymustbebroughtintoRAMfirst.Thisactionwillusually
resultinanotherprocesssmemoryspacebeingswappedoutontotheharddriveinorderto
makespacefortheincomingprocess.Thefollowingexercisedemonstratesthisactivity.
IntheOSSimulatorwindowselecttheViewstab.ClickontheVIEWUTILIZATIONbutton.This
willshowtheResourceUtilisationwindow.HereyoucanseetheCPUandmemoryutilization
asbarcharts(bothRAMandvirtualmemoryshown).Next,clickontheVIEWMEMORY
buttonintheOSSimulator.YoullnowseetheMainMemory(RAM)window.Inthiswindow
firstuncheckthePagingEnabledcheckbox.Thisactionisimportantsomakesureitsdone!
NextloadtheprogramOSQueuesDemoifitsnotalreadyloaded.Createthefollowingthree
processesofthisprogramwiththespecifiedpagesizes(seethetablebelow).Youcanselectthe
processpagesizeusingthedropdownlistPagesintheProcesstabwhileyouarecreatinga
process.AfteryoucreateeachprocessobservethevaluesintheResourceUtilisationwindow
andnotethemdowninthefollowingtable:

Process Pages Free Alloc Swap


P1 4
P2 5
P3 3

CommentonwhyP3smemoryisswappedoutbyreferringtothetableabove:

Now,makesureinOSSimulatortheSuspendonstatechangecheckboxisselectedin
RUNNINGPROCESSareaonly.AlsomakesuretheCPUSpeedsliderisinthefastestposition.
Next,clickontheSTARTbuttonintheOSControltab.Whenthemessagewindowappears
indicatingachangeinstate,makeanoteofthethreememoryutilisationvaluesinthetable
belowagainstthecurrentlyrunningprocess.RestarttheOSbyclickingontheRESUMEbutton
inOSControltab.Carryondoingthisforallprocessesindicatedinthetablebelowmakinga
noteofthethreevaluesinthecorrespondingcolumns:

Process Free Alloc Swap


P1
P2
P3
P1
P2
P3

When you finish, uncheck the Suspend on state change check box and click on the RESUME
button.AstheprocessesstartrunningclickontheKILLbuttontostopthemonebyone.When
allthreeprocessesterminatemakeanoteofthefollowingresourceutilizationvalues:

CPU% Free Alloc Swap


The exercises above have been designed to demonstrate the basic principles of OS memory
management.Reviewwhathasbeendoneinthelastsectionaboveandmakeyourcomments
onthewayvirtualmemorymanagementfunctionsintheboxbelow:


7
8
AppendixProgramsourcesusedinthistutorial

If you feel adventurous you can copy and paste each of the programs below in the integrated
compilers source editor and then compiling them one by one (use the COMPILER button in
the CPU Simulator for this). This tutorial uses the corresponding precompiled code
downloaded. Use the NEW button in the compiler to create a new editor for each of the
programsandcompilethemindividually.

pr ogr amOSQueuesDemo %St ar t of pr ogr am


whi l e t r ue %For ever l oop
f or n = 1 t o 15 %Repeat 15 t i mes
i = 1 %J ust somet hi ng t o do!
next %End of r epeat l oop
wai t ( 3) %Suspend pr ogr amf or 3 secs
wend %End of f or ever l oop
end %End of pr ogr am

pr ogr amWai t OnReadDemo
r egvar d i nt eger %Keep i nput i n a r egi st er

whi l e t r ue
f or n = 1 t o 15 %Thi s l oop keeps CPU busy
i = 1 %J ust somet hi ng t o do!
next

wr i t e( " > " ) %Di spl ay t hi s pr ompt
r ead( d) %Wai t f or keyboar d i nput
wr i t el n( d) %Di spl ay i nput char act er
i f d = 42 t hen %Test i f end of pr ogr am
br eak %End pr ogr ami f a * char act er
end i f
wend
end


pr ogr amFor ever Loopi ng
whi l e t r ue %Do a f or ever l oop
n = 1 %Does not hi ng usef ul but
wend %keeps t he CPU busy
end

Você também pode gostar