操作系统实验四、进程同步实验——抽烟者问题

抽烟者问题

问题描述

假设一个系统中有三个抽烟者进程,每个抽烟者不断地卷烟并抽烟。抽烟者卷起并抽掉一颗烟需要有三种材料:烟草、纸和胶水。一个抽烟者有烟草,一个有纸,另一个有胶水。系统中还有两个供应者进程,它们无限地供应所有三种材料,但每次仅轮流提供三种材料中的两种。得到缺失的两种材料的抽烟者在卷起并抽掉一颗烟后会发信号通知供应者,让它继续提供另外的两种材料。这一过程重复进行。 请用以上介绍的 IPC 同步机制编程,实现该问题要求的功能。

代码实现

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    //建立或获取 ipc 的一组函数的原型说明

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_msq(key_t msq_key,int msq_flag);
int set_sem(key_t sem_key,int sem_val,int sem_flag);
int down(int sem_id);
int up(int sem_id);

/*信号灯控制用的共同体*/
typedef union semuns {
    int val;
} Sem_uns;

/* 消息结构体*/
typedef struct msgbuf {
    long mtype;
    char mtext[1];
} Msg_buf;

//生产消费者共享缓冲区即其有关的变量
key_t buff_key;  int buff_num;  char *buff_ptr;

//生产者放产品位置的共享指针
key_t pput_key;  int pput_num;
int *pput_ptr;

//消费者取产品位置的共享指针
key_t cget_key;
int cget_num;
int *cget_ptr;
int buff_num;
int pput_num;
int buff_number;
int pput_number;
int cget_number;

key_t pput_h;
key_t prod_h;
key_t pmtx_h;
key_t cons_h;
key_t cmtx_h;

key_t buff_h;
key_t cget_h;

//生产者有关的信号量
key_t prod_key;
key_t pmtx_key;
int prod_sem;
int pmtx_sem;

