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

COS 318: Operating Systems Message Passing

Message passing allows processes to communicate by sending and receiving messages. It provides implicit synchronization between processes. Key aspects of message passing include synchronous vs asynchronous communication, buffering strategies, direct vs indirect addressing, and handling exceptions like process termination and message loss or corruption. Careful API design and implementation techniques are needed to build a robust and flexible message passing system.

Uploaded by

Manak Soni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

COS 318: Operating Systems Message Passing

Message passing allows processes to communicate by sending and receiving messages. It provides implicit synchronization between processes. Key aspects of message passing include synchronous vs asynchronous communication, buffering strategies, direct vs indirect addressing, and handling exceptions like process termination and message loss or corruption. Careful API design and implementation techniques are needed to build a robust and flexible message passing system.

Uploaded by

Manak Soni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

COS 318: Operating Systems Message Passing

Todays Topics
!

Message passing
" "

Semantics How to use Synchronous vs. asynchronous Buffering Indirection Exceptions

Implementation issues
" " " "

Big Picture
Sender Receiver

Process

Process

Send and Receive Primitives


Sender
pid, file, port,

Receiver
pid, file, port, any,

msg type

send(dest, type, msg)

recv(src, type, msg)

expected msg type

buffer, n-bytes

buffer, ?-bytes

Many ways to design the message passing API

Synchronous Message Passing


!

Move data between processes


" "

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 );

Recv( S, &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
!

A single buffer at the receiver


" "

More than one process may send messages to the receiver To receive from a specific sender, it requires searching through the whole buffer

A buffer at each sender


" "

A sender may send messages to multiple receivers To get a message, it also requires searching through the whole buffer

Indirect Communication
!

Use mailbox as the abstraction


" "

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
"

Mailbox vs. pipe


" "

pipe

10

Synchronous vs. Asynchronous: Send


!

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

send( dest, type, msg)

msg transfer resource

Asynchronous
" " "

status = async_send( dest, type, msg ) if !send_complete( status ) wait for completion; use msg data structure;

11

Synchronous vs. Asynchronous: Receive


!

Synchronous
"

Return data if there is a message

msg transfer resource recv( src, type, msg )


status = async_recv( src, type, msg ); if ( status == SUCCESS ) consume msg; while ( probe(src) != HaveMSG ) wait for msg arrival recv( src, type, msg ); consume msg;

Asynchronous
" "

Return data if there is a message Return status if there is no message (probe)

12

Event Handler vs. Receive


!

hrecv( src, type, msg, func )


" "

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

void func( char * msg ) { } hrecv( src, type, msg, func)

Which one is more powerful?


" "

Pros and Cons

Create a thread

program

while(1) { recv(src,type, msg); func(msg); }


13

Example: Keyboard Input


!

How do you implement keyboard input?


" "

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

Exception: Process Termination


!

R waits for a message from S, but S has terminated


"

Problem: R may be blocked forever

S sends a message to R, but R has terminated


"

Problem: S has no buffer and will be blocked forever

15

Exception: Message Loss


!

Use ack and timeout to detect and retransmit a lost message


" "

"

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

Exception: Message Loss, contd


!

Retransmission must handle


" "

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
" " "

send1 ack1 S send2 ack2 R

Reduce ack messages


" " "

17

Exception: Message Corruption


Data
Compute checksum
!

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

Example: Sockets API


! !

Abstraction for TCP and UDP


"

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

Create and close a socket


" "

Bind a socket to a local address


"

connect read write write read

Negotiate the connection


" "

Connect a socket to destimation


"

19

Summary
!

Message passing
" " "

Move data between processes Implicit synchronization API design is important

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

You might also like