Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   semaphore question (http://www.go4expert.com/forums/semaphore-question-t23763/)

 oomn123 4Nov2010 05:28

semaphore question

Can anyone help me with this question i try but was not sure

Consider a uniprocessor system executing concurrently two processes P and Q. Each process executes the code listed below, process P – procedure P, and process Q – procedure Q. Both processes arrive within a very short time of each other, but no assumptions can be made about the time they start execution and their relative speed. All statements used in the code below from A to K are atomic ie. they either execute completely or not at all. The execution of the processes is synchronised by two binary semaphores S1 and S2. The semaphore S1 is initialised to 1, and the semaphore S2 is initialised to 0. The code executed by the processes is as follows:
procedure P begin A; wait(S1); B; signal(S1); C; D; signal(S2); E; end procedure Q begin F; wait(S1); G; H; J; signal(S1); wait(S2); K; end a.Give at least four possible orders of execution for statements A to K.
A,C,D,E,K
A,D,C,E,K
F,C,D,E,K
F,D,C,E,K
b. What is the function of each of the semaphores S1 and S2 in the given example?
S1 – used for waiting c. Is it possible for statement E to execute before statement F? Justify your answer.
Yes, (but i am not so sure, can someone confirm?)
d. Is it possible for statement K to execute before statement A? Justify your answer.

 xpi0t0s 4Nov2010 13:02

Re: semaphore question

Formatting code makes it a lot easier to read.
Code:

```S1=1; S2=0; procedure P begin   A;   wait(S1);   B;   signal(S1);   C;   D;   signal(S2);   E; end procedure Q begin   F;   wait(S1);   G;   H;   J;   signal(S1);   wait(S2);   K; end```
a. ABCDEFGHJK is one. None of the four you gave are valid.
For example ACDEK - what happened to B,F,G,H and J? There are
no conditionals; P executes all of A-E and Q all of F-K.

b. I'll let you answer this one.

c. Yes, because there's nothing in P that waits for anything specific in Q. P waits for S1 but S1 is already 1 so P can execute in full before Q starts.

d. No, because wait(S2) will wait until P calls signal(S2).

 xpi0t0s 4Nov2010 13:08

Re: semaphore question

Four possible scenarios for answering the first question could be
(1) P runs until it hits a wait(0), then Q starts;
(2) Q runs until it hits a wait(0), then P starts;
(3) P and Q start at the same time but P goes first and each procedure executes one line before the CPU switches between them (so if you can decode my shorthand you get something like PA, QF, Pwait, Qwait, PB, Qwaiting, Psig, QG, PC etc);
(4) As 3 but Q goes first (QF, PA, Qwait, Pwait, QG, Pwaiting, QH, Pwaiting etc).

 oomn123 4Nov2010 13:14

Re: semaphore question

Thank You man

 oomn123 4Nov2010 13:16

Re: semaphore question

sorry u for uneasy to read
my first post

 xpi0t0s 4Nov2010 13:40

Re: semaphore question

No prob. Make sure you read the posting guidelines and the FAQs, you should *always* do that when you go to a new forum.

 jipsi01 9Jul2012 12:37

Re: semaphore question

Dear xpi0t0s