Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment: Use threads to implement Peterson\'s algorithm for mutual exclusion.

ID: 3802267 • Letter: A

Question

Assignment:

Use threads to implement Peterson's algorithm for mutual exclusion. Demonstrate that the algorithm works using two threads. One of the threads should continually print "a". The other thread should continually print "b". Each time a thread prints, it should update a character counter (which is protected using Peterson's algorithm). After 30 characters have been printed, the thread that prints next should print a new line and reset the character counter to 0. All access to the character counter should reside in a critical section protected by Peterson's algorithm. All code that does not require mutual exclusion should reside outside of critical sections.

Note:- you can code in any programming language you want.

Finally, you will write a report explaining your implementation and addressing the following question:

Does Peterson’s Algorithm properly implement Mutual Exclusion of the critical code section?

Be sure to address each of the six requirements for Mutual Exclusion.

1. Mutual exclusion must be enforced: Only one process at a time is allowed into its critical section, among all processes that have critical sections for the same resource or shared object.

2. A process that halts in its noncritical section must do so without interfering with other processes.

3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely: no deadlock or starvation.

4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay.

5. No assumptions are made about relative process speeds or number of processors.

6. A process remains inside its critical section for a finite time only

Explanation / Answer

#include #include #include #include #include void fatal_error(int err_num, char *function); void* thread0_func(void*); void* thread1_func(void*); bool threadInCritical[2]; const int SHARED_ARRAY_SIZE = 30; const int WAIT_COUNT = 1000000; int sharedArray[SHARED_ARRAY_SIZE]; int sharedIndex = 0; int turnToWinTie = 0; // Petersion's Algorithm main() { pthread_t tid0; pthread_t tid1; int return_val; int i; threadInCritical[0] = false; threadInCritical[1] = false; // Set array to 0 - SHARED_ARRAY_SIZE-1 for(i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote