Chapter 3: Processes
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives
Understand the basics of a process inside operating system.
Identify the separate components of a process and illustrate how they
are represented and scheduled by an operating system.
Understand working of PCB and different process states.
Describe and contrast interprocess communication using shared
memory and message passing.
Describe client-server communication using sockets and remote
procedure calls.
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
Stored on secondary Resides in main memory
Location storage (disk) (RAM)
Allocated CPU time,
memory, and other
Resources No dedicated resources resources
Python script, C++
Examples program Web browser window
Operating System Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne ©2018
Process in Memory
Process – a program in execution; process
execution must progress in sequential fashion.
Multiple parts in main memory.
Stack containing temporary data
• Function parameters, return addresses, local
variables
Heap containing memory dynamically allocated
during run time
Data section containing global variables
The program code, also called text section
Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018
Memory Layout of a C Program
Stack: Functions/parameters/local variables/return value
Heap: Dynamic Allocation
Data: Global Variables ( Initialized/uninitialized)
Text/Code: Program Statements
Operating System Concepts – 10th Edition 3.9 Silberschatz, Galvin and Gagne ©2018
Process Control Block (PCB)
Information associated with each process(also called task control
block)
Process ID – Unique Identifier
Process state – running, waiting, etc.
CPU scheduling information- priorities, scheduling
queue pointers
Program counter – location of instruction to next
execute
Memory Pointers- Helps to link process sections like
Heap, stack, etc..
Context Data – To save and restore process state
during context switching
I/O status information – I/O devices allocated to
process, list of open files
Accounting information – CPU used, clock time
elapsed since start, time limits
Miscellaneous- Other information as well
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
• New: The process is being created
• Ready: The process is waiting to be assigned to a processor
• Running: Instructions are being executed
• Waiting: The process is waiting for some event to occur
• Terminated: The process has finished execution
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
• Job queue – set of all processes in the system
• Ready queue – set of all processes residing in main memory,
ready and waiting to execute
• Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
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:
Long Term or Job Scheduler
Short-Term or CPU Scheduler
Medium-Term Scheduler
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
Long Term Scheduler Short Term Scheduler Medium Term Scheduler
It is a job scheduler It is a CPU scheduler It is a process-swapping
scheduler.
The slowest scheduler. Speed is the fastest Speed lies in between both
among all of them. short and long-term
schedulers.
It controls the degree of It gives less control over It reduces the degree of
multiprogramming how much multiprogramming.
multiprogramming is
done.
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()
•The process executes its last statement and exits.
•The OS de-allocated resources (CPU, memory).
🔹 Abnormal Termination abort()
•Error in process execution (e.g., division by zero).
•Memory limit exceeded (out-of-bounds memory access).
•Hardware failure (power outage, disk crash).
🔹 External Termination: kill( )
•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
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
• Convenience
Cooperating processes need interprocess communication (IPC)
Two models of IPC
• Shared memory
• Message passing
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
Application Areas. Databases, AI, OS, Trading, video processing
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
int SIZE = 5; // total elements
int in= 1; // producer
int out = 1; //consumer
Int buffer[SIZE];
//both consumer and producer at 1 represent that
buffer is empty
Operating System Concepts – 10th Edition 3.29 Silberschatz, Galvin and Gagne ©2018
Producer, Consumer
Shared Memory
// Producer process //Consumer process
while (true) { while (true) {
/* produce an item in buffer */ while (in == out)
while (((in + 1) % SIZE) == ; /* do nothing */
out) next_consumed =
buffer[out];
; /* do nothing */
out = (out + 1) % SIZE;
buffer[in] = next_produced;
in = (in + 1) % SIZE;
/* consume the item in
} next consumed */
}
//if the buffer is full, it
will not allow more items. // if buffer is empty, it will not
allow more read operations
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 */
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
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
Processes communicate with each other without having any
shared variables
IPC facility provides two operations:
• send(message)
• receive(message)
The message size is either fixed or variable
Advantages
• No shared memory issues
• Works across different machines
Application Areas.
• Car riding apps, cloud-based apps, banking systems
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
Named Pipes are more powerful than ordinary pipes
Communication is bidirectional
No parent-child relationship is necessary between the communicating
processes
Several processes can use the named pipe for communication
Provided on both UNIX and Windows systems
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 [Link]:1625 refers to port 1625 on host [Link]
Communication consists between a pair of sockets
All ports below 1024 are well known, used for standard services
Special IP address [Link] (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