#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Sem_wait() problem


    I am using semaphores for 3 processes, P1, P2, P3
    that is I can have multiple processes runnning at the same time just that
    P1 and P3 cannot occur at the same time with P2.

    Basically I use 2 sema, S1 for P1 and P2; S2 for P3 and P2

    but in P2 how do i check for P1 and P3??

    is

    sem_wait(S1);
    sem_wait(S2);

    valid?

    (I know it's not, cause if P3 is on, my P2 will block the entrance of P1 while waiting for P3 at S2 which I don't wan, is there any other ways?)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    But if all three processes always do
    sem_wait(S1);
    sem_wait(S2);

    What is the point of having S2 at all?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    But if all three processes always do
    sem_wait(S1);
    sem_wait(S2);

    What is the point of having S2 at all?
    I am sorry, perhaps my question is not clear enough.
    I want to have 3 processes running P1, P2 and P3 but
    P2 cannot run concurrently with P1 and P3.

    So my solution is using 2 sema, S1 and S2:

    In P1:
    sem_wait(S1);
    //code;

    In P3:
    sem_wait(S2);
    //code;

    In P2:
    sem_wait(S1);
    sem_wait(S2);
    //code

    I was asking for an alternative solution to this problem, cause
    when I don't want my P2 to block the entrance of P1 when P2 is waiting for S2 as P1 and P3 can occur concurrently.

    As in P2 can only occur when both P1 and P3 are not occuring
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    http://linux.die.net/man/3/sem_init
    The first thing to do is initialise the semaphore with the value 2.
    This will permit P1 and P3 to lock and unlock at will.

    P2 on the other hand must tread a bit more carefully, since it needs to lock twice.
    So it could either do
    Code:
    sem_wait(&sem);
    sem_wait(&sem);
    or perhaps
    Code:
    do {
        int s;
        sem_wait(&sem);
        if ( (s=sem_trywait(&sem)) == -1 && errno == EAGAIN ) {  // see also sem_timedwait
            sem_post(&sem);  // didn't get it a 2nd time, back off
            continue;
        } else if ( s == 0 ) {
            // success - break out
            break;
        } else {
            // something wrong - deal with it
        }
    } while ( 1 );
    If P2 is low priority compared to P1/P3, then backing off if it can't get the semaphore twice in quick succession seems like a good idea.

    Don't forget to post() twice, when you're done ;)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo