Question: Topic and Suggested Work: POSIX threads. 1 ) Create a program which spawns a thread that increments a global variable counter every 1 second (

Topic and Suggested Work: POSIX threads.
1) Create a program which spawns a thread that increments a global variable counter every 1 second (you can use the sleep() function to sleep for 1 second in a loop which updates the counter once after each second; this thread can loop infinitely). In the main thread, use a command such as scanf() that asks the user for input. Make a note of the global count of seconds before the scanf() call, and afterward print the users input and a note about how many seconds elapsed while waiting for user input. Notice that the new thread continues running while the main thread waits for user input (the counter shouldnt pause while waiting).
2) After doing the above, modify the program to protect access to the global counter using a mutex. The timer thread should only need to lock access once per loop so that it is accessible most of the time. The main thread would lock and unlock for each read operation (before and after the scanf() call). Consider these questions: is this mutex really needed to protect access to this variable? Is this timer accurate?
3) Make a second thread start function (you can have as many as you want and their names can be anything) and spawn a thread that takes an argument of type double by casting the void pointer (and returns a double as well).
4) Advanced: implement a parallel algorithm such as parallel breadth-first search. You can generate a graph to search using random numbers. Another option is parallel quick sort (using example 16c as a starting point).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!