Processes
What is a process?
Structure of a process
Process state
Schedulers and Scheduling Queues
Context switching
Process creation and termination
Inter Process Communication
Dr. Manmath Narayan Sahoo
Department of Computer Science & Engineering
National Institute of Technology Rourkela
Odisha – 769008
Disclaimer – This slide is prepared taking content from various books and online resources. The presenter
doesn’t own the content. The inherited content is used for educational purpose only.
Process
• Process is a program that has initiated its execution.
• Program is passive entity stored on disk (executable file),
process is active
– Program becomes process when executable file loaded into
memory
• Execution of program started via GUI mouse clicks, command
line entry of its name, etc
• One program can be several processes
– Consider multiple users executing the same program
Dr. Manmath N. Sahoo (CS) 2
Process structure
A process includes:
• Current activity including program counter, processor registers ⇒system
• text/code/program section containing a copy of the machine code instructions
data section storing process context and info about
resources allocated
• Stack containing temporary data
– Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
user and system data sections obviously may be different from one run of a process to another
Dr. Manmath N. Sahoo (CS) 3
Process in memory
Dr. Manmath N. Sahoo (CS) 4
Process State
• As a process executes, it changes state
– 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
5
Process Control Block (PCB)
• PCB - data structure that contains Information associated with each
process:
– Process ID
– Process State
– Program Counter
– Register Contents
– Starting address of the process
– Size of the process
– Pointer to Child Process
– Pointer to Next PCB (Sibling Pointer)
– Resource Pointer
Dr. Manmath N. Sahoo (CS) 6
Process Scheduling Queues
• Job queue – Set of all processes just created (new state
processes).
• Ready queue – set of all processes that are in main memory,
ready to execute (ready state processes).
• Device queues – set of processes waiting to use an I/O device
(waiting state processes).
Dr. Manmath N. Sahoo (CS) 7
Ready Queue And Various I/O Device Queues
Dr. Manmath N. Sahoo (CS) 8
Queueing Diagram
9
Schedulers
• Long-term scheduler (LTS) (or job scheduler) → selects
• Short-term scheduler (STS) (or CPU scheduler) → selects
which processes should be brought into the ready queue.
which process should be executed next on CPU.
10
Schedulers
• Short-term scheduler is invoked very frequently (milliseconds) ⇒ (must be fast).
• Long-term scheduler is invoked very infrequently (seconds, minutes) ⇒ (may be
slow).
• The long-term scheduler controls the degree of multiprogramming
• Long-term scheduler strives for good process mix of I/O-bound and CPU-bound
processes
– I/O-bound process →spends more time doing I/O than computations
– CPU-bound process → spends more time doing computations
– Only I/O bound processes → low CPU utilization
– Only CPU-bound processes → low device utilization
Dr. Manmath N. Sahoo (CS) 11
Schedulers
• Some OS have a light LTS; and a Medium Term Scheduler (MTS).
• If degree of multiprogramming becomes high then total context switch
overhead will be more.
• MTS can temporarily swap some processes out to disk and bring them
back later.
12
Context Switch
• When CPU switches to another process, the system must save
the state of the old process and load the saved state for the new
process.
• Context of a process represented in the PCB
• Context-switch time is overhead; the system does no useful
work while switching.
Dr. Manmath N. Sahoo (CS) 13
CPU switched between processes
14
Process Creation
• Parent process create children processes, which, in turn create other
• There must be a first process (init process) → this is a system
processes, forming a tree of processes
process created during booting
• Resource sharing options
– Parent and children share all resources
– Children share subset of parent’s resources
– Parent and child share no resources
• Execution options
– Parent and children execute concurrently
– Parent waits until children terminate
Dr. Manmath N. Sahoo (CS) 15
A Tree of Processes in Linux
Dr. Manmath N. Sahoo (CS) 16
Process Termination
• Process executes last statement and asks the operating system
to delete it using exit().
– Return data from child to parent.
– Process’ resources are deallocated by OS.
• Parent may terminate execution of children processes (abort).
Reasons for this could be:
– Task assigned to child is no longer required.
– Parent is exiting and O/S does not allow children to continue if
their parent terminates – cascaded termination.
Dr. Manmath N. Sahoo (CS) 17
Cooperating Processes
• Processes within a system may be independent or cooperating
• Cooperating process can legally affect or be affected by the execution of another process.
Reasons for cooperating processes:
• Processes may need to share data
– More than one process reading/writing the same data (a shared file, a database record,…)
– Output of one process being used by another
• Ordering executions of multiple processes may be needed to ensure correctness
– Process X should not do something before process Y does something etc.
– Need mechanisms to pass control signals between processes
• Modularity → dividing system functions into separate processes that then communicate
• Computation speed-up → but only if >1 processor
Dr. Manmath N. Sahoo (CS) 18
Inter-Process Communication (IPC)
Cooperating processes need interprocess communication (IPC)
• Message passing
– P and Q exchange messages
• Shared memory
– P writes into a shared location, Q reads from it and vice-versa
Dr. Manmath N. Sahoo (CS) 19
Inter-Process Communication (IPC)
(a) Message Passing (b) Shared Memory
20
Message Passing System
W e b s e rv e r
S1 S2 S3 S4
o pe ra ti o n s :
H TTP S 1 : a cce pt co n n e cti o n
a pro ce s s S 2 : r e c e i ve ( r e q u e s t )
re qu e s t
S 3 : s e n d (re s po n s e )
a n o pe ra ti o n S 3 : di s co n n e ct
H TTP
C 1 : m a k e co n n e cti o n
re s po n s e
C 2 : s e n d (re qu e s t)
da ta fl o w C 3 : r e c e i ve ( r e s p o n s e )
C1 C2 C3 C4
C 4 : di s co n n e ct
W e b bro ws e r
Dr. Manmath N. Sahoo (CS) 21
Message Passing System
• Direct vs. Indirect
• Symmetric vs. Asymmetric
• Synchronous vs. Asynchronous
Dr. Manmath N. Sahoo (CS) 22
Message Passing System: Direct
Primitives:
• Connect (sender_addr, receiver_addr)
– for connection-oriented communication.
• Send ([receiver],message)
– e.g., Send(P, msg)
• Receive ([sender],buffer)
– e.g., Receive(Q,buf)
• Disconnect(connectionid)
– for connection-oriented communication.
Dr. Manmath N. Sahoo (CS) 23
Message Passing System: Direct
• Symmetric – Both the sender and receiver have to explicitly
name each other.
• Asymmetric – Only the sender has to name the receiver, but
the receiver doesn’t need to name the sender.
– Send(P, msg); → send message to P.
– Facilitates many-to-one communication.
– Receive(id, mbuf); → receive message from any process. “id”
will be set to the name of the process with which communication
has taken place.
Dr. Manmath N. Sahoo (CS) 24
Message Passing System: Indirect
• The messages are sent and received to/from mailboxes (also referred
to as ports).
– Each mailbox has a unique identifier.
– Processes can communicate only if they share a mailbox
• Operations
– create a new mailbox (port)
– send and receive messages through mailbox
– destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, mbuffer) – receive a message from mailbox A
Dr. Manmath N. Sahoo (CS) 25
Message Passing System: Indirect
• Mailbox sharing
– P1, P2, and P3 share mailbox A
– P1 sends; P2 and P3 receive
– Who gets the message?
• Solutions
– Allow a link to be associated with at most two processes
– Allow only one process at a time to execute a receive operation
– Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.
Dr. Manmath N. Sahoo (CS) 26
Message Passing System: Synchronous vs. Asynchronous
• Message passing may be either blocking or non-blocking
•
– Blocking send →the sender is blocked until the message is received
Blocking is considered synchronous
– Blocking receive →the receiver is blocked until a message is
available
•
– Non-blocking send →the sender sends the message and continue
Non-blocking is considered asynchronous
– Non-blocking receive →the receiver receives:
• A valid message, or
• Null message
• Different combinations possible
Dr. Manmath N. Sahoo (CS) 27
Synchronous send and receive
pr o c e s s 1 pr o c e s s 2
r unni ng o n ho s t 1 r unni ng o n ho s t 2
b lo c k in g rec eiv e s tarts
b lo c k in g s en d s tarts an o p eratio n
ex ec u tio n flo w
ac k n o w led g em en t o f d ata rec eiv ed s u s p en d ed p erio d
b lo c k in g s en d retu rn s p ro v id ed b y th e IP C fac ility b lo c k in g rec eiv e en d s
Sync hr o no us Se nd and R e c e i ve
Dr. Manmath N. Sahoo (CS) 28
Asynchronous send and synchronous receive
P ro cess 2
P ro cess 1
b lo c k in g r ec eiv e s tar ts
n o n b lo c k in g s en d
o p er atio n
ex ec u tio n f lo w
s u s p en d ed p er io d
b lo c k in g r ec eiv e r etu r n s
As ync hr o no us Se nd and
Sync hr o no us R e c e i ve
Dr. Manmath N. Sahoo (CS) 29
Synchronous send and Asynchronous Receive - A
P rocess 2
P rocess 1
b lo c k in g s e n d is s u e d
n o n b lo c k in g r e c e iv e is s u e d
tr a n s p a r e n t a c k n o w le d g e m e n t
p r o v id e d b y th e I P C f a c ility e x e c u tio n f lo w
s u s p e n d e d p e r io d
S yn c h r o n o u s S e n d an d
As yn c h r o n o u s R e c e i ve
S c e n a r io A
Dr. Manmath N. Sahoo (CS) 30
Synchronous send and Asynchronous Receive - B
P ro cess 2
P ro cess 1
n o n b lo c k in g r e c e iv e is s u e d
a n d r e tu r n e d im m e d ia te ly
b lo c k in g s e n d is s u e d
in d e f in ite
b lo c k in g e x e c u tio n f lo w
s u s p e n d e d p e r io d
P ro cess 2
P ro cess 1 S yn c h r o n o u s S e n d an d
As yn c h r o n o u s R e c e i ve
S c e n a r io B
Dr. Manmath N. Sahoo (CS) 31
Synchronous send and Asynchronous Receive - C
P rocess 2
P rocess 1
n o n b lo c k in g r e c e iv e is s u e d
a n d r e tu r n e d im m e d ia te ly
b lo c k in g s e n d is s u e d
p r o c e s s is n o tif ie d
tr a n s p a r e n t a c k n o w le d g e m e n t o f th e a r r iv a l o f
p r o v id e d b y th e I P C f a c ility d a ta
e x e c u tio n f lo w
s u s p e n d e d p e r io d
S yn c h r o n o u s S e n d an d
As yn c h r o n o u s R e c e i ve
S c e n a r io C
Dr. Manmath N. Sahoo (CS) 32
Asynchronous send and Asynchronous receive - A
Dr. Manmath N. Sahoo (CS) 33
Asynchronous send and Asynchronous receive - B
Dr. Manmath N. Sahoo (CS) 34
Event diagram
Pro ce s s B
Pro ce s s A
t im e
r eq u es t 1
r es p o n s e 1
r eq u es t 2
in ter p r o c es s c o m m u n ic atio n
ex ec u tio n f lo w
r es p o n s e2
p r o c es s b lo c k ed
E ve nt di ag r am fo r a pr o to c o l
Dr. Manmath N. Sahoo (CS) 35
Blocking, deadlock, and timeouts
• Blocking operations issued in the wrong sequence can cause deadlocks.
• Deadlocks should be avoided: timeout
P r o c es s 1 P r o c es s 2
r ec eiv e f r o m p r o c es s 2 is s u ed
p r o c es s 1 b lo c k ed p en d in g d ata
f r o m p r o c es s 2 .
r ec eiv ed f r o m p r o c es s 1 is s u ed
p r o c es s 2 b lo c k ed p en d in g d ata
f r o m p r o c es s 1 .
Dr. Manmath N. Sahoo (CS) 36
Using threads for asynchronous IPC
• Suppose IPC supports only blocking send and receive
• But user wants asynchronous operation. How???
– use a child process or thread pro ce s s
m ain th r ead
n ew th r ead is s u es a b lo c k in g I P C o p er atio n
th r ead is b lo c k ed
m ain th r ead c o n tin u es w ith
o th er p r o c es s in g
th r ead is u n b lo c k ed af ter th e o p er atio n is f u lf illed
Dr. Manmath N. Sahoo (CS) 37
Buffering during message passing
• Queue of messages attached to the link.
• Implemented in one of three ways
– Zero capacity
• No messages are queued on a link
• Sender must wait for receiver, after generating one item
– Bounded capacity
• Finite length of n messages
• Sender must wait if link full
– Unbounded capacity
• Infinite length
• Sender never waits
Dr. Manmath N. Sahoo (CS) 38
Process: Exception Conditions
• Process Termination
– Sender Terminates: If the receiver has initiated a blocking receive then the OS either
terminates the receiver or notifies it that Sender has terminated
– Receiver Terminates: If the sender has initiated a blocking send then the OS either
terminates the Sender or notifies it that Receiver has terminated
• Lost Messages
– The OS is responsible for detecting this event and for resending the message
– The Sender is responsible for detecting this event and for resending it
– The OS detects the event and notifies the Sender about it
• Scrambled Messages
– Due to noise in the channel
– Handled in the similar ways as lost messages
Dr. Manmath N. Sahoo (CS) 39