Unit 3.1 The Structure of Process, Process Control, Process Scheduling.ppt

AnilkumarBrahmane2 23 views 35 slides May 17, 2024
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

This is for learning purpose. Engineering students can use it for smooth understanding of Operating System.


Slide Content

Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Prof. A. V. Brahmane
Assistant Professor
E-mail : [email protected]
Contact No: 91301 91301 Ext :145, 9922827812
Subject-Operating System and Administration (CO2013)
Unit III-The Structure of Process, Process Control and
Process Scheduling

Content
•Process state and transitions,
•Layout of the system memory,
•Context of the process,
•Saving the context of the process,
•Manipulation the process address space,
•Sleep,
•Process creation,
•Signal,
•Process termination, Awaiting the process termination,
•Invoking other program, Process Scheduling
•Case Study -Access Control, Rootly Powers and Controlling Processes
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Process State and transition
•The complete set of process states:
•Executing in user mode.
•Executing in kernel mode.
•Ready to run.
•Sleeping in memory.
•Ready to run, but in swap space (covered later).
•Sleeping in swap space.
•Preempted. (the process is returning from kernel to user mode, but the kernel preempts it and
does a context switch to schedule another process. Very similar to state 3)
•Newly created. Not ready run, nor sleeping. This is the start state for all processes expect process
0.
•The process executedexitsystem call and is in thezombiestate. The process no longer exists, but
it leaves a record containing an exit code and some timing statistics for its parent process to
collect. The zombie state is the final state of a process.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Process State and its Tansistions
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Kernel uses two tables Process Table and U area Table
•The fields in the process table are the following:
•State of the process
•Fields that allow the kernel to locate the process and its u-area in main memory or in
secondary storage.
•Several user identifiers (user IDs or PIDs) specify the relationship of processes to each
other.
•Event descriptor when the process issleeping.
•Scheduling parameters allow the kernel to determine the order in which processes move
to the stateskernel runninganduser running.
•A signal fields enumerates the signals sent to a process but not yet handled.
•Various timers give process execution time and kernel resource utilization. These are
used for calculation of process scheduling priority. One field is a user-set timer used to
send an alarm signal to a process.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

U-Area Table
•The u-area contains these fields :
•A pointer in the process table identifies the entry that corresponds to the u-area.
•The real and effective user IDs determine various privileges allowed the process, such as file access rights.
•Timer fields record the time the process spent executing in user mode and in kernel mode.
•An array indicates how the process wishes to react to signals.
•The control terminal field identifies the "login terminal" associated with the process, if one exists.
•An error field records errors encountered during a system call.
•A return value field contains the result of system calls.
•I/O parameters describe the amount of data to transfer, the address of the source (or target) data array in
user space, file offsets for I/O, and so on.
•The current directory and current root describe the file system environment of the process.
•The user file descriptor table records the files the process has open.
•Limit fields restrict the size of a process and the size of a file it can write.
•A permission modes field masks mode settings on files the process creates.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Layout of System Memory
•Thephysicalmemoryisaddressable.Startingfromoffset0,goinguptotheamountof
physicalmemory.AprocessinUNIXcontainsthreesections:text,data,andstack.Text
sectioncontainstheinstructions.Thoseinstructionscouldrefertootheraddresses,for
example,addressesofdifferentsubroutines,addressesofglobalvariablesinthedata
section,ortheaddressesoflocaldatastructuresonthestack.Iftheaddressesgenerated
bythecompilerweretobetreatedasphysicaladdresses,itwouldbeimpossibletorun
morethanoneprocessatatime,becausetheaddressescouldoverlap.Evenifthe
compilertriestoaddressthisproblembyusingheuristics,itwouldbedifficultand
impractical.
•Tosolvethisproblem,thekerneltreatstheaddressesgivenbythecompilerasvirtual
addresses.Andwhentheprogramstartsexecuting,thememorymanagementunit
translatesthevirtualaddressestophysicaladdresses.Thecompilerdoesn'tneedto
knowwhichphysicaladdressestheprocesswillget.Forexample,twoinstancesofa
sameprogramcouldbeexecutinginmemoryusingthesamevirtualaddressesbut
differentphysicaladdresses.Thesubsystemsofthekernelandthehardwarethat
cooperatetotranslatevirtualtophysicaladdressescomprisethememory
managementsubsystem.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Regions
•TheUNIXsystemdividesitsvirtualaddressspaceinlogicallyseparatedregions.
Theregionsarecontiguousareaofvirtualaddressspace.Aregionisalogically
distinctobjectwhichcanbeshared.Thetext,data,andstackareusuallyseparate
regions.Itiscommontosharethetextregionamonginstancesofasameprocess.
•Theregiontableentriescontainthephysicallocationsatwhichtheregionis
spread.Eachprocesscontainsaprivateperprocessregionstable,called
apregion.Thepregionentrycontainsapointertoanentryintheregiontable,
andcontainsstartingvirtualaddressoftheregion.pregionarestoredinprocess
table,oru-area,oraseparatelyallocatedmemoryspace,accordingtothe
implementation.Thepregionentriescontaintheaccesspermissions:read-only,
read-write,orread-execute.Thepregionandtheregionstructureisanalogousto
filetableandthein-coreinodetable.Butsince,pregionsarespecifictoa
process,pregiontableisprivatetoaprocess,howeverthefiletableisglobal.
Regionscanbesharedamongstprocesses.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

