Question: This programming question is about semaphore Please create two threads t1 and t2. tl and t2 share an integer data counter. The job oftl is

This programming question is about semaphore Please create two threads t1 and t2. tl and t2 share an integer data counter. The job oftl is to increase the value of counter by 1 when tl is scheduled. The job of t2 is to decrease the value of counter by 1 when t2 is scheduled Because counter is a critical section, you need to use semaphore to implement mutual exclusion between tl and t2 Here is the API you may use sem init: initialize an unnamed semaphore sem wait: lock a semaphore, which is equivalent to the P operation sem post: unlock a semaphore, which is equivalent to the V operation sem destroy: destroy an unnamed semaphore Here is the basic structure of the program int counter; /Mshared between t1 and t2 main) Create semaphore, Create ti, Create t2, Destroy semaphore, thread tl routine0 while (1) PO, Increase counter by 1; Output the value of counter; thread t2 routine while (1) Decrease counter by 1; Output the value of counter; VO
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
