码迷,mamicode.com
首页 > 其他好文 > 详细

信号量

时间:2016-02-07 17:31:07      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<linux/sem.h>//信号量的头文件
  5 #include<errno.h>
  6 #define SEM_PATH "/home/wangguolong/"
  7 #define max_tries 3
  8 int semid;
  9 int main(void)
 10 {
 11     int flag1,flag2,key,i,init_ok,tmperrno;
 1 /*
 2 结构体 semid_ds
 3 用于记录信号量集的各种信息0
 4 struct semid_ds
 5 {
 6      struct ipc_perm sem_perm;//操作权限结构
 7      struct sem *sem_base;//第一个信号量的信息
 8      unsigned short sem_nsems;//信号量的数目
 9      time_t sem_otime;//最终操作时间
10      time_t sme_ctime;//最终更改时间
11 };
12 
13 struct sem
14 {
15      unsigned short semval;//信号量的值,一般≥0
16      pid_t sempid;//最终操作的pid
17      unsigned short semncent;//信号量的值大于currval值的进程个数
18      unsigned short semzcnt;//信号量的值为0时的进程个数
19      
20 };
21 
22 
23 
24 *

 12     struct semid_ds sem_info;
 1 /*
 2 
 3 
 4 struct  seminfo {
 5     int semmap;//信号图条目的数量
 6     int semmni;//最大信号标识符号码
 7     int semmns;//在系统信号量的MAS数
 8     int semmnu;//撤消结构系统的数目是
 9     int semmsl;//最大信号每半数
10     int semopm;//最大的行动电话号码是每个信号量操作
11     int semume;//每个进程的撤销项的最大数目
12     int semusz;//结构sem_undo的比特数
13     int semvmx;//最大信号值
14 15 };
16 
17 */

 13     struct seminfo sem_info2;
 1 /*
 2 联合体semun
 3 
 4 union semun
 5 {
 6       int val;  val的值
 7       struct semid_ds *buf;//供IPC_STAT 和IPC_SET使用
 8       unsigned short array;/供GETALL和SETALL使用
 9 }
10 
11 
12 
13 */

 14     union semun arg;
 1 /*
 2 sembuf结构体
 3 
 4 struct sembuf
 5 {
 6        unsigned short sem_num;//将要操作的信号量
 7        short sem_op;//所要执行的操作
 8        short sem_flg;//semop的行为
 9 }
10 */

 15     struct sembuf askfor_res,free_res;
 16     flag1=IPC_CREAT|IPC_EXCL|00666;
 17     flag2=IPC_CREAT|00666;
 18     key=ftok(SEM_PATH,a);
 19     if(key==-1)
 20     {
 21         perror("ftok error!");
 22         exit(1);
 23     }
 24     
 25     init_ok=0;
 26     semid=semget(key,1,flag1);
 27     
 28 if(semid<0)
 29 {
 30     tmperrno=errno;
 31     perror("semget");
 32         
 33     if(tmperrno==EEXIST)
 34     {
 35         semid=semget(key,1,flag2);
 36         arg.buf=&sem_info;
 37         for(i=0;i<max_tries;i++)
 38         {
 39             if(semctl(semid,0,IPC_STAT,arg)==-1)
 40             {
 41                 perror("semctl error");
 42                 i=max_tries;
 43             }
 44             else
 45             {
 46                 if(arg.buf->sem_otime!=0)
 47                 {
 48                     init_ok=1;
 49                 }
 50                 else
 51                     sleep(1);
 52                 
 53             }
 54         }
 55         
 56         if(!init_ok)
 57         {
 58             arg.val=1;
 59             if(semctl(semid,0,SETVAL,arg)==-1)
 60             perror("semctl setval error");
 61         }
 62     }
 63     else
 64     {
 65         perror("semget error ,process exit");
 66         exit(1);
 67     }
 68 }
 69 else
 70 {
 71     arg.val=1;
 72     if(semctl(semid,0,SETVAL,arg)==-1)
 73     perror("semctl setval error");
 74 }    
 75 
 76     arg.buf=&sem_info;
 77     if(semctl(semid,0,IPC_STAT,arg)==-1)
 78     {
 79         perror("semctl IPC STAT");
 80     }
 81     printf("owner‘s uid is %d\n",arg.buf->sem_perm.uid);
 82     printf("owner‘s gid is %d\n",arg.buf->sem_perm.gid);
 83     printf("creater‘s uid is %d\n",arg.buf->sem_perm.cuid);
 84     printf("creater‘s gid is %d\n",arg.buf->sem_perm.cgid);
 85     arg.__buf=&sem_info2;
 86     if(semctl(semid,0,IPC_INFO,arg)==-1)
 87     perror("semctl IPC_INFO");
 88     
 89     printf("the number of entries in semaphore map is %d\n",arg.__buf->semmap);
 90     printf("max number of semaphore identifiers is %d\n",arg.__buf->semmni);
 91     printf("mas number of semaphores in system is %d\n",arg.__buf->semmns);
 92     printf("the number of undo  structures system wide is %d\n",arg.__buf->semmnu);
 93     printf("max number of semaphores per semid is %d\n",arg.__buf->semmsl);
 94     printf("max number of ops per semop call is %d\n",arg.__buf->semopm);
 95     printf("max number of undo entries per process is %d\n",arg.__buf->semume);
 96     printf("the size of struct sem_undo is %d\n",arg.__buf->semusz);
 97     printf("the maximum semaphore value is %d\n",arg.__buf->semvmx);
 98 
 99     askfor_res.sem_num=0;
100     askfor_res.sem_op=-1;
101     askfor_res.sem_flg=SEM_UNDO;
102     if(semop(semid,&askfor_res,1)==-1)
103     perror("semop error");
104     sleep(3);
105     printf("now free the resource\n");
106     
107     free_res.sem_num=0;
108     free_res.sem_op=1;
109     free_res.sem_flg=SEM_UNDO;
110     if(semop(semid,&free_res,1)==-1)
111     if(errno==EIDRM)
112     printf("The senaophore set was removed\n");
113 
114     if(semctl(semid,0,IPC_RMID)==-1)
115     perror("semctl IPC_RMID");
116     else
117     printf("remove sem ok\n");
118     
119     return 0;
120 }

 

信号量

标签:

原文地址:http://www.cnblogs.com/wireless-dragon/p/5184748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!