多个生产者,多个消费者

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <math.h>

#define NBUFF   10
#define MAXNTHREADS 100

#define	min(a,b)	((a) < (b) ? (a) : (b))
#define	max(a,b)	((a) > (b) ? (a) : (b))

int nitems, nproducers, nconsumers;

struct {
    int buff[NBUFF];
    int nput;
    int nputval;
    int nget;
    int ngetval;
    sem_t   mutex, nempty, nstored;
}shared;

void    *produce(void*), *consume(void*);

int main(int argc, char** argv)
{
    int i, prodcount[MAXNTHREADS], conscount[MAXNTHREADS];
    pthread_t   tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS];

    if(argc != 4)
    {
        printf("usage: prodcons4 <#items> <#consumers>");
    }
    
    nitems = atoi(argv[1]);

    nproducers = min(atoi(argv[2]), MAXNTHREADS);
    nconsumers = min(atoi(argv[3]), MAXNTHREADS);

    sem_init(&shared.mutex, 0, 1);
    sem_init(&shared.nempty, 0, NBUFF);
    sem_init(&shared.nstored, 0, 0);

    Set_concurrency(nproducers + nconsumers);

    for(i = 0; i < nproducers; i++)
    {
        prodcount[i] = 0;
        pthread_create(&tid_produce[i], NULL, produce, &prodcount[i]);
    }

    for(i = 0; i < nconsumers; i++)
    {
        conscount[i] = i;
        pthread_create(&tid_consume[i], NULL, consume, &conscount[i]);
    }

    for(i = 0; i < nproducers; i++)
    {
        pthread_join(tid_produce[i], NULL);
        printf("producer count[%d] = %d\n", i, prodcount[i]);
    }
    for(i = 0; i < nconsumers; i++)
    {
        pthread_join(tid_consume[i], NULL);
        printf("consumer count[%d] = %d\n", i, conscount[i]);
    }

    sem_destroy(&shared.mutex);
    sem_destroy(&shared.nempty);
    sem_destroy(&shared.nstored);
}

void* produce(void* arg)
{
    for(;;)
    {
        sem_wait(&shared.nempty);
        sem_wait(&shared.mutex);
    
    
        if(shared.nput >= nitems)
        {
            sem_post(&shared.nstored);
            sem_post(&shared.nempty);
            sem_post(&shared.mutex);
            return(NULL);
        }
        shared.buff[shared.nput % NBUFF] = shared.nputval;
        shared.nput++;
        shared.nputval++;

        sem_post(&shared.mutex);
        sem_post(&shared.nstored);

        *((int*) arg) += 1;
    }
}

void* consume(void* arg)
{
    int i; 
    for(;;)
    {
        sem_wait(&shared.nstored);
        sem_wait(&shared.mutex);

        if(shared.nget >= nitems)
        {
            sem_post(&shared.nstored);
            sem_post(&shared.mutex);
            return(NULL);
        }
        i = shared.nget % NBUFF;
        if(shared.buff[i] != shared.ngetval);
        {
            printf("error: buff[%d] = %d\n", i, shared.buff[i]);
        }

        shared.nget++;
        shared.ngetval++;

        sem_post(&shared.mutex);
        sem_post(&shared.nempty);

        *((int*)arg) += 1;
    }
}
#include <stdlib.h>
#include <unistd.h>

int set_concurrency(int level)
{
#ifdef	HAVE_THR_SETCONCURRENCY_PROTO
	int		thr_setconcurrency(int);

	return(thr_setconcurrency(level));
#else
	return(0);
#endif
}

void Set_concurrency(int level)
{
	if (set_concurrency(level) != 0)
		printf("set_concurrency error");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值