生产者消费者问题程序解决

 代码仓库:JJLi0427/operationsystem (github.com)

1.问题描述:

有一个货架,如果有空间的话生产者生产的产品可以放到其中,然后消费者可以在上面选择已有商品购买取走

2.解决思路:

这个问题需要思考以下几个点:同一时刻只能一人访问缓冲区,缓冲区空间大小是固定的,生产者放入产品空间减少,消费者取走产品空间增加。于是可以设置一个mutex互斥信号,实现同一时刻只能一人访问缓冲区。对于缓冲区的空间,我们设置两个信号量,从两个角度看待。应该是space信号量,这个表示的是剩余的缓冲区大小,消费者放入物品会对他进行p操作,消费者取走物品会对他进行v操作,若space为空则生产者会被阻塞。另外一个是full信号量,表示已尽有物品的缓冲区数量,作用和space是相反的。

3.程序实现:

c++实现如下,编译时需加上 -lpthread

#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
using namespace std;

sem_t space, full, mutex; //space是缓冲区的信号量,full是已使用的缓冲区,mutex访问缓冲区信号量
typedef struct ThreadData {
    int id;
    int setup;
    int exe;
} tdata; //线程的数据结构

void* producer(void* p) {
    int id = ((tdata*)p)->id;
    int setup = ((tdata*)p)->setup;
    int exe = ((tdata*)p)->exe;

    sleep(setup);
    printf("thread %d: wait to produce\n", id);

    sem_wait(&space); //等待缓冲区有空位
    sem_wait(&mutex); //访问缓冲区

    printf("thread %d: start producing\n", id);
    sleep(exe);
    printf("thread %d: end producing\n", id);

    sem_post(&mutex);
    sem_post(&full);

    pthread_exit(0);
    return NULL;
}

void* consumer(void* p) {
    int id = ((tdata*)p)->id;
    int setup = ((tdata*)p)->setup;
    int exe = ((tdata*)p)->exe;

    sleep(setup);
    printf("thread %d: wait to consume\n", id);

    sem_wait(&full);
    sem_wait(&mutex);

    printf("thread %d: start consuming\n", id);
    sleep(exe);
    printf("thread %d: end consuming\n", id);

    sem_post(&mutex);
    sem_post(&space);

    pthread_exit(0);
    return NULL;
}

int main() {
    int num = 6; //设置生产者和消费者的数量
    pthread_t tid[num]; //线程号初始化
    pthread_attr_t attr;
    pthread_attr_init(&attr); //线程属性初始化  
    sem_init(&mutex, 0, 1);
    sem_init(&space, 0, 3);
    sem_init(&full, 0, 0);

    char worklist[num] = "ccpcpp";
    int setuplist[num] = {2, 3, 4, 3, 7, 5};
    int exelist[num] = {1, 3, 2, 8, 6, 1}; //设置不同的生产者和消费者的等待时间和执行时间
    for(int id = 0; id < num; id++) {
        tdata* d = new tdata;
        d->id = id;
        d->setup = setuplist[id];
        d->exe = exelist[id];

        if(worklist[id] == 'c') {
            printf("create thread %d: consumer\n", id);
            pthread_create(&tid[id], &attr, consumer, d); //创建消费者线程
        }
        else if(worklist[id] == 'p') {
            printf("create thread %d: producer\n", id);
            pthread_create(&tid[id], &attr, producer, d); //创建生产者线程
        }
    }
    for(int i = 0; i < num; i++) {
        pthread_join(tid[i], NULL);
    }

    sem_destroy(&mutex); //销毁信号量
    sem_destroy(&space);
    sem_destroy(&full);
    return 0;
}

4.运行结果:

create thread 0: consumer
create thread 1: consumer
create thread 2: producer
create thread 3: consumer
create thread 4: producer
create thread 5: producer
thread 0: wait to consume
thread 0: start consuming
thread 1: wait to consume
thread 1: start consuming
thread 0: end consuming
thread 3: wait to consume
thread 3: start consuming
thread 2: wait to produce
thread 2: start producing
thread 5: wait to produce
thread 5: start producing
thread 5: end producing
thread 2: end producing
thread 1: end consuming
thread 4: wait to produce
thread 4: start producing
thread 3: end consuming
thread 4: end producing

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哆啦叮当

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值