P
preethamkumark
- The program first creates a shared memory buffer containing an array
of 20 integers.
- Each slot of the buffer can have either 0 or 1, where 0 represents an
empty slot, and 1
represents an occupied one.
- Initially, the buffer is empty. Thus, all the slots are initialized
with 0.
- Then the program creates a fan of 2 child processes (producer and
consumer).
- Both processes enter a while loop, and iteratively operate on the
shared buffer in a mutually
exclusive manner.
- use 3 semaphores s, n, and e;
- s: buffer lock (provides mutual exclusion for accessing the shared
buffer)
- n: current number of buffer items (forces consumer to wait when the
buffer is empty)
- e: current number of empty slots (forces producer to wait when the
buffer is full)
- Also, use the buffer index 'in' to let the producer know where to
put
the new item, and index
'out' to let the consumer know where to take an existing item.
- Producer:
- In each iteration, the producer puts a new item (integer 1) to the
buffer. The new item must be placed in the slot next to the end of
occupied items. If the
buffer is already full, then the process is blocked until the consumer
takes at least one item.
- Right after adding a new item, print out the updated contents of the
buffer to stderr When printing, it must go through a loop, printing
one
character at a time. After printing each character, call usleep(100) to
see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by other
process).
- When printing, specify who's printing (Producer or Consumer). And
the
buffer should be printed out in the following way (note that empty
slots are represented
as dots).
[Producer] 1 1 1 1 1 1 1 . . . . . . . . . 1 1 1 1
- After the printing, call usleep(pro_time) to control the speed of
buffer growing.
- Consumer:
- In each iteration, the consumer takes an existing item (integer 1)
from the buffer. The existing item must be taken from the slot at the
beginning of occupied
items. This is simulated by simply replacing 1 with 0 at the index out.
If the buffer
is already empty, then the process is blocked until the producer puts
at least one item.
- Right after removing an item, print out the updated contents of the
buffer to stderr . When printing, it must go through a
loop, printing one character at a time. After printing each character,
call usleep(100) to
see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by
other process).
- When printing, follow the convention written above (in Producer
section).
- After the printing, call usleep(con_time) to control the speed of
buffer shrinking.
- Note that since we use a bounded buffer system, the buffer must be
implemented as a circular
buffer (the end of the buffer is connected to the start of the buffer).
- Initially, make con_time three times as long as pro_time, so that the
buffer can be quickly filled (for example, pro_time = 100000, con_time
= 300000). Once it is
filled, switch their speed so that now the consumer can quickly empty
the buffer (then
switch their speed again so that the buffer quickly grows). Repeat this
fill-empty cycle twice, and
then terminate both child processes (when the buffer is empty for the
third time). The parent
process must wait for the termination of both child processes, and then
terminate itself.
- An example printing result could be as follows:
[Parent] Starting...
....
[Producer] 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 . . . . . . . . . 1 1 1 1 1 1 1
....
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1 1
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1
....
[Consumer] Two cycles completed. Exiting...
[Producer] Two cycles completed. Exiting...
[Parent] Exiting...
of 20 integers.
- Each slot of the buffer can have either 0 or 1, where 0 represents an
empty slot, and 1
represents an occupied one.
- Initially, the buffer is empty. Thus, all the slots are initialized
with 0.
- Then the program creates a fan of 2 child processes (producer and
consumer).
- Both processes enter a while loop, and iteratively operate on the
shared buffer in a mutually
exclusive manner.
- use 3 semaphores s, n, and e;
- s: buffer lock (provides mutual exclusion for accessing the shared
buffer)
- n: current number of buffer items (forces consumer to wait when the
buffer is empty)
- e: current number of empty slots (forces producer to wait when the
buffer is full)
- Also, use the buffer index 'in' to let the producer know where to
put
the new item, and index
'out' to let the consumer know where to take an existing item.
- Producer:
- In each iteration, the producer puts a new item (integer 1) to the
buffer. The new item must be placed in the slot next to the end of
occupied items. If the
buffer is already full, then the process is blocked until the consumer
takes at least one item.
- Right after adding a new item, print out the updated contents of the
buffer to stderr When printing, it must go through a loop, printing
one
character at a time. After printing each character, call usleep(100) to
see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by other
process).
- When printing, specify who's printing (Producer or Consumer). And
the
buffer should be printed out in the following way (note that empty
slots are represented
as dots).
[Producer] 1 1 1 1 1 1 1 . . . . . . . . . 1 1 1 1
- After the printing, call usleep(pro_time) to control the speed of
buffer growing.
- Consumer:
- In each iteration, the consumer takes an existing item (integer 1)
from the buffer. The existing item must be taken from the slot at the
beginning of occupied
items. This is simulated by simply replacing 1 with 0 at the index out.
If the buffer
is already empty, then the process is blocked until the producer puts
at least one item.
- Right after removing an item, print out the updated contents of the
buffer to stderr . When printing, it must go through a
loop, printing one character at a time. After printing each character,
call usleep(100) to
see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by
other process).
- When printing, follow the convention written above (in Producer
section).
- After the printing, call usleep(con_time) to control the speed of
buffer shrinking.
- Note that since we use a bounded buffer system, the buffer must be
implemented as a circular
buffer (the end of the buffer is connected to the start of the buffer).
- Initially, make con_time three times as long as pro_time, so that the
buffer can be quickly filled (for example, pro_time = 100000, con_time
= 300000). Once it is
filled, switch their speed so that now the consumer can quickly empty
the buffer (then
switch their speed again so that the buffer quickly grows). Repeat this
fill-empty cycle twice, and
then terminate both child processes (when the buffer is empty for the
third time). The parent
process must wait for the termination of both child processes, and then
terminate itself.
- An example printing result could be as follows:
[Parent] Starting...
....
[Producer] 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 . . . . . . . . . 1 1 1 1 1 1 1
....
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1 1
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1
....
[Consumer] Two cycles completed. Exiting...
[Producer] Two cycles completed. Exiting...
[Parent] Exiting...