Chapter3 Processes
Chapter3 Processes
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives
Operating System Concepts – 10th Edition 3.2 Silberschatz, Galvin and Gagne ©2018
Outline
Process Concept-Basics
Layout of process in Memory
Process Control Block ( PCB)
Process States
Process Scheduling
Interprocess Communication
IPC in Shared-Memory Systems
• Producer-consumer problem
• Bounded Buffer Solution
IPC in Message-Passing Systems ( Direct & Indirect Comm)
Pipes (Ordinary and Named ) for parent-child communication
Communication in Client-Server Systems
• Sockets
• RPC
Operating System Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne ©2018
Process Concept
Program- Set of instructions
Process- Program in execution or certain activity.
E.g. browsers, text editors, emails, apps
Types of processes in OS
• Operating system processes
• User processes
Can relate to OS services and program control system calls?
Operations on Processes:
Process creation
Process termination
Process scheduling
IPC
Operating System Concepts – 10th Edition 3.4 Silberschatz, Galvin and Gagne ©2018
Representation
Operating System Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne ©2018
Process Concept (Cont.)
Program is passive entity stored on disk (executable file);
process is active
• Program becomes process when an executable file is
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
• For example, MS word has different files open.
Operating System Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne ©2018
Difference Between Program and
Process
Feature Program Process
Dynamic instance of a
Definition Static set of instructions program in execution
State Passive Active
Operating System Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne ©2018
Process in Memory
Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018
Memory Layout of a C Program
Operating System Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne ©2018
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 via a context switch
Context of a process represented in the PCB
Context-switch time is pure overhead; the system does no useful
work while switching
• The more complex the OS and the PCB the longer the context
switch
Time dependent on hardware support
• Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
Operating System Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne ©2018
Context Switching
A context switch occurs when the CPU switches from one
process to another.
Operating System Concepts – 10th Edition 3.12 Silberschatz, Galvin and Gagne ©2018
Think!
What extra information would you store in the PCB if you were
designing your own OS?
Why do we need a PCB? Can an OS function without a PCB?
What happens if the PCB of a process gets corrupted?
What would happen if two processes had the same PCB?
How does an OS keep track of millions of PCBs?
Operating System Concepts – 10th Edition 3.13 Silberschatz, Galvin and Gagne ©2018
Process States
Operating System Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne ©2018
Process States
As a process executes, it changes state
Operating System Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne ©2018
Diagram of Process State
5 States Model
Operating System Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne ©2018
Scheduling Queues
OS Maintains scheduling queues of processes
Operating System Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne ©2018
Process Scheduling
Types of Scheduling
• Preemptive
• Non-Preemptive
Process scheduler selects among available processes
for next execution on CPU core
Goal -- Maximize CPU usage, quickly switch processes
onto CPU core. The main types of schedulers are:
Operating System Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne ©2018
Representation of Schedulers
Operating System Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne ©2018
Difference Between Schedulers
Operating System Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne ©2018
Process Creation and Termination
Operating System Concepts – 10th Edition 3.21 Silberschatz, Galvin and Gagne ©2018
Parent-Child Processes
Process- Running instance or active state
From this process another processes may be generated. i.e. Parent-
Child relationship between two processes, child process being the
subprocess/subtask.
Fork( ) system call is used to create this relationship during run-time.
Why to use?
Operating System Concepts – 10th Edition 3.22 Silberschatz, Galvin and Gagne ©2018
Process Termination
Causes of Process Termination
🔹 Normal Termination exit()
•Killed by user (e.g., using kill command in Linux or Task Manager in Windows).
•Parent process terminates (child processes may be killed).
Operating System Concepts – 10th Edition 3.23 Silberschatz, Galvin and Gagne ©2018
Inter-Process Communication(IPC)
Operating System Concepts – 10th Edition 3.24 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication
Operating System Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne ©2018
IPC Communications Models
(a) Shared memory. (b) Message passing.
Operating System Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne ©2018
IPC – Shared Memory
An area of memory is shared among the processes that wish to
communicate
The communication is controlled by the users’ processes not the
operating system.
One major issue is to provide a mechanism that will allow the
user processes to synchronize their actions when they access
shared memory.
Advantages
• High speed data transfer
• High volume of information
• Communication on same machine
• Real time data transfers
Operating System Concepts – 10th Edition 3.27 Silberschatz, Galvin and Gagne ©2018
Producer-Consumer Problem
( Shared Memory)
Paradigm for cooperating processes:
• Producer process produces information that is consumed
by a Consumer process
• Problem occurs when both are running concurrently.
Two variations:
• unbounded-buffer places no practical limit on the size of
the buffer or data rate
Producer never waits
Consumer waits if there is no buffer to consume
• bounded-buffer assumes that there is a fixed buffer size-
known data to transfer
Producer must wait if all buffers are full
Consumer waits if there is no buffer to consume
Operating System Concepts – 10th Edition 3.28 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer – Shared-Memory Solution
// Shared data
Operating System Concepts – 10th Edition 3.29 Silberschatz, Galvin and Gagne ©2018
Producer, Consumer
Shared Memory
// Producer process //Consumer process
Operating System Concepts – 10th Edition 3.30 Silberschatz, Galvin and Gagne ©2018
What about Filling all the Buffers?
Suppose that we wanted to provide another solution to the
consumer-producer problem that fills all the buffers.
We can do so by having an integer counter that keeps track
of the number of full buffers.
Initially, counter is set to 0.
The integer counter is incremented by the producer after it
produces a new buffer.
The integer counter is and is decremented by the consumer
after it consumes a buffer.
Operating System Concepts – 10th Edition 3.31 Silberschatz, Galvin and Gagne ©2018
Producer
while (true) {
/* produce an item in next produced */
Operating System Concepts – 10th Edition 3.32 Silberschatz, Galvin and Gagne ©2018
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Operating System Concepts – 10th Edition 3.33 Silberschatz, Galvin and Gagne ©2018
IPC – Message Passing
Operating System Concepts – 10th Edition 3.34 Silberschatz, Galvin and Gagne ©2018
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?
Dynamically, each process must explicitly name
the other process it wants to communicate.
• How many links can there be between every pair of
communicating processes?
Only one link.
• What is the capacity of a link?
Fixed / variable sized messages
• Is the size of a message that the link can accommodate
fixed or variable?
Depends on system.
• Is a link unidirectional or bi-directional?
Depends on system design
Operating System Concepts – 10th Edition 3.35 Silberschatz, Galvin and Gagne ©2018
Implementation of Communication Link
Physical
• Hardware bus
• Network
Logical
• Direct or indirect
• Automatic or explicit buffering
Operating System Concepts – 10th Edition 3.36 Silberschatz, Galvin and Gagne ©2018
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
• The link may be unidirectional, but is usually bi-directional
Operating System Concepts – 10th Edition 3.37 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
Messages are directed and received from mailboxes (also referred
to as ports)
• Each mailbox has a unique id
• Processes can communicate only if they share a mailbox
Properties of communication link
• Link established only if processes share a common mailbox
• A link may be associated with many processes
• Link may be unidirectional or bi-directional
Operating System Concepts – 10th Edition 3.38 Silberschatz, Galvin and Gagne ©2018
Indirect Communication (Cont.)
Operations
• Create a new mailbox (port)
• Send and receive messages through mailbox
• Delete a mailbox
Primitives are defined as:
• send(A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
Operating System Concepts – 10th Edition 3.39 Silberschatz, Galvin and Gagne ©2018
Pipes
Acts as a conduit allowing two processes to communicate
Issues:
• Is communication unidirectional or bidirectional?
Always Uniderectional ,
• Must there exist a relationship (i.e., parent-child) between the
communicating processes?
Yes, it is a must for ordinary pipes.
• Can the pipes be used over a network? No.
Ordinary pipes – cannot be accessed from outside the process that
created it. Typically, a parent process creates a pipe and uses it to
communicate with a child process that it created.
Named pipes – can be accessed without a parent-child relationship.
Operating System Concepts – 10th Edition 3.40 Silberschatz, Galvin and Gagne ©2018
Ordinary Pipes
Ordinary Pipes allow communication in standard producer-consumer
style
Producer writes to one end (the write-end of the pipe) [ fd[1] = write]
Consumer reads from the other end (the read-end of the pipe)
[ fd[0]=read]
Ordinary pipes are therefore unidirectional
Require parent-child relationship between communicating processes
Operating System Concepts – 10th Edition 3.41 Silberschatz, Galvin and Gagne ©2018
Named Pipes
Operating System Concepts – 10th Edition 3.42 Silberschatz, Galvin and Gagne ©2018
Communications in Client-Server Systems
Sockets
Remote Procedure Calls
Operating System Concepts – 10th Edition 3.43 Silberschatz, Galvin and Gagne ©2018
Sockets
A socket is defined as an endpoint for communication
Concatenation of IP address and port – a number included at start of
message packet to differentiate network services on a host
The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
Communication consists between a pair of sockets
All ports below 1024 are well known, used for standard services
Special IP address 127.0.0.1 (loopback) to refer to system on which
process is running
Operating System Concepts – 10th Edition 3.44 Silberschatz, Galvin and Gagne ©2018
Socket Communication
Operating System Concepts – 10th Edition 3.45 Silberschatz, Galvin and Gagne ©2018
Remote Procedure Calls
Remote procedure call (RPC) abstracts procedure calls between
processes on networked systems
• Again uses ports for service differentiation
Stubs – client-side proxy for the actual procedure on the server
The client-side stub locates the server and marshalls the parameters
The server-side stub receives this message, unpacks the marshalled
parameters, and performs the procedure on the server
On Windows, stub code compile from specification written in
Microsoft Interface Definition Language (MIDL)
Operating System Concepts – 10th Edition 3.46 Silberschatz, Galvin and Gagne ©2018
Execution of RPC
Operating System Concepts – 10th Edition 3.47 Silberschatz, Galvin and Gagne ©2018
End of Chapter 3
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018