IPC

advertisement
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
Operating System Concepts
4.1
Silberschatz, Galvin and Gagne 2002
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.
Operating System Concepts
4.2
Silberschatz, Galvin and Gagne 2002
Mechanisms for IPC
Operating System Concepts
4.3
Silberschatz, Galvin and Gagne 2002
Bounded-Buffer – Shared Memory Solution
 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
 Producer
while (1) {
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
 Consumer
while (1) {
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
 Can only use BUFFER_SIZE-1 elements
Operating System Concepts
4.5
Silberschatz, Galvin and Gagne 2002
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);
}
Operating System Concepts
4.6
Silberschatz, Galvin and Gagne 2002
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?
Operating System Concepts
4.7
Silberschatz, Galvin and Gagne 2002
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)
Operating System Concepts
4.8
Silberschatz, Galvin and Gagne 2002
Symmetry of naming
Operating System Concepts
4.9
Silberschatz, Galvin and Gagne 2002
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
Operating System Concepts
4.10
Silberschatz, Galvin and Gagne 2002
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.
Operating System Concepts
4.11
Silberschatz, Galvin and Gagne 2002
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
Operating System Concepts
4.12
Silberschatz, Galvin and Gagne 2002
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
 Sender
send(receiver,message);
receive(receiver,ack_message);
 Receiver
receive(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
Operating System Concepts
4.13
Silberschatz, Galvin and Gagne 2002
Buffering
 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
 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
Operating System Concepts
4.15
Silberschatz, Galvin and Gagne 2002
Download