Shared Memory#defineBUFFERSIZE10item next_produced;typedef struct while (true)(/* produce an item innext produced */}item;while((in+1)%BUFFER_SIZE)==out)item buffer[BUFFER_SIZE];;/*donothing */int in = O;int out = 0;buffer[in] = next_produced;in=(in +1)%BUFFER_SIZE;人ProducerShared memory by producer&consumerprocessesitem next consumed;Consumerwhile (true){while (in == out)1;/* do nothing */next_consumed =buffer[out];out (consumer)in (producer)Out=(out +1)%BUFFER_SIZE;OnlyalloWsBUFFERSIZE-1/*consume theitem *items at the same time. Why?子11
11 Shared Memory item next_produced; while (true) { /* produce an item in next produced */ while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; } item next_consumed; while (true) { while (in == out) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; /* consume the item */ } #define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Shared memory by producer & consumer processes Producer Consumer out (consumer) in (producer) . Only allows BUFFER_SIZE-1 items at the same time. Why?
MessagePassing Communicating processes may reside on differentcomputers connected by a networkIPC facility provides two operations:- send(message) + receive(message) If processes P and Q wish to communicate-Establishacommunicationlinkbetweenthem- Exchange messages via send/receivePMessagepassing12
12 Message Passing • Communicating processes may reside on different computers connected by a network • IPC facility provides two operations: – send(message) + receive(message) • If processes P and Q wish to communicate – Establish a communication link between them – Exchange messages via send/receive P Q Message passing
MessagePassing(Cont.) Implementation issues (logical):- Naming: Direct/indirect communication-Synchronization:Synchronous/asynchronous- Buffering13
13 Message Passing (Cont.) • Implementation issues (logical): – Naming: Direct/indirect communication – Synchronization: Synchronous/asynchronous – Buffering
Naming How to refer to each other?Direct communication: explicitly name each other-Operations (symmetry)· send (Q,message)-senda message toprocessQ·receive(P, message)-receivea messagefromprocessP-Properties of communication link: Links are established automatically (every pair can establish)·A link is associated with exactlyone pairof processes: Between each pair, there exists exactly one link- Disadvantage: limited modularity (hard-coding)14
14 Naming • How to refer to each other? • Direct communication: explicitly name each other – Operations (symmetry) • send (Q, message) – send a message to process Q • receive(P, message) – receive a message from process P – Properties of communication link • Links are established automatically (every pair can establish) • A link is associated with exactly one pair of processes • Between each pair, there exists exactly one link – Disadvantage: limited modularity (hard-coding)
Naming How to refer to each other? Indirect communication: sent to and received frommailboxes (ports)-Operations: send (A, message)- send a message to mailbox A: receive(A, message) - receive a message from mailbox A-Properties of communication link.A linkisestablishedbetween a pairof processesonlyif bothmembers have a shared mailbox·A linkmaybeassociatedwithmorethantwoprocesses·Betweeneach pair,a number of different links may exist15
15 Naming • How to refer to each other? • Indirect communication: sent to and received from mailboxes (ports) – Operations • send (A, message) – send a message to mailbox A • receive(A, message) – receive a message from mailbox A – Properties of communication link • A link is established between a pair of processes only if both members have a shared mailbox • A link may be associated with more than two processes • Between each pair, a number of different links may exist