Title: INTERPROCESS COMMUNICATION
1INTERPROCESS COMMUNICATION AND SYNCHRONIZATION
2Concurrency - Basic Concept
- In a multi-programming environment, there will be
concurrent processes which are of two types - Operating system processes (those that execute
system code) - User processes (those that execute users code)
3Concurrency - Basic Concept
- A simple batch operating system can be viewed as
3 processes - a reader process
- an executor process
- a printer process
Input Buffer
Process
Output Buffer
4Inter-Process Communication
Basic Concepts of IPC Synchronization In order
to cooperate, concurrently executing processes
must communicate and synchronize. Interprocess
communication is based on the use of shared
variables (variables that can be referenced by
more than one process) or message passing.
5Inter-Process Communication
Basic Concepts of IPC Synchronization Synchroni
zation is often necessary when processes
communicate. Processes are executed with
unpredictable speed. Yet to communicate one
process must perform some action such as setting
the value of a variable or sending a message that
the other detects. This only works if the events
perform an action or detect an action are
constrained to happen in that order. Thus one can
view synchronization as a set of constraints on
the ordering of events. The programmer employs a
synchronization mechanism to delay execution of a
process in order to satisfy such constraints.
6Inter-Process Communication
IPC is a capability supported by operating system
that allows one process to communicate with
another process. The processes can be running on
the same computer or on different computers
connected through a network. IPC enables one
application to control another application, and
for several applications to share the same data
without interfering with one another.
7Inter-Process Communication
- Critical Resource
- Critical Section
- Mutual Exclusion
8Inter-Process Communication
- Critical Resource
- It is a resource shared with constraints on its
use(e.g., memory, files, printers, etc) - Critical Section
- Mutual Exclusion
9Inter-Process Communication
- Critical Resource
- Critical Section
- It is code that accesses a critical resource.
- Mutual Exclusion
10Inter-Process Communication
- Critical Resource
- Critical Section
- Mutual Exclusion
- At most one process may be executing a critical
section with respect to a particular critical
resource simultaneously.
11Inter-Process Communication
- IPC can be possible in two different ways
- Shared-Memory System
- Message-Passing System
12Shared-Memory System
Shared-memory system require communication
processes to share some variables. The processes
are expected to exchange information through the
use of these shared variables. Responsibility for
providing communication rests with the
application programmer. The OS only needs to
provide shared memory.
13Message-Passing System
- MPS allow communication processes to exchange
messages responsibility for providing
communication rest with OS. - The function of a MPS is to allow processes to
communicate with each other without the need to
resort to shared variables. - An IPC facility basically provides two
operations - send(message)
- receive(message)
14Message-Passing System
- MPS allow communication processes to exchange
messages responsibility for providing
communication rest with OS. - The function of a MPS is to allow processes to
communicate with each other without the need to
resort to shared variables. - An IPC facility basically provides two
operations - send(message)
- receive(message)
In order to send and to receive messages, a
communication link must exist between the two
involved processes.
15Message-Passing System
- Methods for logically implementing a
communication link and the send/receive
operations are classified into - Naming
- Buffering
16Message-Passing System
- Methods for logically implementing a
communication link and the send/receive
operations are classified into - Naming
- Buffering
Consisting of direct and indirect communication.
17Message-Passing System
- Methods for logically implementing a
communication link and the send/receive
operations are classified into - Naming
- Buffering
Consisting of capacity and message properties.
18Direct Communication
- Each process that wants to communicate must
explicitly name the recipient or sender of the
communication. In this scheme the send and
receive primitives are - send(P, message) send a message to process P
- receive(Q, message) receive a message from
process Q
19Direct Communication
- Each process that wants to communicate must
explicitly name the recipient or sender of the
communication. In this scheme the send and
receive primitives are - send(P, message) send a message to process P
- receive(Q, message) receive a message from
process Q
Here a link is established automatically between
every pair of processes that want to communicate.
Exactly one link exists between each pair of
processes.
20Direct Communication
- Symmetry in addressing
- send(P, message) send a message to process P
- receive(Q, message) receive a message from
process Q - This scheme shows the symmetry in addressing
that is both the sender and the receiver
processes must name the other to communicate.
21Direct Communication
- Asymmetry in addressing
- send(P, message) send a message to process P
- receive(id, message) receive a message from
any process the variable id is set to the name
of the process with which communication has
taken place. - Only the sender names the recipient the
recipient is not required to name the sender.
22Indirect Communication
- With indirect communication, the message are sent
to and receive from a mailbox. It is an object
into which messages may be placed and from which
messages may be removed by processes. Each
mailbox owns a unique identification. A process
may communicate with some other process by a
number of different mailboxes. - send(A, message) send a message to mailbox A
- receive(A, message) receive a message from
mailbox A
23Indirect Communication
Mailbox owned by process Mailboxes may be owned
by either by a process or by the system. If the
mailbox is owned by a process, then the owner
who can only receive from this mailbox and the
user who can only send message to the mailbox are
to be distinguished. When a process that owns a
mailbox terminates, its mailbox disappears.
24Indirect Communication
- Mailbox owned by the OS
- It has an existence of its own, i.e., it is
independent and not attached to any particular
process. - The OS provides a mechanism that allows a process
to - create a new mailbox
- send and receive message through the mailbox
- destroy a mailbox
25Buffering
- Whether the communication is direct or indirect,
messages exchanged by communicating processes
reside in a temporary queue. This queue can be
implemented in three ways - Zero capacity
- Bounded capacity
- Unbounded capacity
26Buffering - Capacity
Zero capacity The queue has maximum length 0
thus, the link cannot have any messages waiting
in it. In this case, the sender must block until
the recipient receives the message. The
zero-capacity link is referred to as a
message-passing system with no buffering.
27Buffering - Capacity
Bounded capacity The queue has finite length n
thus, at most n messages can reside in it. If a
new message is sent, and the queue is not full,
it is placed in the queue either by copying the
message or by keeping a pointer to the message
and the sender can continue execution without
waiting. If the link is full, the sender must
block until space is available in the queue.
28Buffering - Capacity
Unbounded capacity The queue has potentially
infinite length thus, any number of messages can
wait in it. The sender never blocks. Bounded and
Unbounded capacity link is referred to as
message-passing system with automatic buffering.
29Interprocess Synchronization
next file to be printed
Race Condition Value depends on which of the
processes wins the race to update the variable.
. .
Out 4
4
abc
5
Prog.c
In 7
6
Prog.n
Process A
7
Process B
next free slot in the directory
. .
30Interprocess Synchronization
- Serialization
- Make an operating system not to perform several
tasks in parallel. - Two strategies to serializing processes in a
multitasking environment - The Scheduler can be disabled
- A Protocol can be introduced
31Interprocess Synchronization
- The Scheduler can be disabled
- Scheduler can be disabled for a short period of
time, to prevent control being given to another
process during a critical action like modifying
shared data. - Inefficient on multiprocessor machines, since all
other processors have to be halted every time one
wishes to execute a critical section.
32Interprocess Synchronization
- A Protocol can be introduced
- A protocol can be introduced which all programs
sharing data must obey. The protocol ensures that
processes have to queue up to gain access to
shared data.
33Critical Section
Do entry section critical section
exit section remainder section
while(1) General structure of a typical process
34Critical Section
Do entry section critical section
exit section remainder section
while(1) General structure of a typical process
Section of code that request permission to enter
its critical section.
35Critical Section
Do entry section critical section
exit section remainder section
while(1) General structure of a typical process
It is a part of code in which it is necessary to
have exclusive access to shared data.
36Critical Section
Do entry section critical section
exit section remainder section
while(1) General structure of a typical process
Code for tasks just after exiting from the
critical section.
37Critical Section
Do entry section critical section
exit section remainder section
while(1) General structure of a typical process
The remaining code.
38Mutexes Mutual Exclusion
var P1busy, P2busy boolean
parent process P1busyfalse
P2busyfalse initiate P1, P2 end mutex
39Mutexes Mutual Exclusion
var P1busy, P2busy boolean
process P1 begin while true do begin
P1busy true while P2busy do keep
testing critical-section
P1busyfalse other_P1busy_processing
endwhile End P1
parent process P1busyfalse
P2busyfalse initiate P1, P2 end mutex
40Mutexes Mutual Exclusion
process P2 begin while true do begin
P2busy true while P1busy do keep
testing critical-section
P2busyfalse other_P2busy_processing
endwhile End P2
var P1busy, P2busy boolean
process P1 begin while true do begin
P1busy true while P2busy do keep
testing critical-section
P1busyfalse other_P1busy_processing
endwhile End P1
parent process P1busyfalse
P2busyfalse initiate P1, P2 end mutex
41Critical-Section Problem
boolean flag2 int turn do flagi
true turn j while (flagj
turn j) Critical-section flagi
false remainder-section while(1)
Initially flag0flag1false Turn 0 or 1.
42Bakery Algorithm
boolean choosingn int numbern do
choosingi true numberi
max(number0, number1, , numbern-1) 1
choosingi flase for (j0 jltn
j) while (choosingj)
while((numberj!0) ((numberj,j) lt
(numberi,j)))
critical-section numberi 0
remainder-section while(1)
43Thank You
- Murugan R
- Dept. Computer Applications
- MES College Marampally