免费注册 查看新帖 |

Chinaunix

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

使用信号量进行进程间同步时总是出现Numerical result out of range的错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-29 13:49 |只看该作者 |倒序浏览
运行一段时间后semop时就出现Numerical result out of range错误,

测试发现如果设置成
sops.sem_flg = SEM_UNDO
这个,就会出现,
如果
sops.sem_flg = 0
就不出现,有没有使用过信号量的朋友,释疑一下,非常感谢!

论坛徽章:
0
2 [报告]
发表于 2010-10-29 15:00 |只看该作者
本帖最后由 krein8964 于 2010-10-29 15:02 编辑

信号量的值超出信号量允许的最大值。

SEM_UNDO标志会使进程在退出时,对信号量的值进行反操作。例如,进程对信号量进行过两次P操作,那么进程退出时会使信号量的值+2,相当于两次V操作。

论坛徽章:
0
3 [报告]
发表于 2010-10-29 15:22 |只看该作者
信号量的值超出信号量允许的最大值。

SEM_UNDO标志会使进程在退出时,对信号量的值进行反操作。例如,进 ...
krein8964 发表于 2010-10-29 15:00



    可以肯定的是信号量的值没有超出最大值,其实当时的信号量的值也就是1,同样的问题:http://www.unixresources.net/lin ... 0/44/03/440370.html

但是我按照上面的解决办法,测试发现无效,实在费解。

论坛徽章:
0
4 [报告]
发表于 2010-11-04 09:54 |只看该作者
等待高手释疑。。。

论坛徽章:
0
5 [报告]
发表于 2011-01-07 13:16 |只看该作者
问题没有解决,去掉了sops.sem_flg = SEM_UNDO

论坛徽章:
0
6 [报告]
发表于 2011-03-22 16:22 |只看该作者
这个问题我也遇到了,已经解决。在P和V操作时,都要设置此flag(SEM_UNDO),就不会出现问题了

论坛徽章:
0
7 [报告]
发表于 2011-03-22 16:26 |只看该作者
把我的代码贴出来吧,和大家分享一下

  1. /*****************************************************************
  2. *
  3. * Copyright (c) 2011
  4. *
  5. * FILE NAME:   lock.c
  6. * FUNC DESC.:  lock and unluck
  7. *
  8. * Version: 1.0
  9. * Auther: Boonie Chiong
  10. * Date:  2011-03-22
  11. *
  12. * Change logs:
  13. *
  14. ******************************************************************/


  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>

  19. #include <sys/sem.h>

  20. #include "common.h"
  21. #include "lock.h"
  22.   
  23. /**************************************************************************
  24. * function: sem_init
  25. * description: create and initial semaphore
  26. * input:       int key
  27.                 int sem_num
  28.                 unsigned short int value_array
  29. * output:      null
  30. * return:      >0 SUCCEED  other: FAILED
  31. **************************************************************************/
  32. int wea_api_sem_init(int key)
  33. {
  34.     static unsigned short array_value = 1;
  35.     int sem_id;

  36.     union semun
  37.     {
  38.         int val;
  39.         struct semid_ds * buf;
  40.         ushort  * array;
  41.     }argument;

  42.     argument.array = &array_value;

  43.     /* Create the semaphore */
  44.     if((sem_id = semget(key, 1, 0666 | IPC_CREAT))<0)
  45.     {
  46.         /* Always check system returns. */
  47.         return FAILED;
  48.     }


  49.     /*  What we actually get is an array of semaphores. The second
  50.     *  argument to semget() was the array dimension - in our case
  51.     *  1. */
  52.     if( semctl(sem_id, 0, SETALL, argument) < 0)
  53.     {
  54.         return  FAILED;
  55.     }

  56.     if ((sem_id = semget(key, 0, 0)) < 0)
  57.     {
  58.         return FAILED;
  59.     }


  60.     return sem_id;
  61. }

  62. /****************************************************************
  63. * function:    sem_open
  64. * discription: open semaphore
  65. * input:       int key
  66. * output:      null
  67. * return:      0: SUCCEED  other: FAILED
  68. ****************************************************************/
  69. int wea_api_sem_open(int key)
  70. {
  71.     int sem_id;

  72.     if ((sem_id = semget(key, 0, 0)) < 0)
  73.     {
  74.         return FAILED;
  75.     }

  76.     return sem_id;
  77. }


  78. /****************************************************************
  79. * function:    sem_lock
  80. * description: semaphore lock
  81. * input:       int sem_id
  82. *              int sem_num
  83. * output:      null
  84. * return:      0:SUCCEED  other: FAILED
  85. ****************************************************************/
  86. int wea_api_sem_lock(int sem_id)
  87. {
  88.     // An "array" of one operation to perform on the semaphore.
  89.     struct sembuf operations[1];
  90.     int retval;         // Return value from semop()

  91.     /* Set up the sembuf structure. */
  92.     /* Which semaphore in the semaphore array : */
  93.     operations[0].sem_num = 0;
  94.     /* Which operation? Subtract 1 from semaphore value : */
  95.     operations[0].sem_op = -1;
  96.     /* Set the flag so we will wait : */
  97.     //operations[0].sem_flg = 0;
  98.     operations[0].sem_flg = SEM_UNDO;

  99.     /* So do the operation! */
  100.     if((retval = semop(sem_id, operations, 1))!=0)
  101.     {
  102.         return FAILED;
  103.     }

  104.     return SUCCEED;
  105. }

  106. /******************************************************************
  107. * function:    sem_unlock
  108. * description: semaphore unlock
  109. * input:       int sem_id
  110.                 int sem_num
  111. * output:      null
  112. * return:      0:SUCCEED  other: FAILED
  113. ******************************************************************/
  114. int wea_api_sem_unlock(int sem_id)
  115. {
  116.     struct sembuf operations[1];
  117.     /* An "array" of one operation to perform on the semaphore. */
  118.     int retval; /* Return value from semop() */
  119.     /* Do a semaphore V-operation. */
  120.     /*printf("Program sema about to do a V-operation. ");*/
  121.     /* Set up the sembuf structure. */
  122.     /* Which semaphore in the semaphore array : */
  123.     operations[0].sem_num = 0;
  124.     /* Which operation? Add 1 to semaphore value : */
  125.     operations[0].sem_op = 1;
  126.     /* Set the flag so we will wait : */
  127.     //operations[0].sem_flg = 0;
  128.     operations[0].sem_flg = SEM_UNDO;

  129.     /* So do the operation! */
  130.     if((retval = semop(sem_id, operations, 1))!=0)
  131.     {
  132.         return FAILED;
  133.     }

  134.     return SUCCEED;
  135. }


  136. /****************************************************************
  137. * function:    sem_exit
  138. * description: semaphore exit
  139. * input:       int sem_id
  140. * output:      null
  141. * return:      0:SUCCEED  other: FAILED
  142. ***************************************************************/
  143. int wea_api_sem_exit(int sem_id)
  144. {
  145.     if(semctl(sem_id,0,IPC_RMID,(struct msquid_ds *)0) == -1)
  146.     {
  147.         return FAILED;
  148.     }

  149.     return SUCCEED;

  150. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP