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

sem t sl, s2 pthread t fostid, bar tid void *foo ( void *) ( while (1) printf (\

ID: 3696092 • Letter: S

Question

sem t sl, s2 pthread t fostid, bar tid void *foo ( void *) ( while (1) printf ("HI ") sem post( &s2; void *bar void * ( while (1) printf ("HO ") sem post( &sl; int main) sem Anit( «sl, 0, valuel sem init( &s2;, 0, value2 pthread create(foo tid, NULL, foo, NULL pthread_create( ax_cid, NULL, bar, NULL ) ; return 0; Describe the output of the above C/Pthreads program (in its most general form) if the semaphores sl and s2 are given the following initial values: a. value! = 0 and value2 = 0

Explanation / Answer

In the above code,

sem_wait = decrease semaphore operation

sem_post = increase semaphore operation

A) in the first case s1=0, and s2=0

so it cant enter into any of the function blocks.

so nothing will be printed.

B)

s1 val=1 s2 val=0

Here only 1st function block can be entered.

and after the end of first block s2 val becomes 1 and second block can be read and again the iteration continues.

Output= HIHO HIHO HIHO....

C)

here s1=0 and s2=1 so only second block can be executed first and then 1st block and soo on

Output= HOHI HOHI HOHI....