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.
Create a program which spawns a thread that increments a global variable counter every second you can use the sleep function to sleep for 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
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?
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
Advanced: implement a parallel algorithm such as parallel breadthfirst search. You can generate a graph to search using random numbers. Another option is parallel quick sort using example c as a starting point
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
