Hey guys I'm going through some questions, studying for exams and below is one which has had me stressing all afternoon.
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
Give at least four possible orders of execution for statements A to K.
What is the function of each of the semaphores S1 and S2 in the given example?
Is it possible for statement E to execute before statement F? Justify your answer.
Is it possible for statement K to execute before statement A? Justify your answer.
The notes I have are rather vague about comparing two semaphore operations (like in this case)
I have attempted the questions although I assure you my answers will likely be incorrect and in need of better explanations. What I am looking for is not answers but perhaps responses detailing if I am right or wrong and where I was wrong for B C and D. For part A i wish to know the method of how I should approach such a question and the justifications.
A) A (signal change)F (signal change)B (signal change)G H J (signal change)C D (signal change) K (signal change)E
this is beginning from A with changes due to semaphore signalling.
Other possible orders I have in mind are:
A B (signal change)F (signal change)CD (signal change)G H J K (signal change)E
I feel I am not correctly analysing the semaphore operations (and admittingly they do confuse me)
B)some kind of producer consumer question seeing as P1 needs to wait for P2 or P1 is a process providing signals and such for P2 to begin.(basically P1 supporting P2)
C)unsure but I'd say no due to the wait command after A
d)no due to S1 inistialised at 1 (going first since its up)
any help is greatly appreciated