linux系统编程:线程同步-信号量(semaphore)

                             线程同步-信号量(semaphore)

生产者与消费者问题再思考

在实际生活中,只要有商品,消费者就可以消费,这没问题。但生产者的生产并不是无限的,例如,仓库是有限的,原材料是有限的,生产指标受消费指标限制等等。为了进一步,解决好生产者与消费者问题,引入信号量进机制。


信号量

信号量(semaphore)是互斥量的升级版:互斥量的状态为0或1,而信号量可以为n。也就是说,使用互斥量时,最多允许一个线程进入关键区,而信号量允许多个,具体值是信号量当前的内部值。


相关函数

sem_t       //信号量类型
sem_init(sem_t *sem, int pshared, unsigned int value);
sem_wait(sem_t *sem)
sem_trywait
sem_timedwait
sem_post(sem_t *sem)
sem_destroy
重要的是理解:sem_wait和sem_post两个函数。

sem_wait(sem);当sem为零时,线程阻塞;否则,sem减一,线程不阻塞。

sem_post(sem);sem加一。

此外,使用sem_init方法,对信号量类型初始化,第二个参数,默认是0,标明用于线程之间。第三个参数指定了初始值。


单生产者与单消费者

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM 5
sem_t blank_num, product_num;
int i, j, k;
int goods[NUM];
void *producer(void *argv)
{
	while (1)
	{
		sem_wait(&blank_num);
		goods[i] = rand() % 100 + 1;
		printf("produce %d\n", goods[i]);
		sem_post(&product_num);
		i = (i + 1) % NUM;
		sleep(rand() % 2);
	}
}
void *comsumer(void *argv)
{
	while (1)
	{
		sem_wait(&product_num);
		printf("comsume %d\n", goods[j]);
		goods[j] = 0;
		sem_post(&blank_num);
		j = (j + 1) % NUM;
		sleep(rand() % 2);
	}
}
int main(void)
{
	i = j = k = 0;
	//初始化信号量
	sem_init(&blank_num, 0, NUM);
	sem_init(&product_num, 0, 0);
	pthread_t pro, com;
	pthread_create(&com, NULL, producer, NULL);
	pthread_create(&pro, NULL, comsumer, NULL);
	pthread_join(com, NULL);
	pthread_join(pro, NULL);
	sem_destroy(&blank_num);
	sem_destroy(&product_num);
	return 0;
}


多生产者与多消费者

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM 5
pthread_mutex_t m1, m2;
sem_t blank_num, product_num;
int goods[NUM];
int i, j, k;
void *producer(void *argv)
{
	while (1)
	{
		sem_wait(&blank_num);
		pthread_mutex_lock(&m1);
		goods[i] = rand() % 100 + 1;
		printf("produce %d\n", goods[i]);
		i = (i + 1) % NUM;
		pthread_mutex_unlock(&m1);
		sem_post(&product_num);
		sleep(rand() % 2);
	}
}
void *comsumer(void *argv)
{
	while (1)
	{
		sem_wait(&product_num);
		pthread_mutex_lock(&m2);
		printf("comsume %d\n", goods[j]);
		goods[j] = 0;   //置零
		j = (j + 1) % NUM;
		pthread_mutex_unlock(&m2);
		sem_post(&blank_num);
		sleep(rand() % 2);
	}
}
int main(void)
{
	i = j = k = 0;
	//初始化信号量及互斥量
	sem_init(&blank_num, 0, NUM);
	sem_init(&product_num, 0, 0);
	pthread_mutex_init(&m1, NULL);
	pthread_mutex_init(&m2, NULL);
	pthread_t pro[2], com[3];
	for (k = 0; k < 3; k++)
		pthread_create(&com[k], NULL, producer, NULL);
	for (k = 0; k < 2; k++)
		pthread_create(&pro[k], NULL, comsumer, NULL);
	for (k = 0; k < 3; k++)
		pthread_join(com[k], NULL);
	for (k = 0; k < 2; k++)
		pthread_join(pro[k], NULL);
	pthread_mutex_destroy(&m1);
	pthread_mutex_destroy(&m2);
	sem_destroy(&blank_num);
	sem_destroy(&product_num);
	return 0;
}


     

CCPP Blog 目录


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值