Thread: Semaphores

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    32

    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

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    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
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    Thanx....what about s2?? and would I be wrong by thinking that both s1 and s2 are mutex's.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    So sorry ...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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 01:54 AM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 12:45 PM
  3. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  4. semaphores
    By theLukerBoy in forum C Programming
    Replies: 0
    Last Post: 11-05-2002, 01:46 AM
  5. Semaphores
    By edk in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2001, 03:55 PM