7.22IO作业

1.用信号量实现简单生产消费者模型


sem_t sem1;
sem_t sem2;
int a=0;

void* run(void* agr){
    while(1){
        sem_wait(&sem2);
        a-=5;
        printf("消费余%d个苹果\n",a);
        sem_post(&sem2);
        sleep(1);
    }
}


int main(int argc, const char *argv[])
{
//  sem_init(&sem1,0,4);
    sem_init(&sem2,0,1);

    pthread_t id;
    pthread_create(&id,0,run,NULL);
    pthread_detach(id);                  

    while(1){
    sem_wait(&sem2);
        a+=12;
        printf("生产了%d个苹果\n",a);
    sem_post(&sem2);
        sleep(2);
    }


    return 0;
}

 实现效果

2.简单多人多生产者模型


pthread_mutex_t mu1;
pthread_mutex_t mu2;
pthread_mutex_t mu;
pthread_mutex_t mu3;
pthread_cond_t co1;
pthread_cond_t co2;

int apple=0;
int orange=0;

void *run1(void *arg)
{
    while(1){
        pthread_mutex_lock(&mu3);
        while(1){
            pthread_mutex_lock(&mu1);
            apple+=1;
            printf("生产了%d个苹果\n",apple);
            pthread_mutex_unlock(&mu1);
            if(apple>1){
                pthread_cond_signal(&co1);
            }
            sleep(1);
            if(apple>=3){break;}
            }
        pthread_mutex_unlock(&mu);}
}
void *run2(void *arg)
{
    while(1){
        pthread_mutex_lock(&mu1);
        pthread_cond_wait(&co1,&mu1);
        apple-=1;
        printf("A消费余%d个苹果\n",apple);
        sleep(1);
        pthread_mutex_unlock(&mu1);}
}
void *run3(void *arg)
{
    while(1){
        pthread_mutex_lock(&mu);
        while(1){
            pthread_mutex_lock(&mu2);
            orange+=2;
            printf("生产%d个橘子\n",orange);
            pthread_mutex_unlock(&mu2);
            sleep(1);
            if(orange>2){
                pthread_cond_signal(&co2);
            }
            if(orange>=6){break;}
            }
        pthread_mutex_unlock(&mu3);}
}
void *run4(void *arg)
{
    while(1){
        pthread_mutex_lock(&mu2);
        pthread_cond_wait(&co2,&mu2);
        orange-=3;
        printf("B消费余%d个橘子\n",orange);
        sleep(1);
        pthread_mutex_unlock(&mu2);}
}

int main(int argc, const char *argv[])
{
    pthread_mutex_init(&mu1,0);
    pthread_mutex_init(&mu2,0);
    pthread_mutex_init(&mu,0);
    pthread_mutex_init(&mu3,0);
    pthread_cond_init(&co1,0);
    pthread_cond_init(&co2,0);                                               
    pthread_mutex_lock(&mu);
    //
    pthread_t id1;
    pthread_create(&id1,0,run1,NULL);
    pthread_detach(id1);
    pthread_t id2;
    pthread_create(&id2,0,run2,NULL);
    pthread_detach(id2);
    pthread_t id3;
    pthread_create(&id3,0,run3,NULL);
    pthread_detach(id3);
    pthread_t id4;
    pthread_create(&id4,0,run4,NULL);
    pthread_detach(id4);

    while(1);
    return 0;
}
                                                                             
                                                                             
                                                                             
                                                                             
                                                                             

实现效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值