Interprocess Communication
- Information sharing
- Computation speedup
- Modularity
- Convenience
- Shared memory
- Message passing
Communications Models
Cooperating Processes
- Information sharing
- Computation speed-up
- Modularity
- Convenience
Producer-Consumer Problem
- unbounded-buffer places no practical limit on the size of the buffer
- bounded-buffer assumes that there is a fixed buffer size
Bounded-Buffer–Shared-Memory Solution
#define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;Solution is correct, but can only use BUFFER_SIZE-1 elements
Bounded-Buffer – Producer
while (true) { /* Produce an item */ while (((in = (in + 1) % BUFFER SIZE count) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; }
Bounded Buffer – Consumer
while (true) { while (in == out) ; // do nothing -- nothing to consume // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; }
Interprocess Communication – Message Passing
- send(message) – message size fixed or variable
- receive(message)
- establish a communication link between them
- exchange messages via send/receive
- physical (e.g., shared memory, hardware bus)
- logical (e.g., logical properties)
Implementation Questions
Direct Communication
- send (P, message) – send a message to process P
- receive(Q, message) – receive a message from process Q
- 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
Indirect Communication
- Each mailbox has a unique id
- Processes can communicate only if they share a mailbox
- 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
- Operations
- create a new mailbox
- send and receive messages through mailbox
- destroy a mailbox
- 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.
- Message passing may be either blocking or non-blocking
- Blocking is considered synchronous
- Blocking send has the sender block until the message is received
- Blocking receive has the receiver block until a message is available
- Non-blocking is considered asynchronous
- Non-blocking send has the sender send the message and continue
- Non-blocking receive has the receiver receive a valid message or null