0% found this document useful (0 votes)
3 views

Interprocess Communication.pptx

Uploaded by

Priya V. B
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Interprocess Communication.pptx

Uploaded by

Priya V. B
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Interprocess Communication

• Processes executing concurrently in the operating system may be either independent processes or
cooperating processes.

Independent Process Cooperating Process


cannot affect or be affected by the can affect or be affected by the other
other processes executing in the processes executing in the system
system
does not share data with any other Shares the data with other process
process
Reasons for Cooperating Process
1. Information sharing: Since several users may be interested in the same piece of information (for
instance, a shared file), provide an environment to allow concurrent access to such information.
2. Computation speedup: To run a particular task much faster, break it into subtasks, each of which
will be executing in parallel with the others.
3. Modularity: To construct the system in a modular fashion, dividing the system functions into
separate processes or threads,
4. Convenience: Even an individual user may work on many tasks at the same time. For instance, a
user may be editing, listening to music, and compiling in parallel.
Interprocess Communication
• Cooperating processes require an interprocess communication (IPC) mechanism that will allow
them to exchange data and information.
• Two models:
1. Shared memory
2. Message passing.
Shared Memory Model
• IPC using shared memory requires communicating processes to establish a region of shared
memory.
• Processes can then exchange information by reading and writing data to the shared region
• A shared-memory region resides in the address space of the process creating the shared-memory
segment.
• Other processes that wish to communicate using this shared-memory segment must attach it to
their address space.
• They can then exchange information by reading and writing data in the shared areas.
• The form of the data and the location are determined by these processes and are not under the
operating system’s control.
• The processes are also responsible for ensuring that they are not writing to the same location
simultaneously.
Producer Consumer Problem
• A producer process produces information that is consumed by a consumer process.
• Server as a producer and a client as a consumer.
• For example, a web server produces (that is, provides) HTML files and images, which
are consumed (that is, read) by the client web browser requesting the resource.
• A solution for producer–consumer problem uses shared memory.
• This buffer will reside in a region of memory that is shared by the producer and
consumer processes.
• The producer and consumer must be synchronized, so that the consumer does not try to
consume an item that has not yet been produced.
• Two types of buffers:
1. unbounded-buffer places no practical limit on the size of the buffer
2. bounded-buffer assumes that there is a fixed buffer size
Bounded-Buffer
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;

item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
• The shared buffer is implemented as a circular array with two logical pointers: in and out.
• The variable in points to the next free position in the buffer;
• out points to the first full position in the buffer.
• The buffer is empty when in = =out;
• the buffer is full when ((in + 1) % BUFFER SIZE) = = out.
Producer
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;

/* consume the item in next consumed */


}
• 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
Message Passing Model
• Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the
same address space.
• It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers
connected by a network.
• Eg: Internet Chat Program
• A message-passing facility provides at least two operations:
1. send(message)
2. receive(message)
Fixed Size Message Variable Size Message

System-level implementation is straightforward More complex system level implementation

task of programming more difficult programming task becomes simpler


• If processes P and Q wish to communicate, they need to:
• Establish a communication link between them
• Exchange messages via send/receive
• Implementation of communication link
• Physical:
• Shared memory
• Hardware bus
• Network
• Logical:
• Direct or indirect communication
• Synchronous or asynchronous communication
• Automatic or explicit buffering
1. Naming
• Processes that want to communicate must have a way to refer to each other.
• Two ways
1. Direct Communication
2. Indirect Communication
Direct Communication
• Each process that wants to communicate must explicitly name the recipient or sender of
the communication.
Symmetry in addressing:
• Primitives: both the sender
• send(P, message)—Send a message to process P. process and the receiver
• receive(Q, message)—Receive a message from process Q. process must name the
other to communicate

• A communication link properties:


1. A link is established automatically between every pair of processes that want to
communicate. The processes need to know only each other’s identity to communicate.
2. A link is associated with exactly two processes.
3. Between each pair of processes, there exists exactly one link.
Variant of Direct Communication Asymmetry in
addressing

