Homework5_2018529627050 Version 0 |
|
👤 Author: by wx287_oz26ft1wt2_isbggtha4lgepaasa 2020-11-03 06:01:23 |
Implementing a simple producer/consumer problem:
Use a single shared buffer
Only one process (producer or consumer) can be reading or writing the buffer at one time
Producers put things in the buffer with a deposit
Consumers take things out of the buffer with a fetch
Need two semaphores:
empty will keep track of whether the buffer is empty
full will keep track of whether the buffer is full
typeT buf; /* a buffer of some type */
sem empty =1; /*initially buffer is empty */
sem full = 0; /*initially buffer is not full */
process Producer [i = 1 to m] {
while (true) {
...
/*produce data, then depositi it in the buffer */
P(empty);
buf = data;
V(full);
}
}
process Consumer [j=1 to n] {
while (true) {
P(full);
result = buf;
V(empty);
...
}
}