COS 318: Operating Systems Message Passing
COS 318: Operating Systems Message Passing
Todays Topics
!
Message passing
" "
Implementation issues
" " " "
Big Picture
Sender Receiver
Process
Process
Receiver
pid, file, port, any,
msg type
buffer, n-bytes
buffer, ?-bytes
Sender: when data is ready, send it to the receiver process Receiver: when the data has arrived and when the receive process is ready to take the data, move the data to the destination data structure Sender: signal the receiver process that a particular event happens Receiver: block until the event has happened
Synchronization
" "
Send( R, buf, n );
R
5
Example: Producer-Consumer
Producer(){ ... while (1) { produce item; recv(Consumer, &credit); send(Consumer, item); } } Consumer(){ ... for (i=0; i<N; i++) send(Producer, credit); while (1) { recv(Producer, &item); send(Producer, credit); consume item; } }
Questions
" " " "
Does this work? Would it work with multiple producers and 1 consumer? Would it work with 1 producer and multiple consumers? What about multiple producers and multiple consumers?
Implementation Issues
Buffering messages ! Direct vs. indirect ! Unidirectional vs. bidirectional ! Asynchronous vs. synchronous ! Event handler vs. receive ! How to handle exceptions?
!
Buffering Messages
!
No buffering
" "
Sender must wait until the receiver receives the message Rendezvous on each message Finite size Sender blocks on buffer full Use monitor to solve the problem Infinite size Sender never blocks
Bounded buffer
" " "
Unbounded buffer
" "
buffer
Direct Communication
!
More than one process may send messages to the receiver To receive from a specific sender, it requires searching through the whole buffer
A sender may send messages to multiple receivers To get a message, it also requires searching through the whole buffer
Indirect Communication
!
Allow many-to-many communication Require open/close a mailbox A buffer, its mutex and condition variables should be at the mailbox Not necessarily. One can break a large message into packets A mailbox allows many to many communication A pipe implies one sender and one receiver
Buffering
"
mbox
Message size
"
pipe
10
Synchronous
" " " "
Block if resource is busy Initiate data transfer Block until data is out of its source memory Rendezvous: block until receiver has done recv and sent acknowledgment Block if resource is busy Initiate data transfer and return Completion
Require applications to check status Notify or signal the application
Asynchronous
" " "
status = async_send( dest, type, msg ) if !send_complete( status ) wait for completion; use msg data structure;
11
Synchronous
"
Asynchronous
" "
12
msg is an arg of func Execute func on a message arrival Recv with a thread can emulate a Handler Handler can be used to emulate recv by using Monitor
Create a thread
program
Need an interrupt handler Generate a mbox message from the interrupt handler
Suppose a keyboard device thread converts input characters into an mbox message
" "
How would you synchronize between the keyboard interrupt handler and device thread? How can a device thread convert input into mbox messages?
while (1) { P(s); Acquire(m); convert Release(m); };
V(s);
mbox
Process
14
Interrupt handler
Device thread
15
"
Require the receiver to send an ack message for each message Sender blocks until an ack message is back or timeout status = send( dest, msg, timeout ); If timeout happens and no ack, then retransmit the message Duplicates Losing ack messages
send S ack R
Issues
" "
16
Duplicate messages on receiver side Out-of-sequence ack messages on sender side Use sequence number for each message to identify duplicates Remove duplicates on receiver side Sender retransmits on an out-ofsequence ack Bundle ack messages Receiver sends noack messages: can be complex Piggy-back acks in send messages
Retransmission
" " "
17
Detection
" "
Compute a checksum over the entire message and send the checksum (e.g. CRC code) as part of the message Recompute a checksum on receive and compare with the checksum in the message Trigger retransmission Use correction codes to recover
Correction
" "
18
Guest lecture by Prof. Rexford on 11/29 IP address and port number (216 ports available for users) sockid = socket(af, type, protocol); Sockerr = close(sockid); sockerr = bind(sockid, localaddr, addrlength); listen(sockid, length); accept(sockid, addr, length); connect(sockid, destaddr, addrlength);
Addressing
"
Server
socket bind listen accept
Client
socket
19
Summary
!
Message passing
" " "
Implementation issues
" " " "
Synchronous method is most common Asynchronous method provides overlapping but requires careful design considerations Indirection makes implementation flexible Exception needs to be carefully handled
20