• only the sender names the recipient; the recipient is not required to name the sender
• Primitives:
• 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.
Disadvantage:
• Both of these schemes (symmetric and asymmetric) is the limited modularity of the resulting
process definitions. Changing the identifier of a process may necessitate examining all other process
definitions.
Indirect Communication
• The messages are sent to and received from mailboxes, or ports.
• A mailbox can be viewed abstractly as an object into which messages can be placed by
processes and from which messages can be removed.
• Each mailbox has a unique identification.
• A process can communicate with another process via a number of different mailboxes, but
two processes can communicate only if they have a shared mailbox
• Primitives
• send(A, message)—Send a message to mailbox A.
• receive(A, message)—Receive a message from mailbox A.
• Communication link properties:
1. A link is established between a pair of processes only if both members of the pair have
a shared mailbox.
2. A link may be associated with more than two processes.
3. Between each pair of communicating processes, a number of different links may exist,
with each link corresponding to one mailbox.
Now suppose that processes P1, P2, and P3 all share mailbox A. Process P1 sends a message
to A, while both P2 and P3 execute a receive() from A.
P2
Which process will receive the message sent by P1?

P1 A

P3
The answer depends on which of the following methods we choose:
• Allow a link to be associated with two processes at most.
• Allow at most one process at a time to execute a receive() operation.
• Allow the system to select arbitrarily which process will receive the message. The system
may define an algorithm for selecting which process will receive the message .The system
may identify the receiver to the sender.
• A mailbox may be owned either by a process or by the operating system.
• If the mailbox is owned by a process
• the mailbox is part of the address space of the process
• Each mailbox has a unique owner, there can be no confusion about which process
should receive a message sent to this mailbox.
• When a process that owns a mailbox terminates, the mailbox disappears.

• If the mailbox is owned by the operating system has


• It is independent and is not attached to any particular process.
• The operating system then must provide a mechanism that allows a process to do the
following:
1. Create a new mailbox.
2. Send and receive messages through the mailbox.
3. Delete a mailbox.
• the ownership and receiving privilege may be passed to other processes through
appropriate system calls
2. Synchronization
• Message passing may be either blocking or non-blocking
• Blocking is considered synchronous
• Blocking send -- the sender is blocked until the message is received
• Blocking receive -- the receiver is blocked until a message is available
• Non-blocking is considered asynchronous
• Non-blocking send -- the sender sends the message and continue
• Non-blocking receive -- the receiver receives:
1. A valid message, or
2. Null message
• Different combinations possible
• If both send and receive are blocking, we have a rendezvous
3. Buffering
• Whether communication is direct or indirect, messages exchanged by communicating
processes reside in a temporary queue.
• Temporary queues can be implemented in three ways
1. Zero capacity
2. Bounded
3. Unbounded
1. Zero capacity:
• The queue has a maximum length of zero; thus, the link cannot have any messages
waiting in it.
• The sender must block until the recipient receives the message.
• It is referred to as a message system with no buffering
2. Bounded capacity:
• The queue has finite length n; thus, at most n messages can reside in it.
• If the queue is not full when a new message is sent, the message is placed in the
queue and the sender can continue execution without waiting.
Automatic
• If the link is full, the sender must block until space is available in the queue. Buffering
3. Unbounded capacity:
• The queue’s length is potentially infinite; thus, any number of messages can wait in it.
• The sender never blocks.
• Message passing
• exchanging smaller amounts of data,
• no conflicts need be avoided.
• easier to implement in a distributed system than shared memory.
• implemented using system calls and thus require the more time-consuming task of
kernel intervention
• Shared memory
• faster than message passing
• System calls are required only to establish shared- memory regions. Once shared
memory is established, all accesses are treated as routine memory accesses, and no
assistance from the kernel is required.

You might also like