生产者消费者的进程同步——1对1,N对1,N对M —— C语言

1、1个生产者和1个消费者

/* ***1个生产者和1个消费者*** */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>   
#include <semaphore.h>
#include <time.h>

int n=10;
int buffer[10];
int in=0,out=0;
sem_t empty,full;  //信号量

/* ***生产者*** */
void *producer(void *arg)
{
	int tag= pthread_self()%100;  //获取线程号
    int nextPro;

    srand(time(NULL)+tag);  //产生随机数
    while(1){  //无限生产
        nextPro = rand()%97;  //用一个伪随机数表示产品号
        sem_wait(&empty);  //信号量减1,信号量为0调用时阻塞
		//sem_wait(&mutex);
        buffer[in] = nextPro; 
        in = (in+1)%n;
		printf("production:%d\n",nextPro);
        sem_post(&full);  //信号量加1
		//sem_post(&mutex);
        
        usleep(1000*1000/2);  //睡眠:单位是微秒,1秒=1000*1000微秒,除以2表示睡眠0.5秒
    }
} 

/* ***消费者*** */
void *consumer(void *arg)  
{
    int item;

    while(1){
        sem_wait(&full);
	//	sem_wait(&mutex);
        item = buffer[out];
        sem_post(&empty);
	//	sem_post(&mutex);
        printf("consume:%d\n",buffer[out]);
        out = (out+1)%n;
        usleep(1000*1000/2);
    }
}

int main()  
{  
	pthread_t tid[2];  //线程号数组
    
	sem_init( &empty, 0,10);  //整型信号量对象初始化,'0':为当前进程的所有线程共享,大小为10个缓冲区
    sem_init( &full, 0,0);
	//sem_init( &mutex, 0, 1);
    pthread_create( &tid[0], NULL, producer, NULL);  //创建线程,即指出线程入口
	pthread_create( &tid[1], NULL, consumer, NULL);  

	for(int i = 0; i < 2; i++){  //等待所有线程结束,若不然,main结束后其他线程将被终止
		pthread_join(tid[i], NULL);
	}
	printf("main is over\n");
} 

执行结果:
在这里插入图片描述

2、N个生产者和1个消费者

/* ***N个生产者和1个消费者*** */
#include <stdio.h>   
#include <stdlib.h>   
#include <pthread.h>   
#include <unistd.h>   
#include <sys/types.h>   
#include <semaphore.h>
#include <time.h>  

int N=2;  //N个生产者
int n=10;  //缓冲区数目
int buffer[10];  //存放产品的缓冲区数组
int in=0,out=0;  //in生产者放产品的位置,out消费者取产品的位置
sem_t empty,full,mutex;  //信号量

/* ***生产者*** */
void *producer(void *arg)
{
	int tag= pthread_self()%100;  //获取线程号
    int nextPro;

    srand(time(NULL)+tag);  //产生随机数
    while(1){  //无限生产
		nextPro = rand()%97;  //用一个伪随机数表示产品号
		sem_wait(&empty);  //信号量减1,信号量为0调用时阻塞
		sem_wait(&mutex);
		buffer[in] = nextPro;
		in = (in+1)%n;
		printf("production%d:%d\n",tag,nextPro);
		sem_post(&full);  //信号量加1
		sem_post(&mutex);
		usleep(1000*1000/2);  //睡眠:单位是微秒,1秒=1000*1000微秒,除以2表示睡眠0.5秒
	}
} 

/* ***消费者*** */
void *consumer(void *arg)  
{
    int item;
	int tagc= pthread_self()%100;  //获取线程号

    while(1){
        sem_wait(&full);
		sem_wait(&mutex);
        item = buffer[out];//设置item变量,将产品取出
		printf("consume%d:%d\n",tagc,item);
        out = (out+1)%n;
		sem_post(&empty);
		sem_post(&mutex);
        usleep(1000*1000/4);
    }
}

int main()  
{  
	pthread_t tid_p[N];  //生产者线程数组
	pthread_t tid_c[1];  //消费者线程数组
    
	sem_init( &empty, 0, 10);  //整型信号量对象初始化,为当前进程的所有线程共享,大小为10个缓冲区
    sem_init( &full, 0, 0);
	sem_init( &mutex, 0, 1);
	for( int i=0; i<N; i++){

		pthread_create( &tid_p[i], NULL, producer, NULL);  //创建生产者线程

	}
	pthread_create( &tid_c[0], NULL, consumer, NULL);  //创建消费者进程
	for( int i=0; i<=N; i++){  //等待所有线程结束,若不然,main结束后其他线程将被终止
	
		pthread_join(tid_p[i], NULL);

	}
	pthread_join(tid_c[0], NULL);

	printf("main is over\n");
}

