ubuntu: sem_timedwait not waking (C)

前端 未结 6 1226
花落未央
花落未央 2021-02-06 17:01

I have 3 processes which need to be synchronized. Process one does something then wakes process two and sleeps, which does something then wakes process three and sleeps, which d

6条回答
  •  既然无缘
    2021-02-06 17:57

    I have no clue on what is going wrong and the code looks fine too me. Here are some things you could do do somehow get more information.

    • use a different timeout, both shorter and longer, and see if your problem still occurs.
    • use a non timed version, and see if the programm hangs.
    • try to modify the behaviour of your kernel scheduler, for example using kernel command line parameters, or using procfs or sysfs.

    As pointed by Jens, there are two races :

    The first is when evaluationg the value of the semaphore, after the call to sem_timedwait. This is not changing the control flow whit respects to semaphore. Wether the thread timedout or not, it still goes through the "should I trigger the next thread" block.

    The second is in the "Should I wakeup the next thread" part. We could have the following events :

    1. Threads n calls sem_getvalue(trigger) and gets a 1
    2. Thread n+1 returns from sem_timedwait and the semaphore goes to 0
    3. Thread n decides not to post and the semaphore stays to 0

    Now, I can't see how this could trigger the observed behaviour. After all, since Thread n+1 is waked up anyway, it will in turn wake up thread n+2 which will wake up thread n etc...

    While it is possible to get glitches, I can't see how this could lead to systematic timeout from a thread.

提交回复
热议问题