生产者-消费者(计数)

2019-05-22  本文已影响0人  织田茄子

计数信号量相对互斥锁 的优点:

1,不会出现反复无效解锁,信号量就代表实际的产品空间数量,而不是不管三七二十一先上锁再判断;
2,提高了效率,只要一方一提供资源,另一方马上就可以解锁,响应度更高

1 有两个信号量,要用setall,不是setval

        int ret =semctl(semid,0,SETALL,a);//xx1

2 要while循环不然走一轮就结束了;

另外,还有个容易错的,是信号量的数值是unsigned short不是int!!!

 unsigned short a[2]={10,0};

3 被坑最多的,就是信号量结构体写错。要么写重复了,要么就是格式不对,特别是写重了,用接口判断不出bug

                sopv.sem_num= 0;//xx3  not sopp.sem_num
                sopv.sem_op = 1;//xx3 not sopv.op   not sopv.sem_num

4 sleep1,2,不能反了,不然打印的结果会很懵逼,因为产品多,那就消费者多一点


```
#include <func.h>

int main(){
        int semid=semget(10,2,0600|IPC_CREAT);
        ERROR_CHECK(semid,-1,"semget");
        unsigned short a[2]={10,0};// 0 for food 1 for space
        int ret =semctl(semid,0,SETALL,a);//xx1
        ERROR_CHECK(ret,-1,"semctl");
        if(!fork()){//cus
                struct sembuf sopp,sopv;
                sopp.sem_num = 0;
                sopp.sem_op = -1;
                sopp.sem_flg = SEM_UNDO;
                sopv.sem_num = 1;
                sopv.sem_op = 1;
                sopv.sem_flg = SEM_UNDO;
                while(1){//xx2
                        printf("i am customer ,food :%d , space : %d \n",semctl(semid,0,GETVAL),semctl(semid,1,GETVAL));
                        ret =   semop(semid,&sopp,1);
                        ERROR_CHECK(ret,-1,"semop");
                        printf("eatting..\n");
                        ret =   semop(semid,&sopv,1);
                        ERROR_CHECK(ret,-1,"semop");
                        printf("i am customer ,food :%d , space : %d \n",semctl(semid,0,GETVAL),semctl(semid,1,GETVAL));
                        sleep(1);//xx4
                }
        }else{
                struct sembuf sopp,sopv;
                sopp.sem_num = 1;
                sopp.sem_op = -1;
                sopp.sem_flg = SEM_UNDO;
                sopv.sem_num= 0;//xx3  not sopp.sem_num
                sopv.sem_op = 1;//xx3 not sopv.op   not sopv.sem_num
                sopv.sem_flg = SEM_UNDO;
                while(1){
                        printf("i am producer,food:%d,spce :%d\n",semctl(semid,0,GETVAL),semctl(semid,1,GETVAL));
                        ret =   semop(semid,&sopp,1);
                        ERROR_CHECK(ret,-1,"semop");
                        printf("i am producing .. \n");
                        ret =   semop(semid,&sopv,1);
                        ERROR_CHECK(ret,-1,"semop");
                        printf("i am producer ,food :%d , space : %d \n",semctl(semid,0,GETVAL),semctl(semid,1,GETVAL));
                        sleep(2);//xx4
                }
                wait(NULL);
        }
        return 0;
}
```
上一篇下一篇

猜你喜欢

热点阅读