生产者---消费者模型之信号量

信号量:

(1)定义信号量:

sem_t semaphore; //定义一个名为semaphore的信号量;

(2)初始化信号量 

int sem_init(sem_t *sem, int pshared, unsigned int value); 

参数:
sem_t*  sem:要初始化的信号量;
int pshared:pshared = 0;
usigned int value:该信号信号量的初值;
返回值:
a.返回0,表示初始化成功;
b.返回-1,表示操作失败;
(3)P操作
int sem_wait(sem_t* sem);        //对于信号量sem进行P操作;
int sem_trywait(sem_t* sem);

(4)V操作

int sem_post(sem_t* sem);        //对于信号量sem进行V操作

生产者-消费者模型:
交易场所:环形队列;
注:使用信号量进行互斥和同步;
单进程单生产者--单消费者代码实现:
producter_consumer.c
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t blank_count;
sem_t data_count;
int cycle_queue[20];
void* producter_running(void* arg)
{
    int step = 0;
    while(1)
    {
        sem_wait(&blank_count);   //P
        int data = rand()%1000;
        cycle_queue[step] = data;
        step++;
        step %= 20;
        printf("the producter is %d\n",data);
        sem_post(&data_count); //V
    }
}
void* consumer_running(void* arg)
{
    int step =0;
    while(1)
    {
        sleep(1);
        sem_wait(&data_count);
        int data = cycle_queue[step];
        step++;
        step %= 20;
        printf("the consumer is %d\n",data);
        sem_post(&blank_count);
    }
}
//producter_consumer based on single thread
int main()
{
    pthread_t producter1;
    pthread_t consumer1;
    sem_init(&blank_count,0,20);
    sem_init(&data_count,0,0);   
    pthread_create(&producter1,NULL,producter_running,NULL);
    pthread_create(&consumer1,NULL,consumer_running,NULL);
    pthread_join(producter1,NULL);
    pthread_join(consumer1,NULL);
    return 0;
}
Makefile:

producter_consumer:producter_consumer.c
    gcc -o $@ $^ -lpthread
.PHONY:clean
clean:
    rm -f producter_consumer
运行结果: 

单进程多生产者多消费者的生产者消费者模型:

producter_consumer.c

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t blank_count;
sem_t data_count;
int cycle_queue[20];
pthread_mutex_t pro = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t con = PTHREAD_MUTEX_INITIALIZER;
void* producter_running(void* arg)
{
    static int step = 0;
    while(1)
    {
        pthread_mutex_lock(&pro);
        sem_wait(&blank_count);   //P
        int data = rand()%1000;
        cycle_queue[step] = data;
        step++;
        step %= 20;
        printf("producter:pthread_id:%u--->data:%d\n",pthread_self(),data);
        sem_post(&data_count); //V
        pthread_mutex_unlock(&pro);
    }
    return NULL;
}
void* consumer_running(void* arg)
{
    static    int step =0;
    while(1)
    {
        pthread_mutex_lock(&con);
        sem_wait(&data_count);
        int data = cycle_queue[step];
        step++;
        step %= 20;
        printf("consumeer;pthread_id:%u--->data:%d\n",pthread_self(),data);
        sem_post(&blank_count);
        pthread_mutex_unlock(&con);
        sleep(1);
    }
}
//producter_consumer based on single thread
int main()
{
    pthread_t producter1;
    pthread_t producter2;
    pthread_t consumer1;
    pthread_t consumer2;
    sem_init(&blank_count,0,20);
    sem_init(&data_count,0,0);   
    pthread_create(&producter1,NULL,producter_running,NULL);
    pthread_create(&producter2,NULL,producter_running,NULL);
    pthread_create(&consumer1,NULL,consumer_running,NULL);
    pthread_create(&consumer2,NULL,consumer_running,NULL);
    pthread_join(producter1,NULL);
    pthread_join(producter2,NULL);
    pthread_join(consumer1,NULL);
    pthread_join(consumer2,NULL);
    sem_destroy(&blank_count);
    sem_destroy(&data_count);
    pthread_mutex_destroy(&pro);
    pthread_mutex_destroy(&con);
    return 0;
}
运行结果:




作者水平有限,若有问题,请留言,谢谢!!!


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值