Task 1: The Buffer (or Buffer.java)
Internally, the buffer will consist of a fixed-size array of type buffer_item (which will be defined using a typedef). The array of buffer_item objects will be manipulated as a circular queue. The definition of buffer_item, along with the size of the buffer, can be stored in a Java class file or C header file such as the following:
Example snippet of buffer.h or constant.java file
/* buffer.h */ // Java: constant.java typedef int buffer_item; // Java: omit it in Java or use // wrapper class
#define BUFFER_SIZE 5; // Java: public static final // int BUFFER_SIZE = 5
The buffer will be manipulated with two functions, insert_item() and remove_item(), which are called by the producer and consumer threads, respectively. A skeleton outlining these functions appears as:
Example snippet of buffer.c or buffer.java file
#include <buffer.h> //Java: import
/* the buffer */
buffer_item buffer[BUFFER_SIZE];
int insert_item(buffer_item item) {
/* insert item into buffer return 0 if successful, otherwise
return -1 indicating an error condition */
}
int remove_item(buffer_item *item) {
/* remove an object from buffer return 0 if successful, otherwise
return -1 indicating an error condition */
}
The insert_item() and remove_item() functions will synchronize the producer and consumer using the algorithms outlined in the lecture slides. The buffer will also require an initialization code section (which is part of the main() function) that initializes the empty, full, and mutex semaphores.
Task 2: The main() Function (or Main.java)
The main() function will initialize the buffer and create the separate producer and consumer threads. Once it has created the producer and consumer threads, the main () function will sleep for a period of time and, upon awakening, will terminate the application. The main () function will be passed three parameters on the command line:
1.How long to sleep before terminating
2.The number of producer threads
3.The number of consumer threads A skeleton for this function appears as:
Example snippet of buffer.c or Main.java file
#include <buffer.h>
int main(int argc, char *argv[]) { //Java: public static void // main(String[] args)
/* 1. Get command line arguments argv[1], argv[2], argv[3]*/
/* 2. Initialize buffer */
/* 3. Create producer threads */
/* 4. Create consumer threads */
/* 5. Sleep */
/* 6. Exit */
Task 3: Producer and Consumer Threads (or Producer.java and Consumer.java)
The producer thread will alternate between sleeping for a random period of time (1ms to maximum 1000 ms) and inserting a random integer into the buffer. Random numbers will be produced using the rand()function, which produces random integers between 0 and RAND_MAX (Java: Math.random()). The consumer will also sleep for a random period of time (1ms to maximum 1000 ms) and, upon awakening, will attempt to remove an item from the buffer.
Please print the following for both consumer and producer threads