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

Cooperating Processes

Embed Size (px)

DESCRIPTION

Cooperating Processes. Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience - PowerPoint PPT Presentation

Citation preview

Page 1: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.1Operating System Concepts

Cooperating Processes

Independent process cannot affect or be affected by the execution of another process.

Cooperating process can affect or be affected by the execution of another process

Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience Robustness

Page 2: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.2Operating System Concepts

Producer-Consumer Problem

Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. unbounded-buffer places no practical limit on the size of the

buffer. bounded-buffer assumes that there is a fixed buffer size.

Page 3: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.3Operating System Concepts

Mechanisms for IPC

Page 4: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.4Operating System Concepts

Bounded-Buffer – Shared Memory Solution

Shared data

#define BUFFER_SIZE 10

typedef struct {

. . .

} item;

item buffer[BUFFER_SIZE];

int in = 0;

int out = 0;

Page 5: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.5Operating System Concepts

Producerwhile (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE;}

Consumerwhile (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE;

Can only use BUFFER_SIZE-1 elements

Bounded-Buffer – Shared Memory Solution

Page 6: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.6Operating System Concepts

Message Passing

Processes communicate without shared variables

If P and Q wish to communicate, they need to: establish a communication link between them send(message) receive(message)

Producer - Consumer while (1) { send(consumer,produceNext()); }

while (1) { nextToConsume = receive(producer); }

Page 7: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.7Operating System Concepts

Message Passing - Link Properties

How are links established? Can a link be associated with more than two processes? How many links can there be between processes? What is the capacity of a link? (buffers?) Fixed or variable size messages? Is the link simplex or duplex or full duplex?

Page 8: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.8Operating System Concepts

Message Passing - Logical Properties

Direct or indirect communication (process or mailbox) Symmetric or asymmetric (names both ways or one way) Automatic or explicit or no buffering

Send by copy or send by reference Fixed or variable size messages Synchronous (blocking) or asynchronous (non-blocking)

Page 9: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.9Operating System Concepts

Symmetry of naming

Page 10: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.10Operating System Concepts

Direct Communication

Symmetric: Processes name each explicitly send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q Links are established automatically, on demand. A link is associated with exactly two processes. Between each pair there exists exactly one link.

Asymmetric: Only sender names the receiver send (P, message) – send message to process P receive(?, message) – receive message from anyone

Asymmetric: Only receiver names the sender send (?, message) – send message to anyone (copies?) receive(Q, message) – receive message from process Q

Page 11: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.11Operating System Concepts

Indirect Communication

Messages are directed and received from mailboxes (also referred to as ports). Each mailbox has a unique id. Processes can communicate only if they share a mailbox.

Operations Create a new mailbox (Ownership? Who receives?) send(A,message) – send a message to mailbox A message = receive(A) – receive a message from A Destroy a mailbox (? If owner terminates)

Properties of communication link Link established only if processes share a common mailbox A link may be associated with many processes. Each pair of processes may share several links.

Page 12: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.12Operating System Concepts

Indirect Communication

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 Allow the system to select arbitrarily the receiver.

Sender is notified who the receiver was.

Atomicity, regardless

Page 13: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.13Operating System Concepts

Synchronization

Send and receive can be blocking or non-blocking

Blocking send waits until message is received Non-blocking send allows sender to continue

Sender is not assured of reception - must ack Sendersend(receiver,message);

receive(receiver,ack_message); Receiverreceive(sender,message);

send(sender,”ack”);

Blocking receive waits until message is available Non-blocking receive returns null if no message

Blocking send and receives forces a rendezvous

Page 14: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.14Operating System Concepts

Buffering

Queue of messages attached to the link; implemented in one of three ways.o 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 overwriteo Unbounded capacity – infinite length (right!).

Page 15: Cooperating Processes

Silberschatz, Galvin and Gagne 20024.15Operating System Concepts

Exception Conditions

Process terminates Receiver waiting for a message from terminated sender

Notify or terminate receiver Sending to a terminated receiver

Delete message Notify sender?

Lost messages Detect with timeouts - expect an ack within some time Resend (duplicates)

Scrambled messages Error checking codes