Help Needed : Race condition while removing semaphore(sysvsem semaphore)

Discussion in 'C Programming' started by techi_C, Aug 10, 2006.

  1. techi_C

    techi_C Guest

    Hi
    I'm getting a problem while removing semaphore from system.

    Before removing semaphore I'm checking the usage count of a smaphore.

    // checking usage count
    usage_count = semctl(sem_ptr->semid, 1, GETVAL, NULL);
    if( usage_count > 1 )
    // return don't remove semaphore
    else
    // remove semaphore

    If usage count is more than 1 then I'm not removing it.It means that at
    this time there are more than one threads waiting for resource.

    The problem is coming when after checking the usage_count = 1 if
    usage_count becomes 2.
    then problem is coming.This operation is not atomic.the usage_count
    should not be 2 while removing semaphore.

    Please anybody can tell me that How to avoid this problem.

    Regards,
    Pawan
     
    techi_C, Aug 10, 2006
    #1
    1. Advertising

  2. techi_C

    Guest

    techi_C wrote:
    > Hi
    > I'm getting a problem while removing semaphore from system.
    >
    > Before removing semaphore I'm checking the usage count of a smaphore.
    >
    > // checking usage count
    > usage_count = semctl(sem_ptr->semid, 1, GETVAL, NULL);
    > if( usage_count > 1 )
    > // return don't remove semaphore
    > else
    > // remove semaphore
    >
    > If usage count is more than 1 then I'm not removing it.It means that at
    > this time there are more than one threads waiting for resource.
    >
    > The problem is coming when after checking the usage_count = 1 if
    > usage_count becomes 2.
    > then problem is coming.This operation is not atomic.the usage_count
    > should not be 2 while removing semaphore.
    >
    > Please anybody can tell me that How to avoid this problem.
    >
    > Regards,
    > Pawan


    Can you tell me how do you use the semphore and the threads. I think
    it's the problem about the thread.
     
    , Aug 10, 2006
    #2
    1. Advertising

  3. techi_C

    Richard Bos Guest

    "techi_C" <> wrote:

    > I'm getting a problem while removing semaphore from system.


    > Please anybody can tell me that How to avoid this problem.


    Yes. Anybody in a newsgroup where semaphores are on topic can. They're
    not ISO C, so they're off topic here.

    Richard
     
    Richard Bos, Aug 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim

    race condition question

    jimjim, Nov 1, 2003, in forum: Java
    Replies:
    6
    Views:
    434
    jimjim
    Nov 2, 2003
  2. Replies:
    1
    Views:
    377
    Kevin Spencer
    Aug 7, 2006
  3. Replies:
    3
    Views:
    763
  4. fran
    Replies:
    2
    Views:
    535
  5. Bill W.
    Replies:
    13
    Views:
    318
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page