Operating Systems - IPC: Inter-Process Communication: Message Passing
Operating Systems - IPC: Inter-Process Communication: Message Passing
Operating Systems - IPC: Inter-Process Communication: Message Passing
Message Passing
Both concepts require some kind of shared memory How about synchronization problems in a distributed system (no shared memory)?
ECE 344 Operating Systems
Message Passing
An inter-process communication mechanism Based on two primitives
send (destination, &message)
Sends a message to a destination
System calls, not language constructs Blocking versions and non-blocking versions are available
Message Passing
send() receive()
Design Issues
Various design concerns not applicable in semaphore & monitor discussion: Networked system, i.e., messages maybe lost (network is unreliable) Sender/receiver agree to use acknowledgements and retransmission timers Message received, but acknowledgement lost
Receiver will get the same message twice Use consecutive sequence numbers Computer Networks
msg.
ack
ECE 344 Operating Systems
Design Issues
Naming of processes for unambiguous send/receive Authentication, how can client know it is communicating with proper server Tune for performance, if sender & receiver are on the same machine (semaphore is usually cheaper)
P2 Request P4 buffer
Consumer
Producer
void consumer(void)
int item; message m; // initialization loop for (i=0; i<N; i++) send(producer, &m); // N empty slots while (TRUE) { receive(producer, &m); // receive item item=extract_item(&m); send(producer, &m); // send empty slot consume_item(item); }
ECE 344 Operating Systems
Consumer Initialization
Represents the empty slots.
Consumer
Producer
void producer(void)
int item; message m; while (TRUE) { item Produce_Item(); receive(consumer, &m); // empty slot build_message(&m, item); send(consumer, &m); // send item }
ECE 344 Operating Systems
Producer-Consumer
consumer // N empty messages (i.e., slots) producer
Consumer sends N empty messages (i.e., slots) Producer picks up an empty message/slot and send back an item produced Consumer receives an item and sends back an empty message (i.e., a slot) Messages sent, but not received are buffered by OS
ECE 344 Operating Systems
Characteristics
Direct Naming
send(j,m) i send(j,m) i k receive(&from,m) j receive(i,m) j
Indirect Naming
Mailbox
Mailbox
Mailbox
Mailbox
Mailboxes
Who should get the message? receive receive It depends: At most one process may execute a receive operation at a time The system arbitrarily selects one as the receiver Message is retrieved by both (a form of multicast) Ownership Either process can own the mailbox OS can own it and/or creator owns it send
ECE 344 Operating Systems
Sender/Receiver Synchronization
Message passing maybe blocking or non-blocking (synchronous or asynchronous) Blocking send sender blocked until message is received by receiver (or by mailbox) Non-blocking send sending process resumes operation right after sending Blocking receive receiver blocks until message is available Non-blocking receive receiver retrieves a valid message or returns an error code Any combination of the above send/receive is possible
ECE 344 Operating Systems
blocking
Buffering
The mechanism that buffers messages (a.k.a. queue) may have the following properties
Zero capacity: queue has length 0, no messages can be outstanding on link, sender blocks for message exchange Bounded capacity: queue has length N, N messages can be in queue at any point in time, sender blocks if queue is full, otherwise it may continue to execute Unbounded capacity: queue has infinite length, sender never blocks
ECE 344 Operating Systems
Buffering
No buffering, send blocks until receive happens or vice versa, then copy message (called a rendezvous)
Copy-on-write
Message passing for process-to-process communication can be quite inefficient due to context switching etc. Copy-on-write first sets a pointer in receivers address space pointing to received data in senders address space Only if sender / receiver start writing this data will the OS copy the actual data from sender to receiver Otherwise, no copying of the data is performed
ECE 344 Operating Systems