An example of regions:
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Pages and Page Tables
•Inamemorymodelbasedapages,thephysicalmemoryisdividedintoequal
sizedblockscalledpages.Pagesizesareusuallybetween512bytesto4Kbytes,
andaredefinedbythehardware.Everymemorylocationcanbeaddressbya
"pagenumber"and"byteoffset"inthepage.Forexample,amachinewith2^32
bytesofmemoryhaspagesofsize1Kbytes(2^10),thenitwillhave2^22pages.
•Whenkernelassignsphysicalpagesofmemorytoaregion,itneednotassignthe
pagescontiguouslyorinanyparticularorder.Justlikediskblocksarenotassigned
contiguouslytoavoidfragmentation.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

•The kernel maintains a mapping of logical to physical page numbers in
a table which looks like this:
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

•Thesetablesarecalledpagetables.Regiontableentryhaspointerstopage
tables.Sincelogicaladdressspaceiscontiguous,itisjusttheindexintoanarray
ofphysicalpagenumbers.Thepagetablesalsocontainhardwaredependent
informationsuchaspermissionsforpages.Modernmachineshavespecial
hardwareforaddresstranslation.Becausesoftwareimplementationofsuch
translationwouldbetooslow.Hence,whenaprocessstartsexecuting,thekernel
tellsthehardwarewhereitspagetablesreside.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

•Forbeinghardwareindependent,letusassumethatthehardwarehasregister
triples(inabundance)whichthekernelusesformemorymanagement.Thefirst
registerinthetriplecontainstheaddressofthepagetable,thesecondregister
containsthefirstvirtualaddressmappedbythepagetable,andthethirdregister
containscontrolinformationsuchasnumberofpagesinpagetablesandpage
accesspermissions.Whenexecutingaprocess,thekernelloadssuchregister
tripleswiththedatainthepregionentries.
•Ifaprocessaccessesanaddressoutsideitsvirtualaddressspace,anexceptionis
generated.Supposeaprocesshas0to16Kbytesofaddressspaceandthe
processaccessesthevirtualaddress20K,anexceptionwillgenerated,anditis
caughtbytheoperatingsystem.Similarly,ifaprocesstriestoaccessapage
withouthavingenoughpermission,anexceptionwillbegenerated.Insuchcases,
processnormallyexit.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Layout of the Kernel
•Evenifthekernelexecutesinthecontextofaprocess,itsvirtual
addressspaceisindependentofprocesses.Whenthesystemboots
up,thekernelisloadedintomemoryandnecessarypagetablesand
registersareloadedwithappropriatedata.Manyhardwaresystems
sliceaprocess'addressspaceintomanysections,userandkernel
beingtwoofthem.Wheninusermode,accesstokernelpagetables
isprohibited.Whentheprocessswitchestokernelmode,onlythenit
canaccesskernelpagetables.Somesystemimplementationstryto
allocatethesamephysicalpagestothekernel,keepingthetranslation
function,anidentityfunction.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Changing Mode from User to Kernel
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

