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

📄 all.txt

📁 这是在linux环境下C++做的生产者和消费者问题
💻 TXT
字号:
/**抽烟问题:材料区:一个共享内存单元—material,保存提供的材料;供应者填充,抽烟者消费;供应者靠       empty,抽烟者靠full实现同步;供应者:通过一共享内存保存上次提供的材料,以循环提供,供应者之间靠pro-multex互斥;抽烟者:不停查询material中是否是自己所需的材料,如果是唤醒供应者,如果不是则继续等待;       抽烟者靠cons-multex互斥;信号量初值:       empty=1,full=0,pro-multex=1,cons-multex=1;*//*******************************ipc.h**********************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/sem.h>#include <sys/msg.h>#define BUFSZ 256/** the functions to get the shared memary and the semaphore*/int get_ipc_id(char *proc_file,key_t key);char *set_shm(key_t shm_key,int shm_num,int shm_flag);int set_sem(key_t sem_key,int sem_val,int sem_flag);int operationP(int sem_id);int operationV(int sem_id);/** semaphore control union*/typedef union semuns{    int val;}Sem_uns;typedef struct msgbuf{    long mtype;    char *mtext;} Msg_buf;/*** the variables needed by the producers and the consumers*/key_t buff_key;/*material's key*/int i;int buff_num;int *buff_ptr;int buff_flag;key_t full_key;key_t empty_key;key_t pro_ex_key;key_t cons_ex_key;int full_value;int empty_value;int pro_ex_value;int cons_ex_value;int full_sem;int empty_sem;int pro_ex_sem;int cons_ex_sem;int sem_flag;/*********************ipc.h end******************//*******************************ipc.c*******************************************/#include "ipc.h"int get_ipc_id(char *proc_file,key_t key){    FILE *pf;    int i,j;    char line[BUFSZ],colum[BUFSZ];    if((pf=fopen(proc_file,"r"))==NULL){        perror("proc file not open");        exit(EXIT_FAILURE);    }    /*fgets(line,BUFSZ,pf);*/    while(!feof(pf)){        i=j=0;        fgets(line,BUFSZ,pf);        while(line[i]==' ')i++;        while(line[i]!=' ')colum[j++]=line[i++];        colum[j]='\0';        if(atoi(colum)!=key)continue;        j=0;        while(line[i]==' ')i++;        while(line[i]!=' ')colum[j++]=line[i++];        colum[j]='\0';        i=atoi(colum);        fclose(pf);        return i;    }    fclose(pf);    return -1;}int operationP(int sem_id){    struct sembuf buf;    buf.sem_op=-1;    buf.sem_num=0;    buf.sem_flg=600;    if((semop(sem_id,&buf,1))<0){        perror("operationP error");exit(EXIT_FAILURE);    }    return EXIT_SUCCESS;}int operationV(int sem_id){    struct sembuf buf;    buf.sem_op=1;    buf.sem_num=0;    buf.sem_flg=600;    if((semop(sem_id,&buf,1))<0){        perror("operationV error");exit(EXIT_FAILURE);    }    return EXIT_SUCCESS;}int set_sem(key_t sem_key,int sem_val,int sem_flg){    int sem_id;    Sem_uns sem_arg;    if((sem_id=get_ipc_id("/proc/sysvipc/sem",sem_key))<0){        /* to create a new semaphore */        if((sem_id=semget(sem_key,1,sem_flg))<0){            perror("semaphore create error");exit(EXIT_FAILURE);        }        sem_arg.val=sem_val;        if(semctl(sem_id,0,SETVAL,sem_arg)<0){            perror("semaphore set error");exit(EXIT_FAILURE);        }    }    return sem_id;}char *set_shm(key_t shm_key,int shm_num,int shm_flg){    int i,shm_id;    char *shm_buf;    if((shm_id=get_ipc_id("/proc/sysvipc/shm",shm_key))<0){        /* to create a new shared memary */        if((shm_id=shmget(shm_key,shm_num,shm_flg))<0){            perror("shared memary create error");exit(EXIT_FAILURE);        }        if((shm_buf=(char*)shmat(shm_id,0,0))<(char*)0){            perror("sharedmemary get error");exit(EXIT_FAILURE);        }        for(i=0;i<shm_num;i++)shm_buf[i]=0;    }    if((shm_buf=(char*)shmat(shm_id,0,0))<(char*)0){        perror("sharedmemary get error");exit(EXIT_FAILURE);    }    return shm_buf;}/*****************ipc.c end*******************//******************************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**********************//******************************producer.c**********************************/#include "ipc.h"int main(int arc,char *argv[]){key_t buff_key=101;/*material's key*/    int count=0;    buff_num=sizeof i;    buff_flag=IPC_CREAT|0644;    buff_key=101;    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;    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);    pro_ex_sem=set_sem(pro_ex_key,pro_ex_value,sem_flag);    while(count++<12){        sleep(2);        operationP(empty_sem);        operationP(pro_ex_sem);        printf("producer %d sending %d\n",getpid(),((buff_ptr[0]+1)%3));        buff_ptr[0]=(buff_ptr[0]+1)%3;        operationV(full_sem);        operationV(pro_ex_sem);         }} 

⌨️ 快捷键说明

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