mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 14:54:05 -06:00
vault backup: 2022-10-30 14:40:53
This commit is contained in:
parent
555c2e1e61
commit
663a67d6cc
@ -12,8 +12,8 @@ tags:
|
||||
producer consumer problem
|
||||
|
||||
issues
|
||||
- no data in buffe: consumer has nothing to consume
|
||||
- buffer is full: no space for producer to produce
|
||||
- no data in buffe: consumer has nothing to consume: data race
|
||||
- buffer is full: no space for producer to produce: busy waiting
|
||||
|
||||
private vars
|
||||
- in: next pos in buf to be writted
|
||||
@ -24,4 +24,32 @@ shared variables
|
||||
- counter: number of items in buffer
|
||||
|
||||
waiting
|
||||
- busy"
|
||||
- busy: proces keeps checking (if buffer is still full) wasting CPU time
|
||||
- non busy: suspend the process
|
||||
|
||||
soution with shared array
|
||||
```c
|
||||
PRODUCER: repeat
|
||||
...
|
||||
produce an item in nextp
|
||||
...
|
||||
while counter = n do no-op;
|
||||
buffer[in] := nextp;
|
||||
in := in+1 mod n;
|
||||
counter := counter+1;
|
||||
until false;
|
||||
CONSUMER: repeat
|
||||
while counter = 0 do no-op;
|
||||
nextc := buffer[out];
|
||||
out := out+1 mod n;
|
||||
counter := counter-1;
|
||||
|
||||
...
|
||||
consume the item in nextc
|
||||
...
|
||||
until false;
|
||||
```
|
||||
|
||||
counter is a shared variable - causes a data race problem
|
||||
|
||||

|
||||
|
||||
Loading…
Reference in New Issue
Block a user