linux系统编程中的信号量--模拟生产者与消费者

来源:互联网 发布:网龙网络开发怎么样 编辑:程序博客网 时间:2024/05/17 09:36
/*************************************************************FileName    : producer_and_customer.c*description : This app demonstrates how to use the semaphore*              solve the problem about the producer and customer.*version     : 1.0 *history     : none   *************************************************************//**************************************************************用多线程、信号量实现生产者和消费者的模拟,仓库容量为10,仓库中开始有3件产品,消费者每3秒消费一件产品,生产者每两秒生产一个产品,生产者和消费者不能同时进入仓库(需要互斥).***************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <semaphore.h>#include <phread.h>#define msleep(x) usleep(x*1000)#define PRODUCT_SPEED 3 //生产速度#define COSTOM_SPEED  1 //消费速度#define INIT_NUM      3 //仓库原有产品数#define TOTAL_NUM     10//仓库容量sem_t p_sem,c_sem,sh_sem;int num = INIT_NUM;void product(void)//生产产品{    sleep(PRODUCT_SPEED);}int add_to_lib()//添加产品到仓库{    num++;    msleep(500);    return num;}void consum()//消费{    sleep(CONSUM_SPEED);}int sub_from_lib()//从仓库中取产品{    num--;    msleep(500);    return num;}void *productor(void *arg){    while(1)    {        sem_wait(&p_sem);//生产信号减一        product();//生产延时        sem_wait(&sh_sem);//用来互斥的信号        printf("push into!tatol_num=%d\n",add_to_lib());        sem_post(&sh_sem);        sem_post(&c_sem);//消费者信号量加一    }}void *consumer(void *arg){    while(1)    {        sem_wait(&c_sem);//消费者信号量减一        sem_wait(&sh_sem);        printf("pop out!tatol_num=%d\n",sub_from_lib());        sem_post(&sh_sem);        sem_post(&p_sem);//生产者信号量加一        consum();//消费延时    }}int main(int argc, char *argv[]){    pthread_t tid1,tid2;    sem_init(&p_sem,0,TOTAL_NUM-INIT_NUM);    sem_init(&c_sem,0,INIT_NUM);    sem_init(&sh_sem,0,1);    pthread_create(&tid1,NULL,productor,NULL);    pthread_create(&tid2,NULL,consumer,NULL);    pthread_join(tid1,NULL);    pthread_join(tid2,NULL);    return 0;}

0 0