免费注册 查看新帖 |

Chinaunix

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

[Linux] 进程间通信的system v信号量 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-21 16:59 |只看该作者 |倒序浏览

  1.     (1)semcreate.c  [color=Red]/创建一个信号量集/[/color]

  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<sys/sem.h>
  5. #include<unistd.h>
  6. #include<sys/types.h>
  7. int main(int argc,char **argv)
  8. {
  9.     int c,oflag,semid,nsems;

  10.     oflag=0644|IPC_CREAT;
  11.     while((c=getopt(argc,argv,"e"))!=-1)
  12.     {
  13.         switch(c)
  14.         {
  15.             case 'e':
  16.                 oflag|=IPC_EXCL;
  17.                 printf("create faile :exist\n");
  18.                 break;
  19.         }
  20.     }
  21.     if(optind!=argc-2)
  22.         printf("usage:semcreate[-e]<pathname><nsems>\n");
  23.      nsems=atoi(argv[optind+1]);
  24.         printf("nsems=%d\n",nsems);
  25.     semid=semget(ftok(argv[optind],0),nsems,oflag);
  26.     printf("semid=%d\n",semid);
  27.     exit(0);
  28. }
  29.    (2)semrmid.c  [color=Red]/*删除一个信号量集*/[/color]
  30. #include<stdio.h>
  31. #include<stdlib.h>
  32. #include<sys/sem.h>
  33. int main(int argc,char **argv)
  34. {
  35.     int semid;
  36.    if(argc!=2)
  37.         printf("usage:semrid<pathname>");
  38.     semid=semget(ftok(argv[1],0),0,0);
  39.     semctl(semid,0,IPC_RMID);
  40.     exit(0);
  41. }
  42. (3)semsetvalues.c /*设置信号量集用元素的值*/
  43. #include<stdio.h>
  44. #include<unistd.h>
  45. #include<sys/sem.h>
  46. #include<stdlib.h>
  47. union semun
  48. {
  49.     int val;
  50.     struct semid_ds *buf;
  51.     unsigned short *array;
  52. };
  53. int main(int argc,char **argv)
  54. {
  55.     int semid,nsems,i;
  56.     struct semid_ds seminfo;
  57.     unsigned short *ptr;
  58.     union semun arg;

  59.     if(argc<2)
  60.         printf("usage:semsetvalues<pathname>[values...]\n");
  61.     semid=semget(ftok(argv[1],0),0,0);
  62.      printf("semid=%d\n",semid);
  63.         arg.buf=&seminfo;
  64.     semctl(semid,0,IPC_STAT,arg);
  65.     nsems=arg.buf->sem_nsems;
  66.    
  67.     if (argc!=nsems+2)
  68.         printf("%d semaphores in set,%d values specified",nsems,argc-2);
  69.     ptr=calloc(nsems,sizeof(unsigned short));
  70.     arg.array=ptr;
  71.     for(i=0;i<nsems;i++)
  72.         ptr[i]=atoi(argv[i+2]);
  73.     semctl(semid,0,SETALL,arg);
  74.     exit(0);
  75. }
  76. (4)semsetvalues.c
  77. #include<stdio.h>
  78. #include<stdlib.h>
  79. #include<sys/sem.h>
  80. union semun
  81. {
  82.     int val;
  83.     struct semid_ds *buf;
  84.     unsigned short *array;
  85. };

  86. int main(int argc,char **argv)
  87. {
  88.     int semid,nsems,i;
  89.     struct  semid_ds seminfo;
  90.     unsigned short *ptr;
  91.     union semun arg;
  92.        if(argc!=2)
  93.         printf("usage:semgetvalues<pathname>\n");
  94.     semid=semget(ftok(argv[1],0),0,0);
  95.     printf("semid=%d\n",semid);
  96.        arg.buf=&seminfo;
  97.     semctl(semid,0,IPC_STAT,arg);
  98.     nsems=arg.buf->sem_nsems;
  99.     ptr=calloc(nsems,sizeof(unsigned short));
  100.     arg.array=ptr;
  101.        semctl(semid,0,GETALL,arg);
  102.     for(i=0;i<nsems;i++)
  103.         printf("semval[%d]=%d\n",i,ptr[i]);
  104.     exit(0);
  105. }
  106. (5)
  107. semops.c
  108. #include<stdlib.h>
  109. #include<stdio.h>
  110. #include<sys/sem.h>
  111. #include<unistd.h>

  112. int main(int argc,char **argv)
  113. {
  114.     int c,i,flag,semid,nops;
  115.     struct sembuf *ptr;

  116.     flag=0;
  117.     while((c=getopt(argc,argv,"nu"))!=-1)
  118.     {
  119.         switch(c)
  120.         {
  121.             case 'n':
  122.                 flag|=IPC_NOWAIT;
  123.                 break;
  124.             case 'u':
  125.                 flag|=SEM_UNDO;
  126.                 break;
  127.         }
  128.     }
  129.     if(argc-optind<2)
  130.         printf("usage:semops[-n][-u]<pathname>operation...");
  131.     semid=semget(ftok(argv[optind],0),0,0);
  132.     optind++;
  133.     nops=argc-optind;
  134.     ptr=calloc(nops,sizeof(struct sembuf));
  135.     for(i=0;i<nops;i++)
  136.     {
  137.         ptr[i].sem_num=i;
  138.         ptr[i].sem_op=atoi(argv[optind+1]);
  139.         ptr[i].sem_flg=flag;
  140.     }
  141.     semop(semid,ptr,nops);
  142.         exit(0);
  143. }

复制代码
运行:

ermao@lost-desktop:~/ermao$ gcc -o semcreate semcreate.c
ermao@lost-desktop:~/ermao$ gcc -o semrmid  semrmid.c
ermao@lost-desktop:~/ermao$ gcc -o semsetvalue semsetvalue.c
ermao@lost-desktop:~/ermao$ gcc -o semgetvalues semgetvalues.c
ermao@lost-desktop:~/ermao$ gcc -o semops  semops.c
ermao@lost-desktop:~/ermao$ touch /tmp/ermao
ermao@lost-desktop:~/ermao$ ./semcreate /tmp/ermao 3
nsems=3
semid=32769
ermao@lost-desktop:~/ermao$ ./semsetvalue /tmp/ermao 1 2 3
nsems=134514384
ermao@lost-desktop:~/ermao$ ./semgetvalues /tmp/ermao
nsems=134514315
semval[0]=1
semval[1]=2
semval[2]=3

ermao@lost-desktop:~/ermao$ ./semops -n /tmp/ermao -1 -2 -4
./semops: invalid option -- '1'问什么为负数的时候invalid的,求解释?
./semops: invalid option -- '2'
./semops: invalid option -- '4'
usage:semops[-n][-u]<pathname>operation...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP