Instructions: In this assignment, you are required to: · Implement a program that will use mutual exclusion to avoid data races. · The program should correctly terminate when all of a set of...

write c++ code in visual studio 2019. Please save each question in their own subfolders. Ex: Question 1 in folder called Question1, if the question has multiple parts save separately as well to see which question belongs to which.


Instructions: In this assignment, you are required to: · Implement a program that will use mutual exclusion to avoid data races. · The program should correctly terminate when all of a set of concurrent tasks have completed. · Write and test programs that can reveal concurrent programming errors. · Implement your programs in C++/CLI NOTES: · Please do NOT use automatic typing! · Inappropriate use of method Thread::Sleep may avoid race conditions by giving enough time of each transfer to finish in full. This may give the impression that the threads are synchronized, but it is NOT and acceptable solution. Synchronization should work with any sleep times and without sleep times. · Do not define more than one class per source file. I suggest that you separate class definitions in .h files and class implementations in .cpp files. 1. Create a structure Coin and a class Purse. A coin has a name and monetary value (for example, name quarter, monetary value 0.25). Provide a way to check that two coins have equal names and monetary values. A purse holds a collection of coins. Make a Purse constructor to construct an empty purse and include the following methods of the class Purse: a. Add a number of instances of a coin to the purse. Parameters: the coin to add and how many of it. b. Remove a number of instances of a coin from the purse. Parameters: the coin to remove and how many of it. c. Count the number of coins in the purse that match a given coin. Parameter: the coin to match. Return value: the number of coins equal to the coin to match. d. Get the total value of the coins in the purse. Return the sum of all coin values. e. Get the total number of coins in the purse. Return the number of coins. f. Optional: Transfer a number of coins of a given type to another purse. Parameters: the coin to transfer and how many of it. [25 points] 2. Create a console application in which the main function will initialize an array of 10 purses and will start 10 threads working simultaneously with these purses without causing inconsistent states. Initially set up the purses with the same coins in each purse. The main function will wait for all threads to finish and then finish itself. Each of the threads should have its own purse, from which it will take money and place in other purses. For this purpose, it will repeatedly select at random a target purse and coins to move into the target purse. If the needed coins are not currently available, it should wait until they become available and then do the move. Since the move of coins from one purse to another involves taking the coins out of one purse and putting them in another purse, each time select at random which operation to do first, and have a small delay between the operations, to allow for race conditions. From time to time, each thread should sum up the coins of each denomination in all purses. Pay attention to the readability of the program output. If the threads are properly synchronized, the total number of coins of each denomination should be constant. [20 points] 3. Provide synchronization using monitors. Clearly identify with comments the points in the programs at which you enter and exit a monitor, so that it is easy to find these points and test the programs with and without synchronization. Test your programs with and without synchronization and note the results in your assignment report. [25 points] 4. Reason about the possibility of your synchronized programs to create deadlocks and record your reasoning in the assignment report. a. If your analysis shows the possibility of a deadlock, provide test cases that could lead to deadlocks i. Implement each deadlock test case and note the results in the report. ii. Then change the necessary settings (initial coins, maximum numbers to move, associations between threads and purses, etc. to create a program in which a deadlock cannot occur. b. If your analysis shows that deadlock is impossible, change the original settings to be able to illustrate deadlocks, record the changes in the test report, and provide test cases that can lead to a deadlock. In all cases record your reasoning, the experiments, the expected and the actual results in the test report. Provide the source code before and after the changes. [30 points] Report structure: · Check-points for tasks 1 to 3 with indication of the actual results · Reasoning about the possibility of deadlock with a description of test cases · Tests to be performed with their expected and actual results and the names of the programs that illustrate them. · Analysis of the test results · A statement indicating any known errors or unfinished tasks in the assignment
Apr 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here