0% found this document useful (0 votes)
24 views55 pages

OS Unit-3 23-24

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views55 pages

OS Unit-3 23-24

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Operating Systems - Unit-III

Inter-Process Communication ; Deadlocks

February 24, 2024

1/55
Operating Systems - Unit-III February 24, 2024 1 / 55
Unit-III Syllabus

Interprocess Communication Mechanisms


IPC between processes on a single computer system
IPC between processes on different systems
Pipes
FIFOs
Message Queues
Shared Memory
Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection, and Recovery from Deadlock.

2/55
Operating Systems - Unit-III February 24, 2024 2 / 55
Interprocess Communication Basic Concepts

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

3/55
Operating Systems - Unit-III February 24, 2024 3 / 55
Interprocess Communication Basic Concepts

Communication Models

4/55
Operating Systems - Unit-III February 24, 2024 4 / 55
Interprocess Communication Basic Concepts

Cooperating Processes

Independent process cannot affect or be affected by the execution of


another process
Cooperating process can affect or be affected by the execution of
another process
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience

5/55
Operating Systems - Unit-III February 24, 2024 5 / 55
Interprocess Communication Producer-Consumer Problem

Producer-Consumer Problem

Paradigm for cooperating processes, producer process produces


information that is consumed by a consumer process
unbounded-buffer places no practical limit on the size of the buffer
bounded-buffer assumes that there is a fixed buffer size

6/55
Operating Systems - Unit-III February 24, 2024 6 / 55
Interprocess Communication Producer-Consumer Problem

Bounded Buffer-Shared Memory Solution

7/55
Operating Systems - Unit-III February 24, 2024 7 / 55
Interprocess Communication Producer-Consumer Problem

Bounded Buffer-Producer and Consumer

8/55
Operating Systems - Unit-III February 24, 2024 8 / 55
Interprocess Communication IPC-Shared Memory

Interprocess 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 further.

9/55
Operating Systems - Unit-III February 24, 2024 9 / 55
Interprocess Communication IPC-Message Passing

Interprocess Communication- 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)
The message size is either fixed or variable

10/55
Operating Systems - Unit-III February 24, 2024 10 / 55
Interprocess Communication IPC-Message Passing

Interprocess Communication- Message Passing

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?

11/55
Operating Systems - Unit-III February 24, 2024 11 / 55
Interprocess Communication IPC-Message Passing

Interprocess Communication- Message Passing

Implementation of communication link


Physical:
Shared memory
Hardware bus
Network
Logical:
Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering

12/55
Operating Systems - Unit-III February 24, 2024 12 / 55
Interprocess Communication IPC-Message Passing

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

13/55
Operating Systems - Unit-III February 24, 2024 13 / 55
Interprocess Communication IPC-Message Passing

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
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Operations
create a new mailbox (port)
send and receive messages through mailbox
destroy a mailbox

14/55
Operating Systems - Unit-III February 24, 2024 14 / 55
Interprocess Communication IPC-Message Passing

Indirect Communication

Primitives are defined as:


send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
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.

15/55
Operating Systems - Unit-III February 24, 2024 15 / 55
Interprocess Communication Pipes

Pipes in OS

Communication in client–server systems may use


sockets
remote procedure calls (RPCs)
pipes
A socket is defined as an endpoint for communication. A connection
between a pair of applications consists of a pair of sockets, one at
each end of the communication channel.
RPCs are another form of distributed communication. An RPC occurs
when a process (or thread) calls a procedure on a remote application.
Pipes provide a relatively simple ways for processes to communicate
with one another.
Ordinary pipes allow communication between parent and child
processes
named pipes permit unrelated processes to communicate.
16/55
Operating Systems - Unit-III February 24, 2024 16 / 55
Interprocess Communication Pipes

Pipes

Acts as a conduit allowing two processes to communicate


Issues:
Is communication unidirectional or bidirectional?
In the case of two-way communication, is it half or full-duplex?
Must there exist a relationship (i.e., parent-child) between the
communicating processes?
Can the pipes be used over a network?
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.

17/55
Operating Systems - Unit-III February 24, 2024 17 / 55
Interprocess Communication Pipes

Ordinary Pipes
Ordinary Pipes allow communication in standard producer-consumer
style
Producer writes to one end (the write-end of the pipe)
Consumer reads from the other end (the read-end of the pipe)
Ordinary pipes are therefore unidirectional
Require parent-child relationship between communicating processes

Windows calls these anonymous pipes


See Unix and Windows code samples in textbook 18/55
Operating Systems - Unit-III February 24, 2024 18 / 55
Interprocess Communication Pipes

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

19/55
Operating Systems - Unit-III February 24, 2024 19 / 55
Interprocess Communication Pipes

Pipes-Unix

On UNIX systems, ordinary pipes are constructed using the function


