# Algorithms Quiz | SP Contest 1 | Question 10

• Difficulty Level : Easy
• Last Updated : 06 Sep, 2021

Here are the two concurrent process A, B with respective codes:

Code A:

```while (true) // infinite condition
{
M :____;
printf("%c", b);
printf("%c", b);
N:____;
}
```

Code B:

```while (true) // infinite condition
{
O:____;
printf("%c", a);
printf("%c", a);
P:____;
}
```

What should be the binary semaphore operation on M, N, O, P respectively and what must be the initial values of semaphore X, Y in order to get the output bbaabbaabbaa . . . ?
Where P is down and V is up operation respectively.

(A) M = P(Y), N = V(X), O = P(X), P = V(Y); X = 0, Y = 1;
(B) M = P(Y), N = V(X), O = P(X), P = P(Y); X = Y = 1;
(C) M = P(Y), N = V(Y), O = P(X), P = V(X); X = 1, Y = 0;
(D) M = P(Y), N = V(Y), O = P(X), P = V(X); X = Y = 1;

Explanation: In semaphore up is always a successful operation but down is not always successful.
In following concurrent process Operations are:
A: code

```while (true) // infinite condition
{
M :P(Y); // Y become 0 successful down operation.
printf("%c", b);
printf("%c", b);
N:V(X); // X become 1 successful up operation.
}
```

B code:

```while (true) // infinite condition
{
O:P(X); // X  become 0 successful down operation.
printf("%c", a);
printf("%c", a);
P:V(Y); // Ybecome 1 successful up operation.
}```

Here all operation are successful with initial values of X and Y are 0 and 1 respectively.
So, option (A) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}