- #include<linux/sem.h>
- #include<stdio.h>
- #include<errno.h>
- #defineSEM_PATH"/home/my_sem"
- #definemax_tries3
- intsemid;
- main()
- {
- intflag1,flag2,key,i,init_ok,tmperrno;
- structsemid_dssem_info;
- structseminfosem_info2;
- unionsemunarg;//unionsemun£ºÇë²Î¿ŒžœÂŒ2
- structsembufaskfor_res,free_res;
- flag1=IPC_CREAT|IPC_EXCL|00666;
- flag2=IPC_CREAT|00666;
- key=ftok(SEM_PATH,'a');
- //errorhandlingforftokhere;
- init_ok=0;
- semid=semget(key,1,flag1);//createasemaphoresetthatonlyincludesonesemphore.
- if(semid<0)
- {
- tmperrno=errno;
- perror("semget");
- if(tmperrno==EEXIST)
- //errnoisundefinedafterasuccessfullibrarycall(includingperrorcall)soitissaved//intmperrno.
- {
- semid=semget(key,1,flag2);
- //flag2Ö»°üº¬ÁËIPC_CREAT±êÖŸ,²ÎÊýnsems(ÕâÀïΪ1)±ØÐëÓëÔÀŽµÄÐźŵÆÊýÄ¿Ò»ÖÂ
- arg.buf=&sem_info;
- for(i=0;i<max_tries;i++)
- {
- if(semctl(semid,0,IPC_STAT,arg)==-1)
- {perror("semctlerror");i=max_tries;}
- else
- {
- if(arg.buf->sem_otime!=0){i=max_tries;init_ok=1;}
- elsesleep(1);
- }
- }
- if(!init_ok)
- //dosomeinitializing,hereweassumethatthefirstprocessthatcreatesthesemwill
- //finishinitializethesemandrunsemopinmax_tries*1seconds.elseitwillnotrun
- //semopanymore.
- {
- arg.val=1;
- if(semctl(semid,0,SETVAL,arg)==-1)perror("semctlsetvalerror");
- }
- }
- else
- {perror("semgeterror,processexit");
- exit(1);
- }
- }
- else//semid>=0;dosomeinitializing
- {
- arg.val=1;
- if(semctl(semid,0,SETVAL,arg)==-1)
- perror("semctlsetvalerror");
- }
- //getsomeinformationaboutthesemaphoreandthelimitofsemaphoreinredhat8.0
- arg.buf=&sem_info;
- if(semctl(semid,0,IPC_STAT,arg)==-1)
- perror("semctlIPCSTAT");
- printf("owner'suidis%d\n",arg.buf->sem_perm.uid);
- printf("owner'sgidis%d\n",arg.buf->sem_perm.gid);
- printf("creater'suidis%d\n",arg.buf->sem_perm.cuid);
- printf("creater'sgidis%d\n",arg.buf->sem_perm.cgid);
- arg.__buf=&sem_info2;
- if(semctl(semid,0,IPC_INFO,arg)==-1)
- perror("semctlIPC_INFO");
- printf("thenumberofentriesinsemaphoremapis%d\n",arg.__buf->semmap);
- printf("maxnumberofsemaphoreidentifiersis%d\n",arg.__buf->semmni);
- printf("masnumberofsemaphoresinsystemis%d\n",arg.__buf->semmns);
- printf("thenumberofundostructuressystemwideis%d\n",arg.__buf->semmnu);
- printf("maxnumberofsemaphorespersemidis%d\n",arg.__buf->semmsl);
- printf("maxnumberofopspersemopcallis%d\n",arg.__buf->semopm);
- printf("maxnumberofundoentriesperprocessis%d\n",arg.__buf->semume);
- printf("thesizeofofstructsem_undois%d\n",arg.__buf->semusz);
- printf("themaximumsemaphorevalueis%d\n",arg.__buf->semvmx);
- //nowaskforavailableresource:
- askfor_res.sem_num=0;
- askfor_res.sem_op=-1;
- askfor_res.sem_flg=SEM_UNDO;
- if(semop(semid,&askfor_res,1)==-1)//askforresource
- perror("semoperror");
- sleep(3);//dosomehandlingonthesharingresourcehere,justsleeponit3seconds
- printf("nowfreetheresource\n");
- //nowfreeresource
- free_res.sem_num=0;
- free_res.sem_op=1;
- free_res.sem_flg=SEM_UNDO;
- if(semop(semid,&free_res,1)==-1)//freetheresource.
- if(errno==EIDRM)
- printf("thesemaphoresetwasremoved\n");
- //youcancommentoutthecodesbelowtocompileadifferentversion:
- if(semctl(semid,0,IPC_RMID)==-1)
- perror("semctlIPC_RMID");
- elseprintf("removesemok\n");
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/725028