# Semaphores

• 01-18-2004
Jules
Semaphores
I am currently learning about semaphores as a means of preparing for next semester (Im going back to uni) and while answer questions in an Operating Systems book, I got stuck on a question. Briefly:

A processor concurrently runs process A & B that arrive after each other (execution time and speed not given).
Code:

``` procedure A                  procedure B begin                            begin   A;                                  F;   B;                                  G;   C;                                  H;   D;                                  I;   E;                                  J; end                                end```
I need to make sure that statement G does not commence until D has completed.

It says I have to do this by determining the number of semaphores, their type, the initial state for each and the placement in the code of the semaphore operations wait and signal

I have done the following but do not know if it is right. Semaphore S1=1 and S2=0 at start
Code:

``` procedure A                  procedure B begin                            begin   A;                                  F;   wait(S1);                        wait(S1);   B;                                  G;   C;                                  H;   signal(S1);                    signal(S1);   D;                                  I;   signal(S2);                    wait(S2);   E;                                  J; end                                end```
All comments welcome because I want to make sure I understand this properly.

Thanx
Jules
• 01-18-2004
WaltP
Re: Semaphores
Not quite. Your code will run without waiting at G. Try this:
Code:

``` procedure A        procedure B begin              begin   A;                F;   B;                wait(S1);   C;                G;   D;                H;   signal(S1);        I;   E;                J; end                  end```
Now G won't run until S1 is set after D as per your requirements
• 01-18-2004
Jules
Thanx....what about s2?? and would I be wrong by thinking that both s1 and s2 are mutex's.
• 01-18-2004
WaltP
Quote:

Originally posted by Jules
Thanx....what about s2?? and would I be wrong by thinking that both s1 and s2 are mutex's.
What about s2? You want to stop execution in one place based on one signal, what's the second on for? If they are tied together somehow, what in your post explained that?
• 01-18-2004
Jules
So sorry :o ...It says that each process executes the code in which process A - Procedure A, and process B - Procedure B. That is why I asked about s2....Also, would I be wrong by assuming that they are mutex semaphores.