The U area
•Evenifeveryprocesshasau-area,thekernelaccessesthemthrough
itsuvariable.Itneedstoaccessonlyoneu-areaatatime,ofthecurrently
executingprocess.Thekernelknowswherethepagetableentryoftheu-areais
located,therefore,whenaprocessisscheduled,thephysicaladdressofitsu-area
isloadedintokernelpagetables.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Memory Map of U area in the Kernel
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

The Context of a Process
•Thecontextofaprocessconsistsof:
•Contentsofits(user)addressspace,calledasuserlevelcontext
•Contentsofhardwareregisters,calledasregistercontext
•Kerneldatastructuresthatrelatetotheprocess,calledassystemcontext
•Userlevelcontextconsistsoftheprocesstext,data,userstackandshared
memorythatisinthevirtualaddressspaceoftheprocess.Thepartwhichresides
onswapspaceisalsopartoftheuserlevelcontext.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

The register context consists of the following components:
•Programcounterspecifiesthenextinstructiontobeexecuted.Itisanaddressin
kernelorinuseraddressspace.
•Theprocessorstatusregister(PS)specifieshardwarestatusrelatingtheprocess.
Ithassubfieldswhichspecifyiflastinstructionoverflowed,orresultedin0,
positiveornegativevalue,etc.Italsospecifiesthecurrentprocessorexecution
levelandcurrentandmostrecentmodesofexecution(suchaskernel,user).
•Thestackpointerpointstothecurrentaddressofthenextentryinthekernelor
userstack.Ifitwillpointtonextfreeentryorlastusedentryitdependentonthe
machinearchitecture.Thedirectionofthegrowthofstack(towardnumerically
higherorloweraddresses)alsodependonmachinearchitecture.
•Thegeneralpurposeregisterscontaindatageneratedbytheprocessduringits
execution.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

The System Level
•The system level context has a "static part" and a "dynamic part". A
process has one static part throughout its lifetime. But it can have a
variable number of dynamic parts.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

•The static part consists of the following components:
•The process table entry
•The u-area
•Pregion entries, region tables and page tables.
•The dynamic part consists of the following components:
•The kernel stack contains the stack frames the kernel functions. Even if all processes
share the kernel text and data, kernel stack needs to be different for all processes as
every process might be in a different state depending on the system calls it executes. The
pointer to the kernel stack is usually stored in the u-area but it differs according to
system implementations. The kernel stack is empty when the process executes in user
mode
•The dynamic part of the system level context consists of a set of layers, visualized as a
last-in-first-out stack. Eachsystem-level context layercontains information necessary to
recover the previous layer, including register context of the previous layer.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Components of the Context of a Process
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Saving the Context of a Process
•InterruptsandExceptions
•Thesystemisresponsibleforhandlinginterruptsandexceptions.Ifthesystemis
executingatalowerprocessorexecutionlevel,whenaninterruptsoccurs,the
kernelacceptstheinterruptbeforedecodingthenextinstructionandthenraises
theprocessorexecutionleveltoblockotherinterruptsofthatorlowerlevel.It
handlestheinterruptbyperformingfollowingsequenceofoperations:
•Itsavesthecurrentregistercontextoftheexecutingprocessandcreates(pushes)
anewcontextlayer.
•Thekerneldeterminesthesource(cause)oftheinterrupt,andifapplicable,unit
number(suchaswhichdrivecausedtheinterrupt).Whenthesystemreceivesan
interrupt,itgetsanumber.Itusesthatnumberasanindexintotheinterrupt
vector,whichstorestheactionstobetaken(interrupthandlers)wheninterrupts
occur.Exampleofinterruptvector:
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

