OS_Unit_II_Ch3 Process and CPU Scheduling

SDivya19 75 views 48 slides May 31, 2024
Slide 1
Slide 1 of 48
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
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48

About This Presentation

This ppt covers following topics,
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems


Slide Content

Process and CPU
Scheduling
Unit_II
Chap_3

Contents
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server
Systems

Objectives
Tointroducethenotionofaprocess--a
programinexecution,whichformsthe
basisofallcomputation
Todescribethevariousfeaturesof
processes,includingscheduling,
creationand termination,and
communication
Toexploreinterprocesscommunication
usingsharedmemoryandmessage
passing
Todescribecommunicationinclient-
serversystems

Process Concept
Anoperatingsystemexecutesa
varietyofprograms:
◦Batchsystem–jobs
◦Time-sharedsystems–userprograms
ortasks
Process–aprograminexecution;
processexecutionmustprogress
insequentialfashion

Process Concept (Cont.)
Programispassiveentitystoredon
disk(executablefile),processis
active
oProgrambecomesprocesswhen
executablefileloadedintomemory
Memory

Oneprogramcanbeseveralprocesses
◦Considermultipleusersexecutingthesameprogram

The program code, also called text section
Currentactivityincludingprogram
counter,processorregisters
Stackcontainingtemporarydata
Functionparameters,returnaddresses,
localvariables
Datasectioncontainingglobalvariables
Heapcontainingmemorydynamically
allocatedduringruntime
Multiple parts
Process in Memory

Process States
As a process executes, it changes
◦new:The process is being created
◦running:Instructions are being executed
◦waiting:The process is waiting for some event to
occur
◦ready:The process is waiting to be assigned to a
processor
◦terminated:The process has finished execution
state

Diagram of Process State

Process Control Block (PCB)
Information associated with each
process
(also called task control block)
Process state –running, waiting,
etc
Program counter –location of
instruction to next execute
CPU registers –contents of all
process-centric registers
CPU scheduling information-
priorities, scheduling queue
pointers
Memory-management information –
memory allocated to the process
Accounting information –CPU
used, clock time elapsed since
start, time limits
I/O status information –I/O devices
allocated to process, list of open
files

CPU Switch From Process to
Process

Threads
Sofar,processhasasinglethreadof
execution
Considerhavingmultipleprogram
countersperprocess
◦Multiplelocationscanexecuteatonce
Multiplethreadsofcontrol→threads
Mustthenhavestorageforthread
details,multipleprogramcountersin
PCB
Seenextchapter

Process Scheduling
MaximizeCPUuse,quicklyswitch
processesontoCPUfortimesharing
Processschedulerselectsamong
availableprocessesfornextexecutionon
CPU
Maintainsschedulingqueues of
processes
◦Jobqueue–setofallprocessesinthe
system
◦Readyqueue–setofallprocessesresidingin
mainmemory,readyandwaitingtoexecute
◦Devicequeues–setofprocesseswaitingfor
anI/Odevice
◦Processesmigrateamongthevariousqueues

Ready Queue And Various I/O Device Queues

Representation of Process Scheduling
Queuing diagram represents queues, resources, flows

Schedulers
1. Short-term scheduler (or CPU scheduler)–selects which
process should be executed next and allocates CPU
◦Sometimes the only scheduler in a system
◦Short-term scheduler is invoked frequently (milliseconds) (must be
fast)
II. Long-term scheduler (or job scheduler)–selects which
processes should be brought into the ready queue
◦Long-term scheduler is invoked infrequently (seconds, minutes) 
(may be slow)
◦The long-term scheduler controls the degree of multiprogramming
Processes can be described as either:
◦I/O-bound process–spends more time doing I/O than computations,
many short CPU bursts
◦CPU-bound process –spends more time doing computations; few
very long CPU bursts
Long-term scheduler strives for good process mix

Addition of Medium Term
Scheduling
III. Medium-term scheduler can be added if degree of multiple
programming needs to decrease
Remove process from memory, store on disk, bring back in
from disk to continue execution: swapping

Multitasking in Mobile Systems
Somemobilesystems(e.g.,earlyversionofiOS)allow
onlyoneprocesstorun,otherssuspended
Duetoscreenrealestate,userinterfacelimitsiOS
providesfora
◦Singleforegroundprocess-controlledviauserinterface
◦Multiplebackgroundprocesses–inmemory,running,butnot
onthedisplay,andwithlimits
◦Limitsincludesingle,shorttask,receivingnotificationof
events,specificlong-runningtaskslikeaudioplayback
Androidrunsforegroundandbackground,withfewerlimits
◦Backgroundprocessusesaservicetoperformtasks
◦Servicecankeeprunningevenifbackgroundprocessis
suspended
◦Servicehasnouserinterface,smallmemoryuse

Context Switch
WhenCPUswitchestoanotherprocess,the
systemmustsavethestateoftheoldprocess
andloadthesavedstateforthenewprocessvia
acontextswitch
ContextofaprocessrepresentedinthePCB
Context-switchtimeisoverhead;thesystem
doesnousefulworkwhileswitching
◦ThemorecomplextheOSandthePCBthelonger
thecontextswitch
Timedependentonhardwaresupport
◦Somehardwareprovidesmultiplesetsofregisters
perCPUmultiplecontextsloadedatonce

