1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

semaphore question

Discussion in 'C' started by oomn123, Nov 3, 2010.

  1. oomn123

    oomn123 New Member

    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.
     
  2. xpi0t0s

    xpi0t0s Mentor

    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).
     
    shabbir likes this.
  3. xpi0t0s

    xpi0t0s Mentor

    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).
     
  4. oomn123

    oomn123 New Member

    Thank You man
     
  5. oomn123

    oomn123 New Member

    sorry u for uneasy to read
    my first post
     
  6. xpi0t0s

    xpi0t0s Mentor

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

    jipsi01 New Member

    Dear xpi0t0s
    Thank you for your sharing your information, it is working, great your post...
     

Share This Page