•Thekernelinvokestheinterrupthandler.Thekernelstackofthenewcontext
layerislogicallydistinctfromthekernelstackofthepreviouscontextlayer.Some
implementationsusetheprocesseskernelstacktostorethestackframeofan
interrupthandler,whilesomeimplementationsuseaglobalinterruptstackfor
theinterrupthandlerswhichareguaranteedtoreturnwithoutacontextswitch.
•Thekernelreturnsfromtheinterrupthandlerandexecutesasetofhardware
instructionswhichrestorethepreviouscontext.Theinterrupthandlermayaffect
thebehavioroftheprocessasitmightmodifythekerneldatastructures.But
usually,theprocessresumesexecutionasiftheinterruptneveroccurred.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Sample Interrupt Vector
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Algorithm for Handling Intrrupts
•The algorithm for interrupt handling is given below:
•/* Algorithm: inthand
•* Input: none
•* Output: none
•*/
•{
• save (push) current context layer;
• determine interrupt source;
• find interrupt vector;
• call interrupt handler;
• restore (pop) previous context layer;
•}
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

System Call Interface
•Thelibraryfunctionssuchasopen,read,etc.inthestandardClibrary
arenotactuallysystemcalls.Thosearenormalfunctionsandnormal
functionscannotchangethemodeofexecutionofthesystem.These
functionsinvokeaspecialinstructionwhichmakesthesystemchange
itsexecutionmodetokernelmodeandstartexecutingthesystemcall
code.Theinstructioniscalledasoperatingsystemtrap.Thesystem
callsareaspecialcaseofinterrupthandling.Thelibraryroutinespass
theanumberuniqueforeachsystemcall,astheparametertothe
operatingsystemtrapthroughaspecificregisteroronthestack.
Throughthatnumber,thekerneldetermineswhichsystemcallto
execute.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Algorithm Syscall
•/* Algorithm: syscall
•* Input: system call number
•* Output: result of system call
•*/
•{
• find entry in the system call table corresponding to the system call number;
• determine number of parameters to the system call;
• copy parameters from the user address space to u-area;
• save current context for abortive return;// studied later
• invoke system call code in kernel;
• if (error during execution of system call)
• {
• set register 0 in user saved register context to error number;
• turn on carry bit in PS register in user saved register context;
• }
• else
• set register 0, 1 in user saved register context to return values from system call;
•}
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Context Switch
•Asseenpreviously,thekernelpermitsacontextswitchunder4situations:
•Whenaprocesssleeps
•Whenaprocessexits
•Whenaprocessreturnsfromasystemcalltousermodebutisnotthemost
eligibleprocesstorun.
•Whenaprocessreturnsfromaninterrupthandlertousermodebutisnotthe
mosteligibleprocesstorun.
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Manipulation of the Process Address Space
•The region table entry contains the following information:
•The inode of the file from which the region was initially loaded.
•The type of the region (text, shared memory, private data, or stack).
•The size of the region.
•The location of the region in physical memory.
•The state of the region:
•Locked, in demand, being loaded into memory, valid, loaded into memory
•The reference count, giving the number of processes that reference the region
•The operations that manipulate regions are:
•lock a region
•unlock a region
•allocate a region
•attach a region to the memory space of a process
•change the size of a region
•load a region from a file into the memory space of a process
•free a region
•detach a region from the memory space of a process, and duplicate the contents of a region
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Sleep
•Processessleepinsideofsystemcallsawaitingforaparticularresourceorevenif
apagefaultoccurs.Insuchcases,theypushacontextlayeranddoacontext
switch.Thecontextlayersofasleepprocessareshownbelow:
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Algorithms for Sleep
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

Algorithm for Weak up
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

•Thank you …..
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
Tags