pipe(int fd[])
This function creates a pipe that is accessed through the int fd[] file
descriptors: fd[0] is the read-end of the pipe, and fd[1] is the
write-end.
UNIX treats a pipe as a special type of file. Thus, pipes can be
accessed using ordinary read() and write() system calls.
An ordinary pipe 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 creates via fork().
A child process inherits open files from its parent. Since a pipe is a
special type of file, the child inherits the pipe from its parent process.

20/55
Operating Systems - Unit-III February 24, 2024 20 / 55
System Model

System Model

System consists of resources


Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release

21/55
Operating Systems - Unit-III February 24, 2024 21 / 55
Deadlock Characterization

Deadlock Characterization

Deadlocks can arise if four conditions hold simultaneously.


Mutual exclusion: only one process at a time can use a resource
Hold and wait: a process holding at least one resource is waiting to
acquire additional resources held by other processes
No preemption: a resource can be released only voluntarily by the
process holding it, after that process has completed its task
Circular wait: there exists a set { P0, P1, . . . , Pn } of waiting processes
such that P0 is waiting for a resource that is held by P1, P1 is waiting
for a resource that is held by P2, . . . , Pn–1 is waiting for a resource
that is held by Pn, and Pn is waiting for a resource that is held by P0.

22/55
Operating Systems - Unit-III February 24, 2024 22 / 55
Deadlock Characterization

Deadlock with Mutexlocks

Deadlocks can occur via system calls, locking, etc.


See example box in text page 318 for mutex deadlock

23/55
Operating Systems - Unit-III February 24, 2024 23 / 55
Deadlock Characterization

Resource-Allocation Graph

A set of vertices V and a set of edges E.


V is partitioned into two types:
P = { P1, P2, ..., Pn }, the set consisting of all the processes in the
system
R = { R1, R2, ..., Rm }, the set consisting of all resource types in the
system
request edge – directed edge Pi ->Rj
assignment edge – directed edge Rj ->Pi

24/55
Operating Systems - Unit-III February 24, 2024 24 / 55
Deadlock Characterization

Resource-Allocation Graph (Cont.)

25/55
Operating Systems - Unit-III February 24, 2024 25 / 55
Deadlock Characterization

Basic Facts

If graph contains no cycles - no deadlock


If graph contains a cycle -
if only one instance per resource type, then deadlock
if several instances per resource type, possibility of deadlock

26/55
Operating Systems - Unit-III February 24, 2024 26 / 55
Methods of Handling Deadlocks

Methods of Handling Deadlocks

Ensure that the system will never enter a deadlock state:


Deadlock prevention
Deadlock avoidence
Allow the system to enter a deadlock state and then recover
Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX

27/55
Operating Systems - Unit-III February 24, 2024 27 / 55
Methods of Handling Deadlocks Deadlock Prevention

Deadlock Prevention

Restrain the ways request can be made


Mutual Exclusion – not required for sharable resources (e.g., read-only
files); must hold for non-sharable resources
Hold and Wait – must guarantee that whenever a process requests a
resource, it does not hold any other resources
Require process to request and be allocated all its resources before it
begins execution, or allow process to request resources only when the
process has none allocated to it.
Low resource utilization; starvation possible

28/55
Operating Systems - Unit-III February 24, 2024 28 / 55
Methods of Handling Deadlocks Deadlock Prevention

Deadlock Prevention (Cont.)

No Preemption –
If a process that is holding some resources requests another resource
that cannot be immediately allocated to it, then all resources currently
being held are released
Preempted resources are added to the list of resources for which the
process is waiting
Process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting
Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration

29/55
Operating Systems - Unit-III February 24, 2024 29 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Deadlock Avoidance

Requires that the system has some additional a priori information


available.
Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to ensure that there can never be a
circular-wait condition
Resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes

30/55
Operating Systems - Unit-III February 24, 2024 30 / 55
Methods of Handling Deadlocks Deadlock Avoidance

SafeState

When a process requests an available resource, system must decide if


immediate allocation leaves the system in a safe state
System is in safe state if there exists a sequence <P1, P2, . . . , Pn
>of ALL the processes in the systems such that for each Pi, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j <I
That is:
If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished
When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
When Pi terminates, Pi +1 can obtain its needed resources, and so on

31/55
Operating Systems - Unit-III February 24, 2024 31 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Basic Facts

If a system is in safe state - >no deadlocks


If a system is in unsafe state - >possibility of deadlock
Avoidance - >ensure that a system will never enter an unsafe state.

32/55
Operating Systems - Unit-III February 24, 2024 32 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Safe, Unsafe, Deadlock State

33/55
Operating Systems - Unit-III February 24, 2024 33 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Avoidance Algorithms

Single instance of a resource type


Use a resource-allocation graph
Multiple instances of a resource type
Use the banker’s algorithm

34/55
Operating Systems - Unit-III February 24, 2024 34 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Resource-Allocation Graph Scheme

Claim edge Pi ->Rj indicated that process Pj may request resource