执行结果:
在这里插入图片描述

3、N个生产者和M个消费者

/* ***N个生产者和M个消费者*** */
#include <stdio.h>   
#include <stdlib.h>   
#include <pthread.h>   
#include <unistd.h>   
#include <sys/types.h>   
#include <semaphore.h>   

int N=3;  //N个生产者
int M=2;  //M个消费者
int n=10;  //缓冲区数目
int buffer[10];  //存放产品的缓冲区数组
int in=0,out=0;  //in生产者放产品的位置,out消费者取产品的位置
sem_t empty,full,mutex;  //信号量

/* ***生产者*** */
void *producer(void *arg)
{
	int tag= pthread_self()%100;  //获取线程号
    int nextPro;

    srand(time(NULL)+tag);  //产生随机数
    while(1){  //无限生产
		nextPro = rand()%97;  //用一个伪随机数表示产品号
		sem_wait(&empty);  //信号量减1,信号量为0调用时阻塞
		sem_wait(&mutex);
		buffer[in] = nextPro;
		in = (in+1)%n;
		printf("production %d: %d\n",tag,nextPro);
		sem_post(&full);  //信号量加1
		sem_post(&mutex);
		usleep(1000*1000/2);  //睡眠:单位是微秒,1秒=1000*1000微秒,除以2表示睡眠0.5秒
	}
}

/* ***消费者*** */
void *consumer(void *arg)  
{
    int tagc= pthread_self()%100;
	int item;

    while(1){
        sem_wait(&full);
		sem_wait(&mutex);
        item = buffer[out];//设置item变量,先将产品取出暂存,立即使信号量加1,使生产者线程立刻执行,利于提高多线程系统效率
        printf("consume %d: %d\n",tagc,item);
        out = (out+1)%n;
		sem_post(&empty);
		sem_post(&mutex);
        usleep(1000*1000/10);
    }
}

int main()  
{
	pthread_t tid_p[N];  //生产者线程数组
	pthread_t tid_c[M];  //消费者线程数组
    
	sem_init( &empty, 0, 10);  //整型信号量对象初始化,为当前进程的所有线程共享,大小为10个缓冲区
    sem_init( &full, 0, 0);
	sem_init( &mutex, 0, 1);
	for( int i=0; i<N; i++){

		pthread_create( &tid_p[i], NULL, producer, NULL);  //创建线程,即指出线程入口

	}
	for( int j=0; j<M; j++){

		pthread_create( &tid_c[j], NULL, consumer, NULL);  
	}
	for( int i=0; i<=N; i++){  //等待所有线程结束,若不然,main结束后其他线程将被终止
	
		pthread_join(tid_p[i], NULL);

	}
	for( int j=0; j<M; j++){

		pthread_join(tid_c[0], NULL);
	}
	printf("main is over\n");
}

执行结果:
在这里插入图片描述

调试分析:
在调试中,为了观察每个线程的各自执行情况以及验证线程之间的轮转方式是否存在问题,在生产者与消费者线程中都增加了让其输出自己线程号的代码。
在N个生产者或N个消费者时,试验了只用两个信号量empty和full的方法,发现存在消费者消费了下一步即将生产但此时未产出的产品号,分析empty和full只实现了线程间的同步问题,但未实现产品缓冲区的互斥访问。由此增加了互斥信号量mutex,并调试显示正常。

重点扩展:

在生产者消费者问题中,应先执行对资源信号量empty,full的wait操作,然后再执行对互斥信号量mutex的wait操作,否则会引起死锁。因为生产者若先进行wait(mutex)操作对mutex减1,再进行wait(empty)时当缓冲区已满,该生产者线程阻塞,此后mutex为0,以至所有生产者和消费者线程都将阻塞,形成死锁。消费者时亦然。

总结

生产者和消费者线程间存在着同步和互斥的复杂关系,同步是线程间的直接制约问题,互斥是线程间的间接制约问题,在N个生产者和M个消费者问题中,生产者和消费者都要实现对产品缓冲区的同步以保证某一线程引起产品数量变化的实时监测,又要实现线程对缓冲区的互斥访问。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Whitemeen太白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值