Implement the counting semaphores using binary semaphores. Pseudo-code for the implementation is given below. In your main(), spawn three threads; in each thread function, you will need a call to wait(counting_semaphore) and signal(counting_semaphore). For the critical section, you can just have sleep() to make the thread wait for some time. The incorrect implementation output from the attached notes will help you in writing the code. Find output for BOTH the incorrect and correct implementations. Also, attach your codes (two versions: for incorrect and correct solutions) 2 Implementation 1 (incorrect) CSentK) escount ing sen, init K int val K: //value of cset BSem waito): to block on csem BSem nutext1): / protects val Peics) C Evolation showing error nitial: if val 0: 0 1f val0 BSee nutex(1) If protects va Pctcs)C P(gate) al: Knutex); vval-1 if val > 0 Vgate): Pnute): val+1: f val VCgate):
Explanation / Answer
int s = N; \ counting semaphore initialized to N semaphore mutex_s = 1; semaphore delay_s = 0; p(s) = down down(mutex_x); s--; if (s< 0) up(mutex_s) down(delay_s) up(mutex_s) V(s) = up down(mutex_s) s++ if (s