Tightly Coupled
Communicating System
Producer |
Shared |
Consumer |
interpretation: flag=0 means buffer is empty therefore OK to proceed and produce new value flag=1 means buffer is full therefore must wait until contents have been used |
flag = number of valid data items remaining in buffer |
flag=0 means buffer is empty therefore must wait for new contents flag=1 means buffer is full therefore OK to proceed and use contents |
while (1) { ... int ok=0; while (!ok) { P(s); if (flag==0) ok=1; V(s); if (!ok) sleep(1); } ... generate new value; ... P(s); copy new value into buffer; flag=1; V(s); ... } |
buffer: data area flag: int s: semaphore |
while (1) { ... int ok=0; while (!ok) { P(s); if (flag==1) ok=1; V(s); if (!ok) sleep(1); } ... P(s); copy new value from buffer; flag=0; V(s); ... make use of new value; ... } |
Loosely Coupled
Communicating System
Producer |
Shared |
Consumer |
|
QueueFront == NULL means there is no data ready for use at the moment. |
|
while (1) { link *item=NULL; ... generate new value; ... item=new link(); item->data=the new value; item->next=NULL; P(s); if (QueueFront==NULL) QueueFront=item; else QueueBack->next=item; QueueBack=item; V(s); ... } |
struct link { data: whatever; next: ptr to link; }; QueueFront, QueueBack: ptr to
link; s: semaphore |
while (1) { link *item=NULL; P(s); if (QueueFront!=NULL) { item=QueueFront; QueueFront=QueueFront->next; } V(s); ... if (item==NULL) sleep(1); else { new value = item->data; make use of new value; ... } |