In the programming question of Homework 2, there are two versions of the shared memory producer-
consumer program (either leave one slot of the buffer empty to indicate an empty buffer or using all slots for storing data. Write two pthread programs, each for one of the versions. In your code, consider “item” as a ‘char’ type, and the main function creates two threads (producer and consumer). The producer read the text shown at this URL: “
https://www.usenix.org/sites/default/files/usenix2…
”, and write them, character by character, to the buffer (you may first download the file and read from the local file). And the consumer reads from the buffer and print them to the screen, character by character. Your code’s logic should match that of the homework code. Observation: For each of the buffer sizes (2, 3, 5, 10, 15, 20, 25, and 30), measure number of trips for the producer to deliver characters to the buffer in each version of the programs. A trip is defined as one or multiple continuous writes to the buffer without being interrupted by a busy waiting (run into the “do nothing” code line due the a full buffer). Insert code into your programs to collect the statistics and report them in a table. Problem 1: [50 points for code + 10 points for observations]
In the programming question of Homework 2, there are two versions of the shared memory producer-
consumer program (either leave one slot of the buffer empty to indicate an empty buffer or using all slots for
storing data. Write two pthread programs, each for one of the versions. In your code, consider “item” as a
‘char’ type, and the main function creates two threads (producer and consumer). The producer read the text
shown at this URL: “https://www.usenix.org/sites/default/files/usenix2019 v3.1.tex”, and write them, character
by character, to the buffer (you may first download the file and read from the local file). And the consumer
reads from the buffer and print them to the screen, character by character. Your code’s logic should match
that of the homework code.
Observation: For each of the buffer sizes (2, 3, 5, 10, 15, 20, 25, and 30), measure number of trips for the
producer to deliver characters to the buffer in each version of the programs. A trip is defined as one or
multiple continuous writes to the buffer without being interrupted by a busy waiting (run into the “do
nothing” code line due the a full buffer). Insert code into your programs to collect the statistics and report
them in a table.
item next-produced;
while (true) {
#define BUFFER_SIZE 10
/* produce an item in next-produced */
typedef struct {
} item;
item buffer [BUFFER_SIZE];
int in = 0;
int out
0%;
item next consumed;
while (true) {
while (in == out)
; /* do nothing */
next consumed = buffer [out];B
out (out + 1) % BUFFER_SIZE;
while (((in + 1) % BUFFER_SIZE) ==
; /* do nothing */
out)
next_produced;
buffer [in]
in (in + 1) % BUFFER_SIZE;
Figure 3.12 The producer process using shared memory.
/* consume the item in next consumed */
}
Figure 3.13 The consumer process using shared memory.