1,720 views
6 votes
6 votes

Consider a schedule with 3 processes A,B and C, binary semaphores are S =1 , T=0 , Z=0 initially ;

Process A
{
while(1)
{
P(S)
printf("*");
V(T)
}
}

Process B
{
P(T)
V(Z)
}

Process C
{
P(Z)
V(S)
}


What is the maximum number of times * is printed?

4 Answers

Best answer
13 votes
13 votes

Here code for $3$ processes are : 

Process A {
    while(1) { // see here infinite loop
        P(S)
        printf("*");
        V(T)
    }
}
Process B { // B executes only once
    P(T) 
    V(Z)
}
Process C { // C executes exactly once
    P(Z)
    V(S)
}

Initially, $S =1 , T=0 , Z=0$, So, only Process $A$ can execute, and $*$ is printed once. After process $A$ completes $S =0 , T=1, Z=0$. $T = 1$ implies that only $B$ can execute now. After $B$ completes its execution $S =0, T=0 , Z=1$. Now $C$ executes and makes $S = 1$ ( both $B$ and $C$ are completed now). Again Process $A$ executes $1$ more time and prints $*$ once more.

So, Total number of times $*$ is printed $= 2$

The only thing to note here is that $A$ can execute infinitely(given $S= 1$), whereas $B$ and $C$ can execute only once. (no infinite loop)

selected by
7 votes
7 votes
Star will be printed 2 times.

Since the process I is waiting on S(initialized with 1), so it will print *. After some time when process K and L are done running their code, S will gain have a value 1, process I having a while(1), it will again print *. Now there is no process which can increment semaphor S again, so process I will keep waiting on S.

Thus answer is 2.
2 votes
2 votes
Proceess B and C are not in Loop so They will run for 1 time only whereas Process A can run more than once

Maximum times print can be done is 2

Am I correct???
0 votes
0 votes
for above problem only process A having it's criticla section & B,C having just exectuion whenever A enter into CS.

so A can enter into cs infinite time  so i think correct ans will be atleast 1 time/ greater then 1 time * will be printed.

Related questions

0 votes
0 votes
1 answer
1
Mrityudoot asked Jan 27
179 views
Can a counting semaphore acquire a negative value?S = 2;15 P operations done, should the semaphore be 0 or -13
1 votes
1 votes
1 answer
2
0 votes
0 votes
1 answer
3
Na462 asked Jul 19, 2018
546 views
0 votes
0 votes
1 answer
4
Na462 asked Jul 14, 2018
691 views