信号量应用

 
  1. #include<linux/sem.h>
  2. #include<stdio.h>
  3. #include<errno.h>
  4. #defineSEM_PATH"/home/my_sem"
  5. #definemax_tries3
  6. intsemid;
  7. main()
  8. {
  9. intflag1,flag2,key,i,init_ok,tmperrno;
  10. structsemid_dssem_info;
  11. structseminfosem_info2;
  12. unionsemunarg;//unionsemun£ºÇë²Î¿ŒžœÂŒ2
  13. structsembufaskfor_res,free_res;
  14. flag1=IPC_CREAT|IPC_EXCL|00666;
  15. flag2=IPC_CREAT|00666;
  16. key=ftok(SEM_PATH,'a');
  17. //errorhandlingforftokhere;
  18. init_ok=0;
  19. semid=semget(key,1,flag1);//createasemaphoresetthatonlyincludesonesemphore.
  20. if(semid<0)
  21. {
  22. tmperrno=errno;
  23. perror("semget");
  24. if(tmperrno==EEXIST)
  25. //errnoisundefinedafterasuccessfullibrarycall(includingperrorcall)soitissaved//intmperrno.
  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("semctlerror");i=max_tries;}
  34. else
  35. {
  36. if(arg.buf->sem_otime!=0){i=max_tries;init_ok=1;}
  37. elsesleep(1);
  38. }
  39. }
  40. if(!init_ok)
  41. //dosomeinitializing,hereweassumethatthefirstprocessthatcreatesthesemwill
  42. //finishinitializethesemandrunsemopinmax_tries*1seconds.elseitwillnotrun
  43. //semopanymore.
  44. {
  45. arg.val=1;
  46. if(semctl(semid,0,SETVAL,arg)==-1)perror("semctlsetvalerror");
  47. }
  48. }
  49. else
  50. {perror("semgeterror,processexit");
  51. exit(1);
  52. }
  53. }
  54. else//semid>=0;dosomeinitializing
  55. {
  56. arg.val=1;
  57. if(semctl(semid,0,SETVAL,arg)==-1)
  58. perror("semctlsetvalerror");
  59. }
  60. //getsomeinformationaboutthesemaphoreandthelimitofsemaphoreinredhat8.0
  61. arg.buf=&sem_info;
  62. if(semctl(semid,0,IPC_STAT,arg)==-1)
  63. perror("semctlIPCSTAT");
  64. printf("owner'suidis%d\n",arg.buf->sem_perm.uid);
  65. printf("owner'sgidis%d\n",arg.buf->sem_perm.gid);
  66. printf("creater'suidis%d\n",arg.buf->sem_perm.cuid);
  67. printf("creater'sgidis%d\n",arg.buf->sem_perm.cgid);
  68. arg.__buf=&sem_info2;
  69. if(semctl(semid,0,IPC_INFO,arg)==-1)
  70. perror("semctlIPC_INFO");
  71. printf("thenumberofentriesinsemaphoremapis%d\n",arg.__buf->semmap);
  72. printf("maxnumberofsemaphoreidentifiersis%d\n",arg.__buf->semmni);
  73. printf("masnumberofsemaphoresinsystemis%d\n",arg.__buf->semmns);
  74. printf("thenumberofundostructuressystemwideis%d\n",arg.__buf->semmnu);
  75. printf("maxnumberofsemaphorespersemidis%d\n",arg.__buf->semmsl);
  76. printf("maxnumberofopspersemopcallis%d\n",arg.__buf->semopm);
  77. printf("maxnumberofundoentriesperprocessis%d\n",arg.__buf->semume);
  78. printf("thesizeofofstructsem_undois%d\n",arg.__buf->semusz);
  79. printf("themaximumsemaphorevalueis%d\n",arg.__buf->semvmx);
  80. //nowaskforavailableresource:
  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)//askforresource
  85. perror("semoperror");
  86. sleep(3);//dosomehandlingonthesharingresourcehere,justsleeponit3seconds
  87. printf("nowfreetheresource\n");
  88. //nowfreeresource
  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)//freetheresource.
  93. if(errno==EIDRM)
  94. printf("thesemaphoresetwasremoved\n");
  95. //youcancommentoutthecodesbelowtocompileadifferentversion:
  96. if(semctl(semid,0,IPC_RMID)==-1)
  97. perror("semctlIPC_RMID");
  98. elseprintf("removesemok\n");
  99. }

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/725028

你可能感兴趣的:(信号量)