sem_wait不能被sem_post唤醒

zjie_main 2014-12-08 06:43:05
有两种情况:

先wait再post不能唤醒
+----------------------------+--------------------+----------------+
|sem_init(&sem_001, 0, 0) |... |... |
+----------------------------+--------------------+----------------+
|thread 1 |thread 2 |thread other |
+----------------------------+--------------------+----------------+
|sem_wait(&sem_001) |... |no sem func |
|... |sem_post(&sem_001) |no sem func |
|can not wakeup |... |... |
+----------------------------+--------------------+----------------+

先post再wait可以唤醒
+----------------------------+--------------------+----------------+
|sem_init(&sem_001, 0, 0) |... |... |
+----------------------------+--------------------+----------------+
|thread 1 |thread 2 |thread other |
+----------------------------+--------------------+----------------+
|... |sem_post(&sem_001) |no sem func |
|sem_wait(&sem_001) |... |no sem func |
|can wakeup |... |... |
+----------------------------+--------------------+----------------+

代码复杂贴不上来,请问可能是什么原因导致的。
...全文
2235 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
sem_init函数最后两个参数注意一下,问题就找到了。
jackchzj 2016-03-05
  • 打赏
  • 举报
回复
是不是屏蔽了某些信号?
zjie_main 2015-01-08
  • 打赏
  • 举报
回复
引用 9 楼 K_THEKING 的回复:
简单例子测试是通过的,但是不知道为什么您的程序没有通过
无解了
zhxianbin 2015-01-08
  • 打赏
  • 举报
回复
找自己代码的问题
K_THEKING 2014-12-31
  • 打赏
  • 举报
回复
简单例子测试是通过的,但是不知道为什么您的程序没有通过
zjie_main 2014-12-17
  • 打赏
  • 举报
回复
引用 2 楼 zjie_main 的回复:
坐等大神回复
zhxianbin 2014-12-13
  • 打赏
  • 举报
回复
你可以按上面的两种情况写一个简化的代码测试一下,我觉得应该没问题,正常的话就表明问题在其它部分
zjie_main 2014-12-13
  • 打赏
  • 举报
回复
引用 4 楼 zhxianbin 的回复:
先wait再post不能唤醒 +----------------------------+--------------------+----------------+ |sem_init(&sem_001, 0, 0) |... |... | +----------------------------+--------------------+----------------+ |thread 1 |thread 2 |thread other | +----------------------------+--------------------+----------------+ |sem_wait(&sem_001) |... |no sem func | |... |sem_post(&sem_001) |no sem func | |can not wakeup |... |... | +----------------------------+--------------------+----------------+ |thread 2 的 sem_post 执行了吗
当然有
zhxianbin 2014-12-12
  • 打赏
  • 举报
回复
先wait再post不能唤醒 +----------------------------+--------------------+----------------+ |sem_init(&sem_001, 0, 0) |... |... | +----------------------------+--------------------+----------------+ |thread 1 |thread 2 |thread other | +----------------------------+--------------------+----------------+ |sem_wait(&sem_001) |... |no sem func | |... |sem_post(&sem_001) |no sem func | |can not wakeup |... |... | +----------------------------+--------------------+----------------+ |thread 2 的 sem_post 执行了吗
zjie_main 2014-12-12
  • 打赏
  • 举报
回复
引用 1 楼 zhxianbin 的回复:
因为你初始化为 0 的,先 wait 变成 -1 了,所以 一个 post 不能唤醒
If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signal handler interrupts the call.
zjie_main 2014-12-12
  • 打赏
  • 举报
回复
坐等大神回复
zhxianbin 2014-12-08
  • 打赏
  • 举报
回复 1
因为你初始化为 0 的,先 wait 变成 -1 了,所以 一个 post 不能唤醒

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