//消费者有关的信号量
key_t cons_key;
key_t cmtx_key;
int cons_sem;
int cmtx_sem;
int sem_val;
int sem_flg;
int shm_flg;


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 down(int sem_id) {
    struct sembuf buf;
    buf.sem_op = -1;
    buf.sem_num = 0;
    buf.sem_flg = SEM_UNDO;
    if((semop(sem_id,&buf,1)) <0) {
        perror("down error ");
        exit(EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}

int up(int sem_id) {
    struct sembuf buf;
    buf.sem_op = 1;
    buf.sem_num = 0;
    buf.sem_flg = SEM_UNDO;
    if((semop(sem_id,&buf,1)) <0) {
        perror("up 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;

    //测试由 sem_key 标识的信号灯数组是否已经建立
    if((sem_id = get_ipc_id("/proc/sysvipc/sem",sem_key)) < 0 ){
        //semget 新建一个信号灯,其标号返回到 sem_id
        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;

    //测试由 shm_key 标识的共享内存区是否已经建立
    if((shm_id = get_ipc_id("/proc/sysvipc/shm",shm_key)) < 0 ){
        //shmget 新建 一个长度为 shm_num 字节的共享内存,其标号返回到 shm_id
        if((shm_id = shmget(shm_key,shm_num,shm_flg)) <0){
            perror("shareMemory set error");
        exit(EXIT_FAILURE);
    }
    //shmat 将由 shm_id 标识的共享内存附加给指针 shm_buf
    if((shm_buf = (char *)shmat(shm_id,0,0)) < (char *)0){
        perror("get shareMemory error");
        exit(EXIT_FAILURE);
    }
    for(i=0; i<shm_num; i++) shm_buf[i] = 0;
        //初始为 0
    }
    //shm_key 标识的共享内存区已经建立,将由 shm_id 标识的共享内存附加给指 针 shm_buf
    if((shm_buf = (char *)shmat(shm_id,0,0)) < (char *)0){
        perror("get shareMemory error");
        exit(EXIT_FAILURE);
    }
return shm_buf;
}

int set_msq(key_t msq_key,int msq_flg){
    int msq_id;
    //测试由 msq_key 标识的消息队列是否已经建立
    if((msq_id = get_ipc_id("/proc/sysvipc/msg",msq_key)) < 0 ){
            //msgget 新建一个消息队列,其标号返回到 msq_id
        if((msq_id = msgget(msq_key,msq_flg)) < 0){
            perror("messageQueue set error");
        exit(EXIT_FAILURE);
        }
    }
    return msq_id;

producer.c

#include "ipc.h"
int main(int argc,char *argv[]){
	int rate=3;
	int producerid=atoi(argv[1]);
	buff_h=101;
	buff_number=8;
	pput_h=102;
	pput_number=1;
	shm_flg=IPC_CREAT|0644;
	buff_ptr = (char *)set_shm(buff_h,buff_number,shm_flg);
    pput_ptr = (int *)set_shm(pput_h,pput_number,shm_flg);
	prod_h = 201;
	pmtx_h = 202;
	cons_h = 301;
	cmtx_h = 302;
	sem_flg = IPC_CREAT|0644;
	sem_val = buff_number;
	prod_sem = set_sem(prod_h,sem_val,sem_flg);
	sem_val = 0;
	cons_sem = set_sem(cons_h,sem_val,sem_flg);
	sem_val = 1;
	pmtx_sem = set_sem(pmtx_h,sem_val,sem_flg);
	if(producerid==0){
		buff_ptr[0]='D';
		*pput_ptr=0;
	}
	while(1){
		if(buff_ptr[0]-'D'==producerid){
			down(prod_sem);
			down(pmtx_sem);
			*pput_ptr = (*pput_ptr+1)%3;
			if(*pput_ptr==0){
				buff_ptr[0] = 'A';
				printf("%d The producer gives tobacco and paper\n",getpid());
			}
			if(*pput_ptr==1){
				buff_ptr[0] = 'B';
				printf("%d The producer gives tobacco and glue\n",getpid());
			}
			if(*pput_ptr==2){
				buff_ptr[0] = 'C';
				printf("%d The producer gives glue and paper\n",getpid());
			}
			sleep(rate);
			up(pmtx_sem);
			up(cons_sem);
		}
	}
	return EXIT_SUCCESS;
}


consumer.c

#include "ipc.h"
int main(int argc,char *argv[]) {
	int rate = 3;
	int consumerid=atoi(argv[1]);
	buff_h = 101;
	buff_number = 8;
	cget_h = 103;
	cget_number = 1;
	shm_flg = IPC_CREAT | 0644;
	buff_ptr = (char *)set_shm(buff_h,buff_number,shm_flg);
	cget_ptr = (int *)set_shm(cget_h,cget_number,shm_flg);
	prod_h = 201;
	pmtx_h = 202;
	cons_h = 301;
	cmtx_h = 302;
    sem_flg = IPC_CREAT | 0644;
	sem_val = buff_number;
	prod_sem = set_sem(prod_h,sem_val,sem_flg);
	sem_val = 0;
	cons_sem = set_sem(cons_h,sem_val,sem_flg);
	sem_val = 1;
	cmtx_sem = set_sem(cmtx_h,sem_val,sem_flg);
	if(consumerid==0)
		*cget_ptr=0;
	while(1){
		if(buff_ptr[0]-'A'==consumerid){
			down(cons_sem);
			down(cmtx_sem);
			sleep(rate);
			if(buff_ptr[0]=='A'){
				printf("%d The consumer has glue.\nThe consumer gets tobacco and paper\n",getpid());
			}
			if(buff_ptr[0]=='B'){
				printf("%d The consumer has paper.\nThe consumer gets tobacco and glue\n",getpid());
			}
            if(buff_ptr[0]=='C'){
				printf("%d The consumer has tobacco.\nThe consumer gets glue and paper\n",getpid());
			}
			*cget_ptr = (*cget_ptr+1);
			if(*cget_ptr%2==0)
				buff_ptr[0]='D';
			else
				buff_ptr[0]='E';
			up(cmtx_sem);
			up(prod_sem);
		}
	}
	return EXIT_SUCCESS;
}


编译

gcc -g -c producer.c ipc.c
gcc Producer.o ipc.o -o producer
gcc -g -c Customer.c ipc.c
gcc Customer.o ipc.o -o customer

运行
在当前终端窗体中启动一个生产者进程
打开另一终端窗体,进入当前工作目录,从中再启动另一执行速率为 1的生产者进程:
再打开另外两个终端窗体,进入当前工作目录,从中启动执行速率为 2 和 4 的两个消费者进程:

./producer 0
./producer 1
./consumer 1
./consumer 2
./consumer 3

运行截图
在这里插入图片描述

©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页