免费注册 查看新帖 |

Chinaunix

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

System V信号量 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-22 16:55 |只看该作者 |倒序浏览
我实现了一个信号量的程序.
主进程循环创建5个子进程并使它们争夺数值为3的信号量.
下面是我的程序,可以运行,但觉得很麻烦,我想知道有比较简便的方法吗.

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/sem.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <unsitd.h>
  8. #include <errno.h>

  9. union semun{
  10. int val;
  11. struct semid_ds *buf;
  12. unsigned short int *array;
  13. };

  14. void print(int);
  15. int value;
  16. int main(int argc,char **argv)
  17. {
  18. int n=0;
  19. int id;
  20. struct sembuf lock_it);
  21. union semun options;

  22. if((id=semget(ftok(argv[1],0),1,IPC_CREAT|IPC_EXCL|0666))==-1)
  23.     perror(“semget”);
  24. options.val=3;
  25. semctl(id,0,SETVAL,options);

  26. while(n++<5)
  27. {
  28.     if(fork()==0)
  29.     {
  30.         while(0)
  31.         {  
  32.               lock_it.sem_num=0;
  33.               lock_it.sem_op=-1;
  34.               lock_it.sem_flg=IPC_NOWAIT;

  35.               if((semop(id,&lock_it,1))==-1)
  36.               {
  37.                    if(errno==EAGAIN)
  38.                        continue;
  39.                    exit(1);
  40.                }
  41.                print(id);
  42.                lock_it.sem_num=0;
  43.                lock_it.sem_op=1;
  44.                lock_it.sem_flg=IPC_NOWAIT;
  45.                if((semop(id,&lock_it,1))==-1)
  46.                {
  47.                     perror(“semop2”);
  48.                     exit(1);
  49.                }
  50.                else
  51.                {
  52.                     printf(“I’m finished,my pid is %d\n”,getpid());
  53.                     return;
  54.                }
  55.          }
  56.     }
  57.     sleep(3);
  58.     semctl(id,0,IPC_RMID,0);
  59.     exit(0);
  60. }
  61. }

  62. void print(int id)
  63. {
  64. printf(“I get id,my pid is %d\n”,getpid());
  65. value=semctl(id,0,GETVAL);
  66. printf(“now the value have %d\n”,value);
  67. sleep(1);
  68. }
复制代码

[ 本帖最后由 湖光倒影 于 2007-4-22 17:21 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-04-22 17:47 |只看该作者
据The Art of Unix Programming 说System V的IPC方式已经过时了

论坛徽章:
0
3 [报告]
发表于 2007-04-22 17:51 |只看该作者
原帖由 lgfang 于 2007-4-22 17:47 发表
据The Art of Unix Programming 说System V的IPC方式已经过时了

我看也是,用posix IPC方便多了

论坛徽章:
0
4 [报告]
发表于 2007-04-24 00:54 |只看该作者
APUE第二版作者建议新程序不要用IPC了,不管是posix还是System V,最好用 Unix Domain Socket
除非你是为了与以前的程序兼容。。

论坛徽章:
0
5 [报告]
发表于 2007-04-24 05:26 |只看该作者
原帖由 redhat008 于 2007-4-24 00:54 发表
APUE第二版作者建议新程序不要用IPC了,不管是posix还是System V,最好用 Unix Domain Socket
除非你是为了与以前的程序兼容。。


socket 不也是进程间通讯的一种吗?

论坛徽章:
0
6 [报告]
发表于 2007-04-24 07:11 |只看该作者
进程间互斥好像还只有SYSV IPC

论坛徽章:
0
7 [报告]
发表于 2007-04-24 13:35 |只看该作者
原帖由 langue 于 2007-4-24 05:26 发表


socket 不也是进程间通讯的一种吗?


是啊。。我说得的IPC指狭义的 System V IPC结构,如消息队列,信号量等,不指广义上的进程间通讯机制。

APUE 15.11. Summary
After comparing the timing of message queues versus full-duplex pipes, and semaphores versus record locking, we can make the following recommendations: learn pipes and FIFOs, since these two basic techniques can still be used effectively in numerous applications. Avoid using message queues and semaphores in any new applications. Full-duplex pipes and record locking should be considered instead, as they are far simpler. Shared memory still has its use, although the same functionality can be provided through the use of the mmap function 。

[ 本帖最后由 redhat008 于 2007-4-24 13:42 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP