免费注册 查看新帖 |

Chinaunix

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

这段话是不是说错了?(关于信号量) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-30 21:55 |只看该作者 |倒序浏览
最近在看《Operating Systems Design and Implementation ,Third Edition》
看到信号量这里(2.26),书里面有一段话,我觉得似乎说错了。注意加粗段。

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.


应该是1意味着unlocked,0是locked

是我理解错了,还是作者说错了?

论坛徽章:
0
2 [报告]
发表于 2007-11-30 22:23 |只看该作者
我也觉得有点问题,一般p操作是把信号量减1,v操作加1。
按照正常情况二元信号量0应该是上锁了。

论坛徽章:
0
3 [报告]
发表于 2007-11-30 22:53 |只看该作者
没问题

论坛徽章:
0
4 [报告]
发表于 2007-11-30 23:04 |只看该作者
楼上解释一下,为什么 "0 meaning unlocked "

论坛徽章:
0
5 [报告]
发表于 2007-11-30 23:10 |只看该作者
0说明0个用户,n说明n个用户不是很直观麽。
而且,具体用什么数字表示lock/unlock完全是你自己规定的,你就愿意用1表示unlock也没问题。

论坛徽章:
0
6 [报告]
发表于 2007-11-30 23:26 |只看该作者
但是PV操作是当程序要做P操作的时候,发现信号量是0时,就等待。

如果按你的说法,那就要重新实现PV操作了阿?

我很怀疑是书印错了。

[ 本帖最后由 janusle 于 2007-11-30 23:27 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2007-11-30 23:35 |只看该作者
人家说了是mutex也没说是semaphore。。。
再说了这些东西现在就是对原子操作的抽象,什么pv qw的叫什么不成啊

论坛徽章:
0
8 [报告]
发表于 2007-12-01 00:10 |只看该作者
不好意思阿,是我没有说清楚,其实这段话出现在“信号量”这里,作者是使用信号量来演示互斥。

他说的这段话,和他前面演示的代码有出入,所以我才问一下的。


  1. #define N 100                          /* number of slots in the buffer */ typedef int semaphore;                 /* semaphores are a special kind of int */
  2. semaphore mutex = 1;                   /* controls access to critical region */
  3. semaphore empty = N;                   /* counts empty buffer slots */
  4. semaphore full = 0;                    /* counts full buffer slots */
  5. void producer(void)
  6. {
  7.      int item;
  8.      while (TRUE){                     /* TRUE is the constant 1 */
  9.           item = produce_item();       /* generate something to put in buffer */
  10.           down(&empty);                /* decrement empty count */
  11.           down(&mutex);                /* enter critical region */
  12.           insert_item(item);           /* put new item in buffer */
  13.           up(&mutex);                  /* leave critical region */
  14.           up(&full);                   /* increment count of full slots */
  15.      }
  16. }
  17. void consumer(void)
  18. {
  19.      int item;
  20.      while (TRUE){                     /* infinite loop */
  21.           down(&full);                 /* decrement full count */
  22.           down(&mutex);                /* enter critical region */
  23.           item = remove_item();        /* take item from buffer */
  24.           up(&mutex);                  /* leave critical region */
  25.           up(&empty);                  /* increment count of empty slots */
  26.           consume_item(item);          /* do something with the item */
  27.      }
  28. }



复制代码

[ 本帖最后由 janusle 于 2007-12-1 00:18 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2007-12-01 01:09 |只看该作者
哦,那就不知道了,这本书没看过,你可以找找网站上有没有勘误啥的,或者给作者个email确认一下。

论坛徽章:
0
10 [报告]
发表于 2007-12-01 12:52 |只看该作者

生产者--消费者问题

我认为互斥信号量mutex是保证两个进程对一个资源互斥操作。一般就有两个值:0和1。而且初始值为1,当一进程申请资源时,执行wait()操作,mutex--,值为0,其他进程不能再对资源作任何操作即lock.当操作结束后执行signal() 操作,mute++,值为1。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP