Description
Problem Statement: Read through Project 3 (page 253-255) in the textbook, the Producer–Consumer Problem, and finish it using Pthreads only (ignore the Windows API part). Your project must meet the following requirements for full credit: 1. Instead of generating random numbers and write to the buffer, producer threads will write incremental integers into the buffer. For example, if the initial value of the number is 10, then the next number should be 11, and the next will be 12…. 2. Create producer and consumer threads with 3 scenarios and show your results in screenshots similar as the sample screenshots shown below. The three scenarios are: 1) more producers than consumers; 2) more consumers than producers; 3) same number of producers and consumers. Please refer to the screenshots shown below for sample outputs under different scenarios. (Your outputs may be different) 3. The main () function will be passed four parameters on the command line. The four parameters are: 1. How long will the main thread sleep before terminating 2. The number of producer threads 3. The number of consumer threads 4. The initial value of the number (for producers) Your program should be executed like this: ./buffer . Here buffer is your executable file which is followed by four parameters, sleep time, the number of producer threads, the number of consumer threads, and the initial number. Eg. ./buffer 10 2 2 10 Some Notes: 1. To help you to get started, a skeleton of the buffer.c is provided in iCollege. 2. To get a thread’s ID, you may call the pthread_self() function and convert the result into an integer. Below is a sample showing how a thread can get and print its ID: printf(“Consumer %u consumed %d \n”,(unsigned int)pthread_self(), random); 3. Using gcc –pthread –o buffer buffer.c to compile, and then an executable file buffer would be generated. (Suppose buffer.c is your code file) 4. In your report, you need to include screenshots of the three scenarios (see sample screenshots below). 5. Work individually. Programs should exhibit a modular or object-oriented design. Poor design will not earn full credit. What to submit (submit trough iCollege) 1) Upload a source code file of buffer.c and a project report (named as “HW3.pdf” or “HW3.doc”) to iCollege. The project report should include: 1) the source code; 2) a screenshot of the output. 2) Submit your files separately; do NOT submit a zip file. 2 Sample screenshots: Scenario 1: number of producers is less than number of consumers 0’s are the return values from functions that initialize the synchronization tools: mutex, empty and full semaphores These are thread ids 3 Scenario 2: Number of producers is equal to number of consumers 4 Scenario 3: Number of producers is larger than number of consumers