Theprocessofsavingthe
statusofan interrupted
processandloadingthestatus
ofthescheduledprocessis
known as
ContextSwitching.
Whenarunningprocessis
interruptedandtheOS
assignsanotherprocessand
transferscontroltoit,itis
known as
ProcessSwitching.

Contents
Process Concept
Process Scheduling
Operations on Processes
InterprocessCommunication
Examples of IPC Systems
Communication in Client-Server
Systems

Operations on Processes
System must provide mechanisms for:
process creation,
process termination,
and so on as detailed next

Process Creation
Parentprocesscreatechildrenprocesses,
which,inturncreateotherprocesses,forminga
treeofprocesses
Generally,processidentifiedandmanagedviaa
processidentifier(pid)
Resourcesharingoptions
Parentandchildrenshareallresources
Childrensharesubsetofparent’sresources
Parentandchildsharenoresources
Executionoptions
Parentandchildrenexecuteconcurrently
Parentwaitsuntilchildrenterminate

A Tree of Processes on a typical
Solaris

A Tree of Processes in Linuxinit
pid = 1
sshd
pid = 3028
login
pid = 8415
kthreadd
pid = 2
sshd
pid = 3610
pdflush
pid = 200
khelper
pid = 6
tcsch
pid = 4005
emacs
pid = 9204
bash
pid = 8416
ps
pid = 9298

Process Creation (Cont.)
Address space
◦Child duplicate of parent
◦Child has a program loaded into it
UNIX examples
◦fork()system call creates new process
◦exec()system call used after a fork()to replace the
process’memory space with a new program

Process Termination
Processexecuteslaststatementandthenasksthe
operatingsystemtodeleteitusingtheexit()
systemcall.
◦Returnsstatusdatafromchildtoparent(viawait())
◦Process’resourcesaredeallocatedbyoperatingsystem
Parentmayterminatetheexecutionofchildren
processesusingtheabort()systemcall.Some
reasonsfordoingso:
◦Childhasexceededallocatedresources
◦Taskassignedtochildisnolongerrequired
◦Theparentisexitingandtheoperatingsystemsdoesnot
allowachildtocontinueifitsparentterminates

Process Termination cont..
Someoperatingsystemsdonotallowchildtoexistsifitsparent
hasterminated.Ifaprocessterminates,thenallitschildrenmust
alsobeterminated.
◦cascadingtermination.Allchildren,grandchildren,etc.are
terminated.
◦Theterminationisinitiatedbytheoperatingsystem.
Theparentprocessmaywaitforterminationofachildprocess
byusingthewait()systemcall.Thecallreturnsstatus
informationandthepidoftheterminatedprocess
pid=wait(&status);
Ifnoparentwaiting(didnotinvokewait())processisazombie
Ifparentterminatedwithoutinvokingwait,processisan
orphan

AZombieprocessordefunctprocessisaprocess
thathascompletedexecution(viaexitsystemcall)
butstillhasanentryintheprocesstable.Itisa
processinthe“Terminatedstate”.
Thisoccursforchildprocesses,wheretheprocess
tableentryisstillneededtoallowtheparentprocess
toreaditschild’sexitstatus:oncetheexitstatusis
readviawaitsystemcallbytheparentprocess.
AnOrphanprocessisaprocessthatisstill
executing,butwhoseparenthasdied.Whenthe
parentdies,theorphanedchildprocessisadopted
byinitorsystemd(processID)

MultiprocessArchitecture –Chrome Browser
Manywebbrowsersrunassingleprocess(some
stilldo)
◦Ifonewebsitecausestrouble,entirebrowsercan
hangorcrash
GoogleChromeBrowserismultiprocesswith3
differenttypesofprocesses:
◦Browserprocessmanagesuserinterface,diskand
networkI/O
◦Rendererprocessrenderswebpages,dealswith
HTML,Javascript.Anewrenderercreatedforeach
websiteopened
RunsinsandboxrestrictingdiskandnetworkI/O,
minimizingeffectofsecurityexploits
◦Plug-inprocessforeachtypeofplug-in

InterprocessCommunication
Processeswithinasystemmaybeindependentor
cooperating
Cooperatingprocesscanaffectorbeaffectedby
otherprocesses,includingsharingdata
Reasonsforcooperatingprocesses:
◦Informationsharing
◦Computationspeedup
◦Modularity
◦Convenience
Cooperatingprocessesneedinterprocess
communication(IPC)
TwomodelsofIPC
◦Sharedmemory
◦Messagepassing

Cooperating Processes
Independentprocesscannotaffectorbe
affectedbytheexecutionofanother
process
Cooperatingprocesscanaffectorbe
affectedbytheexecutionofanother
process
Advantagesofprocesscooperation
◦Informationsharing
◦Computationspeed-up
◦Modularity
◦Convenience

