⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 me_consumer.c

📁 这是在linux环境下C++做的生产者和消费者问题
💻 C
字号:
/******************************consumer.c**********************************/#include "ipc.h"int main(int arc,char *argv[]){    /**    * this producer doesn't sleep    */       int count=0;    int material;    buff_key=101;    buff_num=sizeof i;    buff_flag=IPC_CREAT|0644;    full_key=201;    empty_key=202;    pro_ex_key=301;    cons_ex_key=302;    full_value=0;    empty_value=1;    pro_ex_value=1;    cons_ex_value=1;    sem_flag=IPC_CREAT|0644;    printf("enter the material this consumer need:");    scanf("%d",&material);    buff_ptr=(int*)set_shm(buff_key,buff_num,buff_flag);       full_sem=set_sem(full_key,full_value,sem_flag);    empty_sem=set_sem(empty_key,empty_value,sem_flag);    cons_ex_sem=set_sem(cons_ex_key,cons_ex_value,sem_flag);    while(count++<8){        while(1){            sleep(1);            operationP(full_sem);            operationP(cons_ex_sem);            if(buff_ptr[0]==material){                printf("costomer:%d getting %d\n",getpid(),material);                operationV(cons_ex_sem);                printf("costomer:%d signal empty\n",getpid());                operationV(empty_sem);                break;                                 }            operationV(full_sem);            operationV(cons_ex_sem);                    }         }} /*************con.c end**********************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -