Cooperating Processes n Independent process cannot affect or

  • Slides: 15
Download presentation
Cooperating Processes n Independent process cannot affect or be affected by the execution of

Cooperating Processes n Independent process cannot affect or be affected by the execution of another process. n Cooperating process can affect or be affected by the execution of another process n Advantages of process cooperation F Information sharing F Computation speed-up F Modularity F Convenience F Robustness Operating System Concepts 4. 1 Silberschatz, Galvin and Gagne 2002

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces information that is consumed

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. F unbounded-buffer places no practical limit on the size of the buffer. F bounded-buffer assumes that there is a fixed buffer size. Operating System Concepts 4. 2 Silberschatz, Galvin and Gagne 2002

Mechanisms for IPC Operating System Concepts 4. 3 Silberschatz, Galvin and Gagne 2002

Mechanisms for IPC Operating System Concepts 4. 3 Silberschatz, Galvin and Gagne 2002

Bounded-Buffer – Shared Memory Solution n Shared data #define BUFFER_SIZE 10 typedef struct {.

Bounded-Buffer – Shared Memory Solution n Shared data #define BUFFER_SIZE 10 typedef struct {. . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Operating System Concepts 4. 4 Silberschatz, Galvin and Gagne 2002

Bounded-Buffer – Shared Memory Solution n Producer while (1) { while (((in + 1)

Bounded-Buffer – Shared Memory Solution n Producer while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = next. Produced; in = (in + 1) % BUFFER_SIZE; } n Consumer while (1) { while (in == out) ; /* do nothing */ next. Consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; n Can only use BUFFER_SIZE-1 elements Operating System Concepts 4. 5 Silberschatz, Galvin and Gagne 2002

Message Passing n Processes communicate without shared variables n If P and Q wish

Message Passing n Processes communicate without shared variables n If P and Q wish to communicate, they need to: F establish a communication link between them F send(message) F receive(message) n Producer - Consumer while (1) { send(consumer, produce. Next()); } while (1) { next. To. Consume = receive(producer); } Operating System Concepts 4. 6 Silberschatz, Galvin and Gagne 2002

Message Passing - Link Properties n How are links established? n Can a link

Message Passing - Link Properties n How are links established? n Can a link be associated with more than two processes? n How many links can there be between processes? n What is the capacity of a link? (buffers? ) n Fixed or variable size messages? n Is the link simplex or duplex or full duplex? Operating System Concepts 4. 7 Silberschatz, Galvin and Gagne 2002

Message Passing - Logical Properties n Direct or indirect communication (process or mailbox) n

Message Passing - Logical Properties n Direct or indirect communication (process or mailbox) n Symmetric or asymmetric (names both ways or one way) n Automatic or explicit or no buffering F Send by copy or send by reference n Fixed or variable size messages n Synchronous (blocking) or asynchronous (non-blocking) Operating System Concepts 4. 8 Silberschatz, Galvin and Gagne 2002

Symmetry of naming Operating System Concepts 4. 9 Silberschatz, Galvin and Gagne 2002

Symmetry of naming Operating System Concepts 4. 9 Silberschatz, Galvin and Gagne 2002

Direct Communication n Symmetric: Processes name each explicitly F send (P, message) – send

Direct Communication n Symmetric: Processes name each explicitly F send (P, message) – send a message to process P F receive(Q, message) – receive a message from process Q F Links are established automatically, on demand. F A link is associated with exactly two processes. F Between each pair there exists exactly one link. n Asymmetric: Only sender names the receiver F send (P, message) – send message to process P F receive(? , message) – receive message from anyone n Asymmetric: Only receiver names the sender F send (? , message) – send message to anyone (copies? ) F receive(Q, message) – receive message from process Q Operating System Concepts 4. 10 Silberschatz, Galvin and Gagne 2002

Indirect Communication n Messages are directed and received from mailboxes (also referred to as

Indirect Communication n Messages are directed and received from mailboxes (also referred to as ports). F Each mailbox has a unique id. F Processes can communicate only if they share a mailbox. n Operations F Create a new mailbox (Ownership? Who receives? ) F send(A, message) – send a message to mailbox A F message = receive(A) – receive a message from A F Destroy a mailbox (? If owner terminates) n Properties of communication link F Link established only if processes share a common mailbox F A link may be associated with many processes. F Each pair of processes may share several links. Operating System Concepts 4. 11 Silberschatz, Galvin and Gagne 2002

Indirect Communication n Mailbox sharing F P 1, P 2, and P 3 share

Indirect Communication n Mailbox sharing F P 1, P 2, and P 3 share mailbox A. F P 1, sends; P 2 and P 3 receive. F Who gets the message? n Solutions F Allow a link to be associated with at most two processes. F Allow only one process at a time to execute a receive F Allow the system to select arbitrarily the receiver. 4 Sender is notified who the receiver was. n Atomicity, regardless Operating System Concepts 4. 12 Silberschatz, Galvin and Gagne 2002

Synchronization n Send and receive can be blocking or non-blocking n Blocking send waits

Synchronization n Send and receive can be blocking or non-blocking n Blocking send waits until message is received n Non-blocking send allows sender to continue F Sender is not assured of reception - must ack F Sender send(receiver, message); receive(receiver, ack_message); F Receiver receive(sender, message); send(sender, ”ack”); n Blocking receive waits until message is available n Non-blocking receive returns null if no message n Blocking send and receives forces a rendezvous Operating System Concepts 4. 13 Silberschatz, Galvin and Gagne 2002

Buffering n Queue of messages attached to the link; implemented in one of three

Buffering n Queue of messages attached to the link; implemented in one of three ways. Zero capacity – 0 messages Sender must wait for receiver (must rendezvous). o Bounded capacity – finite length of n messages Sender must wait if link full, or overwrite o Unbounded capacity – infinite length (right!). o Operating System Concepts 4. 14 Silberschatz, Galvin and Gagne 2002

Exception Conditions n Process terminates F Receiver waiting for a message from terminated sender

Exception Conditions n Process terminates F Receiver waiting for a message from terminated sender 4 Notify or terminate receiver F Sending to a terminated receiver 4 Delete message 4 Notify sender? n Lost messages F Detect with timeouts - expect an ack within some time F Resend (duplicates) n Scrambled messages F Error checking codes Operating System Concepts 4. 15 Silberschatz, Galvin and Gagne 2002