Interprocess Communication.pptx
Interprocess Communication.pptx
• Processes executing concurrently in the operating system may be either independent processes or
cooperating processes.
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;
• 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.