Rj; represented by a dashed line
Claim edge converts to request edge when a process requests a
resource
Request edge converted to an assignment edge when the resource is
allocated to the process
When a resource is released by a process, assignment edge reconverts
to a claim edge Resources must be claimed a priori in the system

35/55
Operating Systems - Unit-III February 24, 2024 35 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Resource-Allocation Graph

36/55
Operating Systems - Unit-III February 24, 2024 36 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Unsafe State in Resource-Allocation Graph

37/55
Operating Systems - Unit-III February 24, 2024 37 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Resource-Allocation Graph Algorithm

Suppose that process Pi requests a resource Rj


The request can be granted only if converting the request edge to an
assignment edge does not result in the formation of a cycle in the
resource allocation graph

38/55
Operating Systems - Unit-III February 24, 2024 38 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Banker’s Algorithm

Multiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite
amount of time

39/55
Operating Systems - Unit-III February 24, 2024 39 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Data Structure for Banker’s Algorithm

Let n=number of processes, m=number of resources types.


Available: Vector of length m. If available [j] = k, there are k
instances of resource type Rj available
Max: n x m matrix. If Max [i,j] = k, then process Pi may request at
most k instances of resource type Rj
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently
allocated k instances of Rj
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more
instances of Rj to complete its task

Need [i,j] = Max[i,j] – Allocation [i,j]

40/55
Operating Systems - Unit-III February 24, 2024 40 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Safety Algorithm

41/55
Operating Systems - Unit-III February 24, 2024 41 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Resource-Request Algorithm for Process Pi

42/55
Operating Systems - Unit-III February 24, 2024 42 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Example - Bankers Algorithm

Is this a Safe State?


43/55
Operating Systems - Unit-III February 24, 2024 43 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Example (Cont.)

44/55
Operating Systems - Unit-III February 24, 2024 44 / 55
Methods of Handling Deadlocks Deadlock Avoidance

Example (Cont.)

45/55
Operating Systems - Unit-III February 24, 2024 45 / 55
Methods of Handling Deadlocks Deadlock Detection

Deadlock Detection

Allow system to enter deadlock state


Detection algorithm
Recovery scheme

46/55
Operating Systems - Unit-III February 24, 2024 46 / 55
Methods of Handling Deadlocks Deadlock Detection

Single Instance of Each Resource Type

Maintain wait-for graph


Nodes are processes
Pi ->Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock
An algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices in the graph

47/55
Operating Systems - Unit-III February 24, 2024 47 / 55
Methods of Handling Deadlocks Deadlock Detection

Resource-Allocation Graph and Wait-for Graph

(a)Resource Alocation Graph (b)Wait for Graph


48/55
Operating Systems - Unit-III February 24, 2024 48 / 55
Methods of Handling Deadlocks Deadlock Detection

Several Instances of a Resource Type

Available: A vector of length m indicates the number of available


resources of each type
Allocation: An n x m matrix defines the number of resources of each
type currently allocated to each process
Request: An n x m matrix indicates the current request of each
process. If Request [i][j] = k, then process Pi is requesting k more
instances of resource typeRj .

49/55
Operating Systems - Unit-III February 24, 2024 49 / 55
Methods of Handling Deadlocks Deadlock Detection

Detection Algorithm

This algorithm requires an order of O(m*n2 ) operations to detect whether


the system is in deadlock state or not
50/55
Operating Systems - Unit-III February 24, 2024 50 / 55
Methods of Handling Deadlocks Deadlock Detection

Example of Detection Algorithm

51/55
Operating Systems - Unit-III February 24, 2024 51 / 55
Methods of Handling Deadlocks Deadlock Detection

Example (Cont.)

52/55
Operating Systems - Unit-III February 24, 2024 52 / 55
Methods of Handling Deadlocks Deadlock Detection

Detection-Algorithm Usage

When, and how often, to invoke depends on:


How often a deadlock is likely to occur?
How many processes will need to be rolled back?
one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be many cycles
in the resource graph and so we would not be able to tell which of the
many deadlocked processes “caused” the deadlock.

53/55
Operating Systems - Unit-III February 24, 2024 53 / 55
Methods of Handling Deadlocks Deadlock Detection

Recovery from Deadlock: Process Termination

Abort all deadlocked processes


Abort one process at a time until the deadlock cycle is eliminated
In which order should we choose to abort?
Priority of the process
How long process has computed, and how much longer to completion
Resources the process has used
Resources process needs to complete
How many processes will need to be terminated
Is process interactive or batch?

54/55
Operating Systems - Unit-III February 24, 2024 54 / 55
Methods of Handling Deadlocks Deadlock Detection

Recovery from Deadlock: Resource Preemption

Selecting a victim – minimize cost


Rollback – return to some safe state, restart process for that state
Starvation – same process may always be picked as victim, include
number of rollback in cost factor

55/55
Operating Systems - Unit-III February 24, 2024 55 / 55

You might also like