一、代码(修改前)

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>

#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;

void *producer ( void * arg )
{
	static int p = 0;

	for ( ;; ) {
		sem_wait( &blank_number );
		queue[p] = rand() % 1000;
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;
		sleep ( rand() % 5);
		sem_post( &product_number );
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;
		sleep( rand() % 5 );
		sem_post( &blank_number );
	}
}

int main(int argc, char *argv[] )
{
	pthread_t pid, cid;

	sem_init( &blank_number, 0, NUM );
	sem_init( &product_number, 0, 0);
	pthread_create( &pid, NULL, producer, NULL);
	pthread_create( &cid, NULL, consumer, NULL);
	pthread_join( pid, NULL );
	pthread_join( cid, NULL );
	sem_destroy( &blank_number );
	sem_destroy( &product_number );
	return 0;
}

编译运行

image
image

2.代码(修改后)

同步资源个数为3个,使用资源的线程 (20201305%3+4)=5 个,即消费者线程增加到个数为5,且需要互斥地去访问产品(最多为三个),1个生产者线程,5个消费者线程。

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

#define NUM 3//资源的数量为3
int queue[NUM];
sem_t blank_number, product_number,mutex;//设置缓冲区空白位置数量、缓冲区产品数量、互斥信号量

void *producer ( void * arg )
{
	static int p = 0;

	for ( ;; ) {
		sem_wait( &blank_number );//是否对生产者阻塞
		sem_wait( &mutex);//占用互斥信号量,互斥信号量-1
		queue[p] = rand() % 1000;//等待队列
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;//因为资源只有3个,计数后还需要mod 3
		sleep ( rand() % 5);//休眠
		sem_post(&mutex); //释放互斥信号量
		sem_post( &product_number );//是否唤醒消费者
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );//是否对消费者进行阻塞
    	sem_wait(&mutex);//互斥信号量 
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;//同理,资源数只有3个
		sleep( rand() % 5 );
		sem_post(&mutex);//互斥信号量
		sem_post( &blank_number );//是否唤醒生产者
	}
}

int main(int argc, char *argv[] )
{
	pthread_t pid, cid1,cid2,cid3,cid4,cid5;//创建5个消费者线程
	sem_init( &blank_number, 0, NUM );//初始化空闲缓冲区信号量
	sem_init( &product_number, 0, 0);//初始化产品信号量
	sem_init( &mutex, 1, 1);//初始化互斥信号量
	pthread_create( &pid, NULL, producer, NULL);//创建生产者进程
	pthread_create( &cid1, NULL, consumer, NULL);//创建消费者线程
	pthread_create( &cid2, NULL, consumer, NULL);
	pthread_create( &cid3, NULL, consumer, NULL);
	pthread_create( &cid4, NULL, consumer, NULL);
	pthread_create( &cid5, NULL, consumer, NULL);//这里创建了5个消费者线程
	pthread_join( pid, NULL );
	pthread_join( cid1, NULL );//等待消费者线程执行完毕
	pthread_join( cid2, NULL );
	pthread_join( cid3, NULL );
	pthread_join( cid4, NULL );
	pthread_join( cid5, NULL );//5个消费者线程执行完毕
	sem_destroy( &blank_number );//销毁信号量
	sem_destroy( &product_number );
	return 0;
}

运行结果

image
image

原文地址:http://www.cnblogs.com/1395372955jth/p/16885517.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性