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
``````

a.
Give at least four possible orders of execution for statements A to K.
b.
What is the function of each of the semaphores S1 and S2 in the given example?
c.
Is it possible for statement E to execute before statement F? Justify your answer.
d.
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.

MY attemps:
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

Man, how I miss my Operating Systems class. BEST. CLASS. EVER.

Let me rewrite this with comments:

``````void P(){
A();
//Begin critical section
wait(S1); //Grab S1
B();
signal(S1); // Release S1
//End of critical section
C();
D();
signal(S2); // Producer
E();
}

void Q(){
F();
//Begin critical section
wait(S1);
G();
H();
J();
signal(S1); // Release S1
//End of critcal section
wait(S2); //Consumer
K();
}
``````

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.