I wanted to know the output of this program, step-by-step, please initially, values are: S1 = 0 , S2 = 1, S3 = 0, a = 1. Center code here
p1 p2 p3 while (1) {while (1) {while (1) {p1} (S2); P (S3); One = 2 * one; A = a + 1; P (S3); V (S3)} V (S1); Printf ("% d \ n", one); V (S3)} V (S2); The solution that you have linked is correct in informal words: P block while Sqvr = = 0 or succeeds and continues; V increases the semaphore. In the first S1 and S3 == 0, therefore P1 and P3 are blocked.
P2 is the only one that can grow This one (== 2) increases and increments S1 and S3 can not continue because S2 is now 0.
P3 can do only one step, because after the first P call, S3 == 0 again.
P1 is the only process that works. It doubles (== 4), then makes helplessness to the S3. This can not be released because S2 is 0.
Now P3 is the only one who can walk. It prints 4 then unblock P2 Note that the problem is exactly like the beginning (except now == 4). Therefore each cycle runs exactly the same way: p2 increments A, P1 a pair, P3 prints a. Repeat.
Therefore printed value 4, 10, 22, 46 ...
No comments:
Post a Comment