You are on page 1of 33

Chapter 3

Processes
Prepared by
Dr Samia Loucif
1
Chapter 3: Processes

 Process Concept
 Process Scheduling
 Operations on Processes
 Inter-process Communication (IPC)

2
Objectives
 To introduce the notion of a process -- a program in execution, which
forms the basis of all computation
 To describe the various features of processes, including scheduling,
creation and termination, and communication
 To explore inter-process communication using shared memory and
message passing

3
Process Concept
 What is a Process? – a program in execution;
 It is Program code and execution state (see later)
 Program is passive entity stored on disk (executable file), process is active
 Execution of program starts via GUI mouse clicks, command line entry
of its name, etc

 Job and process used interchangeably

4
Process Concept (Cont.)
 To execute, processes need resources like
 Memory space (loaded into RAM) if too long some parts in SWAP or virtual
memory (part of the disk –extension of RAM)
 CPU
 Other resources like Input and/or Output devices

 It is the OS which assigns resources to processes

5
Process in Memory –Address Space

 The program code a.k.a. text section

 Data section containing global


variables

 Heap containing memory dynamically


allocated during run time

 Stack containing temporary data


 Function parameters, return
addresses, local variables

6
Process State
 As a process executes, it changes state:
 new: The process is being created
 ready: The process is waiting to be assigned to it CPU to start the
execution
 running: Instructions are being executed
 waiting: The process is waiting for some event to occur
 terminated: The process has finished execution

7
Diagram of Process State
 With One CPU, Only 1 process is running
 Many processes can be ready or waiting

Process States

8
Process Control Block (PCB)
Question: how OS keeps track of processes?
Answer: Using PCB (information associated with each process)
What are the elements of a PCB?
 Process state – running, waiting, etc.
 Process ID -unique identifier given to processes
 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 9
10
CPU Switch From Process to Process

11
Process Creation
 Parent process create children processes, which, in turn create other processes,
forming a tree of processes
 Generally, process identified and managed via a process identifier (pid)
 In Linux, Process creation is by means of the kernel system call, fork()

12
A Tree of Processes in Linux
Process Termination
 Once a process terminates, all resources assigned to it will be deallocated
by the OS
 A process terminates either
 Normal exit (voluntary), execute last instruction of the code then asks
the operating system to delete it using the exit() system call. or
interactive mode close the application
 Error exit like instruction division by 0, accessing file not existing, etc.
 A parent process can terminate a child process (has exceeded allocated
resources, Task assigned to child is no longer required)
 Some situation when a parent process is terminated, all child and
grandchild processes are terminated as well (cascading termination)

.
13
Process Scheduling
 Maximize CPU use, quickly switch processes onto CPU for time sharing
 A process created and admitted to system goes to ready queue
 CPU scheduler selects among available processes (in ready queue) for next
execution on CPU
 The system maintains queues of processes such as
 Ready queue all process waiting for cpu– set of all processes residing in main
memory, ready and waiting to execute
 Device queues all device have queues – set of processes waiting for an I/O device

 Processes during their lifetime in the system migrate among the various queues

14
Ready Queue And Various I/O Device Queues

15
Representation of Process Scheduling
 Queueing diagram represents queues, resources, flows

16
Question-

17
Question

18
Inter-Process Communication (IPC)
 Processes within a system may be independent or cooperating
 Cooperating process can affect or be affected by other processes, including
sharing data
 Reasons for cooperating processes:
 Information sharing
 Computation speedup
 Modularity: dividing the program functions into separate processes 
 Cooperating processes need inter-process communication (IPC)
 Two models of IPC
 Shared memory
 Message passing(passing messages)

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

20
Inter-process Communication –
Shared Memory

 An area of memory shared among the processes that wish to communicate

 The communication is under the control of the users processes not the
operating system.

 Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.

 Synchronization is discussed in great details in Chapter 5.

21
Inter-process Communication –
Message Passing
 Mechanism for processes to communicate
 Message system – processes communicate with each other without
resorting to shared variables

 IPC facility provides two operations:


 send(message)
 receive(message)

 The message size is either fixed or variable

22
Question
 Processes running on different computers communicate via
A. Shared memory
B. Passing message

23
Message Passing (Cont.)
 If processes P and Q wish to communicate, they need to:
 Establish a communication link between them
 Exchange messages via send/receive
 Implementation issues:
 How are links established?
 Can a link be associated with more than two processes?
 How many links can there be between every pair of communicating processes?
 What is the capacity of a link?
 Is the size of a message that the link can accommodate fixed or variable?
 Is a link unidirectional or bi-directional?

24
Message Passing (Cont.)
 Implementation of communication link
 Physical:
 Shared memory
 Hardware bus
 Network
 Logical:
 Direct or indirect
 Synchronous or asynchronous
 Automatic or explicit buffering

25
Direct Communication
 Processes must name each other explicitly:

 send (P, message) – send a message to process P

 receive(Q, message) – receive a message from process Q

 Properties of communication link

 Links are established automatically

 A link is associated with exactly one pair of communicating processes

 Between each pair there exists exactly one link

26
Indirect Communication
 Messages are directed and received from mailboxes (also referred to as ports)
 Each mailbox(space in memory) has a unique id
 Processes can communicate only if they share a mailbox
(the os will give the acces for the mail box since it’s the space in the memory)
 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, message) – receive a message from mailbox A
 Properties of communication link
 Link established only if processes share a common mailbox
 A link may be associated with many processes
 Each pair of processes may share several communication links 27

 Link may be unidirectional or bi-directional


Synchronization
 Message passing may be either blocking or non-blocking, depending on needs
 Blocking is considered synchronous
 Blocking send -- the sender is blocked until the message is received
When it is executing the send it will stop at this point (sender is blocked until the sender is
received)
 Blocking receive -- the receiver is blocked until a message is available
 Synchronous allows both send and receive are blocking known as rendezvous
 Non-blocking is considered asynchronous
 Non-blocking send -- the sender sends the message and continue
 Non-blocking receive -- the receiver receives:
 A valid message, or
 Null message
 Synchronous communication guarantees the . Asynchronous communication increases
28

performance but does not guarantee message received


Buffering
 Queue of messages attached to the link.

 implemented in one of three ways

1. Zero capacity (no buffer between the sender and the receiver ,kind of communication and works only
with :rendezvous, ready to receive )

(message system with no buffering)– no messages are queued on a link.


Sender must wait for receiver (rendezvous)

2. Bounded(limit) capacity (explicit buffering) process will continue , the number of messages reach n there will be no
space then blocked ,after n when the link is full the process sender needs to block

finite length of n messages Sender must wait if link full. But its advantage is no waste of memory space

3. Unbounded capacity(no limit,open) (automatic buffering) the process will send any time and will stay continues with
no block

Disdavantges :not all space will be fully used

– infinite length  Sender never blocks


29
But its disadvantage is if more memory space reserved than needed results in waste of memory space
Size of A Message That The Link Can Accommodate Fixed
or Variable?
 Fixed size messages:
 makes buffering simpler
 sender knows exact size of messages to send
 But if sender has large messages, must be broken which is an extra
work at sender side
 Variable size messages:
 More flexibility for the sender
 But how does it know the message will fit in the buffer or in case it is
larger than the buffer size

30
31

32
 Textbook:
 Silberschatz, Operating System Concepts
 References:
 W. Stallings, Operating Systems: Internals And Design Principles, 9/E
 Andrew Tanenbaum & Herbert Bos,  Modern Operating Systems, 4/E
 https://www.cs.unc.edu/~porter/courses/cse306/s13/slides/

33

You might also like