快速业务通道

信号量应用

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-03-23
  1. #include <linux/sem.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #define SEM_PATH "/home/my_sem"
  5. #define max_tries 3
  6. int semid;
  7. main()
  8. {
  9. int flag1,flag2,key,i,init_ok,tmperrno;
  10. struct semid_ds sem_info;
  11. struct seminfo sem_info2;
  12. union semun arg; //union semun£º Çë²Î¿ŒžœÂŒ2
  13. struct sembuf askfor_res, free_res;
  14. flag1=IPC_CREAT|IPC_EXCL|00666;
  15. flag2=IPC_CREAT|00666;
  16. key=ftok(SEM_PATH,''a'');
  17. //error handling for ftok here;
  18. init_ok=0;
  19. semid=semget(key,1,flag1);//create a semaphore set that only includes one semphore.
  20. if(semid<0)
  21. {
  22. tmperrno=errno;
  23. perror("semget");
  24. if(tmperrno==EEXIST)
  25. //errno is undefined after a successful library call( including perror call) so it is saved //in tmperrno.
  26. {
  27. semid=semget(key,1,flag2);
  28. //flag2 Ö»°üº¬ÁËIPC_CREAT±êÖŸ, ²ÎÊýnsems(ÕâÀïΪ1)±ØÐëÓëÔ­ÀŽµÄÐźŵÆÊýÄ¿Ò»ÖÂ
  29. arg.buf=&sem_info;
  30. for(i=0; i<max_tries; i )
  31. {
  32. if(semctl(semid, 0, IPC_STAT, arg)==-1)
  33. { perror("semctl error"); i=max_tries;}
  34. else
  35. {
  36. if(arg.buf->sem_otime!=0){ i=max_tries; init_ok=1;}
  37. else sleep(1);
  38. }
  39. }
  40. if(!init_ok)
  41. // do some initializing, here we assume that the first process that creates the sem will
  42. // finish initialize the sem and run semop in max_tries*1 seconds. else it will not run
  43. // semop any more.
  44. {
  45. arg.val=1;
  46. if(semctl(semid,0,SETVAL,arg)==-1) perror("semctl setval error");
  47. }
  48. }
  49. else
  50. {perror("semget error, process exit");
  51. exit(1);
  52. }
  53. }
  54. else //semid>=0; do some initializing
  55. {
  56. arg.val=1;
  57. if(semctl(semid,0,SETVAL,arg)==-1)
  58. perror("semctl setval error");
  59. }
  60. //get some information about the semaphore and the limit of semaphore in redhat8.0
  61. arg.buf=&sem_info;
  62. if(semctl(semid, 0, IPC_STAT, arg)==-1)
  63. perror("semctl IPC STAT");
  64. printf("owner''s uid is %d\n", arg.buf->sem_perm.uid);
  65. printf("owner''s gid is %d\n", arg.buf->sem_perm.gid);
  66. printf("creater''s uid is %d\n", arg.buf->sem_perm.cuid);
  67. printf("creater''s gid is %d\n", arg.buf->sem_perm.cgid);
  68. arg.__buf=&sem_info2;
  69. if(semctl(semid,0,IPC_INFO,arg)==-1)
  70. perror("semctl IPC_INFO");
  71. printf("the number of entries in semaphore map is %d \n", arg.__buf->semmap);
  72. printf("max number of semaphore identifiers is %d \n", arg.__buf->semmni);
  73. printf("mas number of semaphores in system is %d \n", arg.__buf->semmns);
  74. printf("the number of undo structures system wide is %d \n", arg.__buf->semmnu);
  75. printf("max number of semaphores per semid is %d \n", arg.__buf->semmsl);
  76. printf("max number of ops per semop call is %d \n", arg.__buf->semopm);
  77. printf("max number of undo entries per process is %d \n", arg.__buf->semume);
  78. printf("the sizeof of struct sem_undo is %d \n", arg.__buf->semusz);
  79. printf("the maximum semaphore value is %d \n", arg.__buf->semvmx);
  80. //now ask for available resource:
  81. askfor_res.sem_num=0;
  82. askfor_res.sem_op=-1;
  83. askfor_res.sem_flg=SEM_UNDO;
  84. if(semop(semid,&askfor_res,1)==-1)//ask for resource
  85. perror("semop error");
  86. sleep(3); //do some handling on the sharing resource here, just sleep on it 3 seconds
  87. printf("now free the resource\n");
  88. //now free resource
  89. free_res.sem_num=0;
  90. free_res.sem_op=1;
  91. free_res.sem_flg=SEM_UNDO;
  92. if(semop(semid,&free_res,1)==-1)//free the resource.
  93. if(errno==EIDRM)
  94. printf("the semaphore set was removed\n");
  95. //you can comment out the codes below to compile a different version:
  96. if(semctl(semid, 0, IPC_RMID)==-1)
  97. perror("semctl IPC_RMID");
  98. else printf("remove sem ok\n");
  99. }

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号