1、一个读取设置信号量集合的示例程序 code: #include<stdio.h> #include<sys/types.h> #include<sys/sem.h> #include<errno.h> #define MAX_SEMAPHORES 5 int main() { union semun // 参数联合体 { int val; struct semid_ds * buf; unsigned short * array; //for GETALL and SETALL } arg; int semid=semget(IPC_PRIVATE,MAX_SEMAPHORES,IPC_CREAT|0666); //创建信号量集合 if(semid!=-1){ unsigned short sem_array[MAX_SEMAPHORES]; int i; for(i=0;i<MAX_SEMAPHORES;i++) sem_array[i]=i+1; arg.array=sem_array; int ret=semctl(semid,0,SETALL,arg); //设置信号量-全部 if(ret==-1) printf("failed %d/n",errno); unsigned short sem_read_array[MAX_SEMAPHORES]; arg.array=sem_read_array; ret=semctl(semid,0,GETALL,arg); //读取信号量-全部 if(ret==-1) printf("failed %d/n",errno); for(i=0;i<MAX_SEMAPHORES;i++) printf("Semaphore %d, value %d/n",i,sem_read_array[i]); for(i=0;i<MAX_SEMAPHORES;i++) { ret=semctl(semid,i,GETVAL); //读取信号量-单个 printf("Semaphore %d, value %d/n",i,ret); } ret=semctl(semid,0,IPC_RMID); //删除信号量集合 } else printf("Could not allocate semaphore %d/n",errno); return 0; } 结果: root@ubuntu:/code/chap9# ./run3 Semaphore 0, value 1 Semaphore 1, value 2 Semaphore 2, value 3 Semaphore 3, value 4 Semaphore 4, value 5 Semaphore 0, value 1 Semaphore 1, value 2 Semaphore 2, value 3 Semaphore 3, value 4 Semaphore 4, value 5 2、使用信号量 实现生产者-消费者问题 code: 生产者: root@ubuntu:/code/chap9# cat producer.c #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<errno.h> int sem_id; void init() //建立信号量集合 { key_t key; int ret; key=ftok(".",'s'); sem_id=semget(key,2,IPC_CREAT|0644); union semun { int val; struct semid_ds * buf; unsigned short * array; }arg; unsigned short sem_array[2]={0,100}; arg.array=sem_array; ret=semctl(sem_id,0,SETALL,arg); //初始化信号量,2个信号量,produt、space if(ret==-1) printf("setall failed %d/n",errno); printf("product init is %d/n",semctl(sem_id,0,GETVAL)); printf("space init is %d/n",semctl(sem_id,1,GETVAL)); } int main() { //操作两个信号量使用的结构体 struct sembuf sop[2]; sop[0].sem_num=0; //执行加1操作,每生产一个产品,对 product 加1 sop[0].sem_op=1; sop[0].sem_flg=0; sop[1].sem_num=1; //执行减1操作,每生产一个产品,对 space 减1 sop[1].sem_op=-1; sop[1].sem_flg=0; init(); printf("this is productor/n"); int count=1; while(1) { printf("stage %d/nbefore produce:/n",count++); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n",semctl(sem_id,1,GETVAL)); semop(sem_id,(struct sembuf*)&sop[1],1); //get the space printf("now producing.../n"); semop(sem_id,(struct sembuf*)&sop[0],1); //put the product printf("after produce:/n"); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n/n",semctl(sem_id,1,GETVAL)); sleep(4); } return 0; } 消费者: root@ubuntu:/code/chap9# cat customer.c #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<errno.h> int sem_id; void init() { key_t key; key=ftok(".",'s'); sem_id=semget(key,2,IPC_CREAT|0644); } int main() { init(); struct sembuf sop[2]; sop[0].sem_num=0; sop[0].sem_op=-1; sop[0].sem_flg=0; sop[1].sem_num=1; sop[1].sem_op=1; sop[1].sem_flg=0; printf("this is customer/n"); int count=1; while(1) { printf("stage %d:/nbefore sonsume:/n",count++); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n",semctl(sem_id,1,GETVAL)); semop(sem_id,(struct sembuf*)&sop[0],1); //get the product printf("now consuming.../n"); semop(sem_id,(struct sembuf*)&sop[1],1); //increase the space printf("after consume/n"); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n/n",semctl(sem_id,1,GETVAL)); sleep(3); } } 结果: 生产者: stage 3 before produce: product is 2 space is 98 now producing... after produce: product is 3 space is 97 stage 4 before produce: product is 3 space is 97 now producing... after produce: product is 4 space is 96 消费者: stage 2: before sonsume: product is 9 space is 91 now consuming... after consume product is 8 space is 92 stage 3: before sonsume: product is 9 space is 91 now consuming... after consume product is 8 space is 92