Question: The preferred language for this assignment is C. Objective: In this assignment, you will be writing a multithreaded program that shares data between threads. You
The preferred language for this assignment is C.
Objective:
In this assignment, you will be writing a multithreaded program that shares data between threads. You will need to properly protect data that is shared between threads. You will be using mutex or semaphore locks around critical sections to ensure you program works correctly and does not experience race conditions.
Details:
The previous program is limited to a maximum of 99 numbers, and we need to fix that problem. You will write a program similar to that of Assignment 3 - Multithreaded Program that will use two threads and a limited Producer-Consumer buffer to move input numbers from the main thread to the calculate thread. Your program will not know the quantity of integers that will need to be summed. It could be three, it could be three million. If you write the Producer-Consumer code correctly, the count of numbers will not matter.
The Producer-Consumer buffer will have a maximum size of 20 integers. The buffer must be shared between the main thread and the calculate thread. Your program must protect the buffer with synchronization primatives specific to the buffer. You are allowed to use one of the following:
Mutex - pthread_mutex_t
Only one mutex per buffer is necessary to accomplish this task.
You will need to use a polling loop to properly wait for the synchronized data
Polling loops are dreadfully inefficient, thus I suggest not using this method
Monitor - pthread_cond_t
You will likely use one mutex and two condition variables per buffer for this solution
There is still a polling loop, but it is more efficiently used
Not a simple solution, but allowed
Semaphores - sem_t
Two semaphores per buffer are usually needed for this solution
Does not require a polling loop.
Lack of polling loop makes this the preferred solution
The Producer-Consumer buffer and any locking primitives are the only global variables allowed for this assignment. This assignment can be completed with no global variables used, and I will give two extra credit points if your assignment does not use global variables at all.
Before the main thread may read any numbers from stdin or from the filename specified on the command line, the main thread should start the compute thread. Once the thread is started, the main thread will read three numbers at a time. The main thread will then add these numbers to the buffer as long as the buffer has available free entries. Free entries in the buffer must be detected for each number added to the buffer.
The main thread is *not* allowed to lock the buffer, add three numbers, then unlock the buffer.
The main thread must lock the buffer, add one number, then unlock the buffer, three times.
You are allowed to put all three numbers into a structure and make the buffer an array of pointers to the structure. This would actually be the preferred solution.
After all the numbers are read in by the main thread, the thread must somehow notify the compute thread that all the numbers have been provided. There are many ways for this to be accomplished. One way is to send a count value before sending the group of numbers to be calculated. For example, if the numbers "1 2 3" are read by the main thread, then the values "3 1 2 3" are sent by the main thread to the compute thread. The first three indicates there are three numbers to be calculated. The one, two, and three are the values to use in the calculation. This would happen for all groups until no more numbers are available. When the main thread has finished reading numbers, it should send the number "0" to the compute thread to indicate there are no more numbers to calculate. An example of the data sent from the main thread to the compute thread would have for the main thread an input of "10 11 12 13 14 15" and an output to the compute thread of "3 10 11 12 3 13 14 15 0". When the end value is sent, the main thread will wait for the compute thread to complete the work. Then the main thread will exit the program. The main thread will only print the startup header. The main thread will not print any numeric values.
The compute thread will loop, multiplying groups of three numbers. The compute thread will print the results before looping again. When the compute thread received notification from the main thread that there is no more data forthcoming, the compute thread should terminate. When the compute thread has completed printing the results to the console, the thread should then display the number of results printed.
Output:
You should now have the number of results printed as the last line of your output.
# ./assn4 numbers.txt The product of 2, 3, and 4 is 24 The product of 12, 19, and 17 is 3876 The product of 21, -3, and -9 is 567 The product of 103, 31, and 211 is 673723 The product of 89, 0, and 1023 is 0 Total products calculated: 5
# echo 2 3 5 7 11 13 17 19 23 | ./assn4 The product of 2, 3, and 5 is 30 The product of 7, 11, and 13 is 1001 The product of 17, 19, and 23 is 7429 Total products calculated: 3
If you really want to put your assignment through the wringer, try sending it three million numbers like this:
# python -c 'print(" 2 3 ".join(map(str, range(1000000))) + " 2 3")' | ./assn4 | tail The product of 999991, 2, and 3 is 5999946 The product of 999992, 2, and 3 is 5999952 The product of 999993, 2, and 3 is 5999958 The product of 999994, 2, and 3 is 5999964 The product of 999995, 2, and 3 is 5999970 The product of 999996, 2, and 3 is 5999976 The product of 999997, 2, and 3 is 5999982 The product of 999998, 2, and 3 is 5999988 The product of 999999, 2, and 3 is 5999994 Total products calculated: 1000000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
