消费者与生产者

概念简介

                缓冲区:存放生产者产生的数据,消费者提取数据。

                生产者:产生数据。

                消费者:消耗数据。

                生产与消费关系:当缓冲区达到最高预值,停止生产加快消耗,反之,则加快生产,停止

                消耗。

demo:

                1.定义字符串数组(大小20)作为缓冲区,字符#为货物。

                2.定义线程t1为生产者,t2为消费者,t3发送信号。

                3.数组大小为15,线程t1停止,数组为0,t2停止。

代码示例:

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

char goods[20] = {'\0'};
pthread_mutex_t signal;
pthread_mutex_t mutex;
pthread_cond_t cond1;
pthread_cond_t cond2;

int goods_count(char goods[])
{
        int i;
        int num = 0;
        for(i = 0;i < 20;i++)
        {
                if(goods[i] == '#')
                {
                        num++;
                }

        }
        return num;
}

void goods_adjust(char goods[])
{
        int i;
        if(goods_count(goods) > 0)
        {
                while(goods[0] == '\0')
                {
                        for(i = 0;i < 18;i++)
                        {
                                goods[i] = goods[i+1];
                        }
                }
                goods[19] = '\0';
        }
}

void consumer(char goods[],int num)
{
        int i;
        int output;
        output = rand()%10/3;
        if(output > 0)
        {
                for(i = 0;i < output;i++)
                {
                        goods[i] = '\0';
                }
                goods_adjust(goods);
                printf("output : %d ,%s\n",output,goods);
        }
}

void product(char goods[],int num)
{
        int i;
        int input;
        input = rand()%10/3;
        if(input > 0)
        {
                for(i = 0;i < input;i++)
                {
                        goods[num+i] = '#';
                }
                printf("input : %d ,%s\n",input,goods);
        }
}

void *func1()
{
        while(1)
        {
                pthread_cond_wait(&cond1,&mutex);
                int num = goods_count(goods);
                product(goods,num);
                sleep(1);
        }
        pthread_exit(NULL);
}

void *func2()
{
        while(1)
        {
                pthread_cond_wait(&cond2,&mutex);
                int num = goods_count(goods);
                consumer(goods,num);
                sleep(1);
        }
        pthread_exit(NULL);
}

void *func3()
{
        while(1)
        {
        pthread_mutex_lock(&signal);
        int num = goods_count(goods);
        if(num >= 5 && num <= 15)
        {
                pthread_cond_signal(&cond1);
                pthread_cond_signal(&cond2);
        }
        else if(num < 5)
        {
                pthread_cond_signal(&cond1);
        }
        else if( num > 15)
        {
                pthread_mutex_lock(&mutex);
                pthread_mutex_unlock(&mutex);
                pthread_cond_signal(&cond2);
        }
        else
        {
                perror("error");
                exit(-1);
        }
        pthread_mutex_unlock(&signal);
        }
        pthread_exit(NULL);
}

int main()
{
        int num;
        pthread_t t1;
        pthread_t t2;
        pthread_t t3;
        pthread_mutex_init(&signal,NULL);
        pthread_mutex_init(&mutex,NULL);
        pthread_cond_init(&cond1,NULL);
        pthread_cond_init(&cond2,NULL);
        pthread_create(&t1,NULL,func1,NULL);
        pthread_create(&t2,NULL,func2,NULL);
        pthread_create(&t3,NULL,func3,NULL);
        pthread_join(t1,NULL);
        pthread_join(t2,NULL);
        pthread_join(t3,NULL);
        pthread_mutex_destroy(&signal);
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond1);
        pthread_cond_destroy(&cond2);
        return 0;
}

 结果示例:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值