Producer-Consumer Problem
Paradigmforcooperatingprocesses,
producerprocessproducesinformation
thatisconsumedbyaconsumer
process
◦unbounded-bufferplacesnopracticallimit
onthesizeofthebuffer
◦bounded-bufferassumesthatthereisa
fixedbuffersize

Bounded-Buffer –Shared-Memory Solution
Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;

Bounded-Buffer
item next_produced;
while (true) {
/* produce an item
in next produced */
while (((in + 1) %
BUFFER_SIZE) == out);
/* do nothing */
buffer[in] =
next_produced;
in = (in + 1) %
BUFFER_SIZE;
}
item next_consumed;
while (true) {
while (in == out);
/* do nothing */
next_consumed=
buffer[out];
out = (out + 1) %
BUFFER_SIZE;
/* consume the item in
next consumed */
}
Producer Consumer
This scheme allows at most BUFFER_SIZE -1 items
in the buffer at the same time.

Communications Models
(a) Message passing. (b) shared memory.

InterprocessCommunication –Shared
Memory
Anareaofmemorysharedamongtheprocesses
thatwishtocommunicate
Thecommunicationisunderthecontrolofthe
usersprocessesnottheoperatingsystem.
Majorissuesistoprovidemechanismthatwill
allowtheuserprocessestosynchronizetheir
actionswhentheyaccesssharedmemory.
Synchronizationisdiscussedingreatdetailsin
Chapter5.

Two kinds of buffers
Unbounded buffer Bounded buffer
FULL
WAIT

InterprocessCommunication –Message
Passing
Mechanism for processes to communicate and to
synchronize their actions
Message system –processes communicate with
each other without resorting to shared variables
IPC facility provides two operations:
◦send(message)
◦receive(message)
Themessagesize is either fixed or variable

Message Passing (Cont.)
IfprocessesPandQwishto
communicate,theyneedto:
◦Establishacommunicationlinkbetween
them
◦Exchangemessagesviasend/receive
Implementationissues:
 Naming
 Synchronization
 Buffering

Message Passing (Cont.)
Implementation of communication link
◦Physical:
Shared memory
Hardware bus
Network
◦Logical:
Directorindirect
Synchronousorasynchronous
Automaticorexplicit buffering

Direct Communication
Processesmustnameeachother
explicitly:
◦send(P,message)–sendamessageto
processP
◦receive(Q,message)–receiveamessage
fromprocessQ
Propertiesofcommunicationlink
◦Linksareestablishedautomatically
◦Alinkisassociatedwithexactlyonepairof
communicatingprocesses
◦Betweeneachpairthereexistsexactlyonelink
◦Thelinkmaybeunidirectional,butisusuallybi-
directional

Indirect Communication
Messagesaredirectedandreceivedfrommailboxes8(also
referredtoasports)
◦Eachmailboxhasauniqueid
◦Processescancommunicateonlyiftheyshareamailbox
Propertiesofcommunicationlink
◦Linkestablishedonlyifprocessesshareacommonmailbox
◦Alinkmaybeassociatedwithmanyprocesses
◦Eachpairofprocessesmayshareseveralcommunicationlinks
◦Linkmaybeunidirectionalorbi-directional

Indirect Communication cont…
Primitivesaredefinedas:
send(A,message)–sendamessageto
mailboxA
receive(A,message)–receivea
messagefrommailboxA
Operations
◦createanewmailbox(port)
◦sendandreceivemessagesthrough
mailbox
◦destroyamailbox

Synchronization
Messagepassingmaybeeitherblockingornon-
blocking
Blockingisconsideredsynchronous
◦Blockingsend--thesenderisblockeduntilthe
messageisreceived
◦Blockingreceive--thereceiverisblockeduntila
messageisavailable
Non-blockingisconsideredasynchronous
◦Non-blockingsend--thesendersendsthemessage
andcontinue
◦Non-blockingreceive--thereceiverreceives:
Avalidmessage,or
Nullmessage
Differentcombinationspossible
Ifbothsendandreceiveareblocking,wehavea
rendezvous

Buffering
Queueofmessagesattachedtothelink.
implementedinoneofthreeways
1.Zerocapacity–nomessagesarequeued
on a link.
Sendermustwaitforreceiver(rendezvous)
2.Boundedcapacity–finitelengthofn
messages
Sendermustwaitiflinkfull
3.Unboundedcapacity–infinitelength
Senderneverwaits

Examples of IPC Systems –Windows
Message-passingcentricviaadvanced
localprocedurecall(LPC)facility
◦Onlyworksbetweenprocessesonthesame
system
◦Usesports(likemailboxes)toestablishand
maintaincommunicationchannels
◦Communicationworksasfollows:
Theclientopensahandletothesubsystem’s
connectionportobject.
Theclientsendsaconnectionrequest.
Theservercreatestwoprivatecommunication
portsandreturnsthehandletooneofthemtothe
client.
Theclientandserverusethecorrespondingport
handletosendmessagesorcallbacksandtolisten
forreplies.

Local Procedure Calls in Windows
Tags