cse333_ch3_B
cse333_ch3_B
Chapter 3 – Part II ! 1!
Communications Models!
Chapter 3 – Part II ! 2!
Producer-Consumer Problem!
Chapter 3 – Part II ! 3!
Bounded-Buffer – Shared-Memory Solution!
■ Shared data"
#define BUFFER_SIZE 10"
typedef struct {"
". . ."
} item;"
"
item buffer[BUFFER_SIZE];"
int in = 0;"
int out = 0;"
■ Solution is correct, but can only use
BUFFER_SIZE-1 elements"
!
Chapter 3 – Part II ! 4!
Bounded-Buffer – Producer Process!
while (true) {
/* Produce an item */
while (((in + 1) %
BUFFER_SIZE) == out)
; /* do nothing -- no
free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
{
"
Chapter 3 – Part II ! 5!
Bounded Buffer – Consumer Process!
while (true) {
while (in == out)
; // do nothing --
nothing to consume
// remove an item from the
buffer
item = buffer[out];
out = (out + 1) %
BUFFER_SIZE;
}
Chapter 3 – Part II ! 6!
Message Passing System!
■ Mechanism for processes to communicate and to
synchronize their actions"
■ Message system – processes communicate with
each other without resorting to shared variables"
■ IPC facility provides two operations:"
send(message) –message size fixed/variable "
●
● receive(message)"
■ If P and Q wish to communicate, they need to:"
● establish a communication link between them"
● exchange messages via send/receive"
■ Implementation of communication link"
● physical (e.g., shared memory, hardware bus)"
● logical (e.g., logical properties)"
Chapter 3 – Part II ! 7!
Direct Communication!
■ Processes must name each other explicitly:"
● send (P, message) – send a message to
process P"
● receive(Q, message) – receive a message
from process Q"
■ Properties of communication link"
● Links are established automatically"
● A link is associated with exactly one pair of
communicating processes"
● Between each pair there exists exactly one link"
● The link may be unidirectional, but is usually bi-
directional"
Chapter 3 – Part II ! 8!
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"
■ 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
communication links"
● Link may be unidirectional or bi-directional"
Chapter 3 – Part II ! 9!
Synchronization!
■ Message passing may be either blocking or non-
blocking"
■ Blocking is considered synchronous!
● Blocking send has the sender block until the
message is received"
● Blocking receive has the receiver block until a
message is available"
■ Non-blocking is considered asynchronous!
● Non-blocking send has the sender send the
message and continue"
● Non-blocking receive has the receiver receive a
valid message or null"
■ Sockets"
■ Remote Procedure Calls"