免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3041 | 回复: 10
打印 上一主题 下一主题

[C] 突然对信号量的理解有点模糊了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-07 17:24 |只看该作者 |倒序浏览
对信号量,我一直这样认为的,而且初始值为S,当一进程申请资源时,执行wait()操作,mutex--,S值减1;当操作结束后执行signal() 操作,mute++,S值加1。
对于互斥信号量,这就意味着1是unlocked,0是locked

近日看到另外一个说法:
A mutex is a variable that can be in one of two states: unlocked or locked. Consequently, only 1 bit is required to represent it, but in practice an integer often is used, with 0 meaning unlocked and all other values meaning locked. Two procedures are used with mutexes. When a process (or thread) needs access to a critical region, it calls mutex_lock. If the mutex is currently unlocked (meaning that the critical region is available), the call succeeds and the calling thread is free to enter the critical region.
而这就意味着0是unlocked,1是locked

是不是这两者说的不是一个事,我按上面两种方法来实现互斥功能,都成功了,我都糊涂了。

[ 本帖最后由 rock_jq 于 2008-8-7 17:26 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-08-07 19:00 |只看该作者
信号量一种是互斥,一种是同步.

论坛徽章:
0
3 [报告]
发表于 2008-08-07 19:10 |只看该作者
策略和机制的关系。
不用太在意~~

论坛徽章:
0
4 [报告]
发表于 2008-08-07 20:39 |只看该作者
0是unlocked,1是locked

只是自己定义为0或1,机器不识别,所以无论你怎么变,都会都成功.

论坛徽章:
0
5 [报告]
发表于 2008-08-07 20:46 |只看该作者
原帖由 ronghuahan 于 2008-8-7 19:00 发表
信号量一种是互斥,一种是同步.

应该分为公用和私用吧

论坛徽章:
0
6 [报告]
发表于 2008-08-07 20:59 |只看该作者
路过~

论坛徽章:
0
7 [报告]
发表于 2008-08-07 23:36 |只看该作者
好熟悉的,好像哪见过.
一查,原来是俺刚来混C版是见到的:
http://bbs.chinaunix.net/viewthr ... =%D0%C5%BA%C5%C1%BF

论坛徽章:
0
8 [报告]
发表于 2008-08-08 10:25 |只看该作者
为什么只局限于0跟1,0跟-1也是可以的

论坛徽章:
0
9 [报告]
发表于 2008-08-08 10:30 |只看该作者

回复 #8 wmytch 的帖子

0和1只需要一个bit,在很多情况下更简单。

论坛徽章:
0
10 [报告]
发表于 2008-08-11 11:32 |只看该作者
简单点说semop函数怎么知道我采用那种机制?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP