Question: Problem 1: Write a multithreaded program that works on a list of numbers. This program will be passed a series of numbers on the command
Problem 1:
Write a multithreaded program that works on a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads that only share the list. Use Mutex Locks to confirm correct results among running of concurrent threads.
1. One thread will determine the median of the numbers. 2. Second one will determine the mean of the numbers. 3. Third one will determine the prime numbers in the list.
For example, suppose your program is passed the integers: 44, 50, 38, 96, 42, 47, 40, 39, 46, 50 The program will report:
The median value is 45 | The mean value is 49.2 | The prime numbers is 47
Problem 2:
Sara, Mohammed, and Abdullah make coffee perfectly. Sara grinds coffee and put them in capsules. Mohammed then places the coffee capsule in a coffee machine. Abdullah then serves the coffee that is done by the coffee machine. There are several synchronization constraints:
-
Mohammed cannot make a coffee unless at least one coffee capsule exists, but Mohammed does not care how far Sara gets ahead of him.
-
Abdullah cannot serve a coffee unless at least one capsule exists in which Mohammed has used in a machine to make a coffee, and the coffee has not yet been served. Abdullah does not care how far Mohammed gets ahead of him.
-
Mohammed does care that Sara does not get more than MAX capsules ahead of him. Thus, if there are MAX coffee capsules, Sara has to wait.
-
There is only one coffee machine with which both Sara and Mohammed need to prepare and use the coffee capsules, respectively.
Write a multithreaded program with three threads which represent Sara, Mohammed and Abdullah using semaphores as the synchronization mechanism.
Hint : No need to provide detailed implementation regarding operations bellow: prepare_capsule(); // Saras operation make_coffee(); // Muhammads operation serve_coffee(); // Abdullahs operation
You can implement them as printf() statements.
*make sure that the code must be in C language
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
