嵌入式学习——2——互斥锁+条件变量(练习题)


pthread_mutex_t pthread_mutex_hm1;
pthread_mutex_t pthread_mutex;
pthread_cond_t pthread_condA;
pthread_cond_t pthread_condB;
/* 1:有一个隧道,全长5公里(5km),有2列火车,全长200米,
      火车A时速 100公里每小时
      火车B时速 50公里每小时
      现在要求模拟火车反复通过隧道的场景(不可能2列火车都在隧道内运行)*/
void funcHomeword_one();
void *trainD(void *avg);
void *trainE(void *avg);
/*2 有一个隧道,全长5公里,有3列火车,全长200米,
    火车A时速 100公里每小时
    火车B时速 50公里每小时
    火车c时速 25公里每小时
    现在要求 火车A先通过隧道,火车B再通过隧道,最后火车C通过隧道*/
void funcHomeword_two();
void *trainA(void *avg);
void *trainB(void *avg);
void *trainC(void *avg);
int second_pass_hm1 = 0;
int second_pass = 0;
int can_pass = 0;
int main(int argc, char const *argv[]) {
    // funcHomeword_two();
    funcHomeword_one();
    return 0;
}

//========================以下问题1=============================
void *trainD(void *avg){
    while (1) {
        printf("trainD\n");
        pthread_mutex_lock(&pthread_mutex_hm1);        
        printf("火车D第%d秒开始进入隧道\n",second_pass_hm1);
        double distances = 5200;
        while (distances > 0) 
        {
            distances -= 100000.00/3600.0;
            second_pass_hm1 += 1;
        }
        printf("火车D第%d秒完全通过隧道\n",second_pass_hm1);
        sleep(1);
        pthread_mutex_unlock(&pthread_mutex_hm1);
    }
}
void *trainE(void *avg){
    while (1) {
        printf("trainE\n");
        pthread_mutex_lock(&pthread_mutex_hm1);        
        printf("火车E第%d秒开始进入隧道\n",second_pass_hm1);
        double distances = 5200;
        while (distances > 0) 
        {
            distances -= 50000.00/3600.0;
            second_pass_hm1 += 1;
        }
        printf("火车E第%d秒完全通过隧道\n",second_pass_hm1);
        sleep(1);
        pthread_mutex_unlock(&pthread_mutex_hm1);
    }
}
void funcHomeword_one(){
    pthread_mutex_init(&pthread_mutex_hm1, NULL);
    pthread_t pthread11,pthread22;
    if (pthread_create(&pthread11, 0, trainD, 0) !=0 || 
        pthread_create(&pthread22, 0, trainE, 0) !=0 ){
        printf("pthread_create error:%s\n",strerror(errno));
        return ;
    }

   
    while (1)
    pthread_detach(pthread11);
    pthread_detach(pthread22);
    pthread_mutex_destroy(&pthread_mutex_hm1);
}

//========================以下问题2=============================
void *trainA(void *avg){
    printf("火车A第%d秒开始进入隧道\n",second_pass);
    double distance = 5200;
    while (distance > 0) 
    {
        distance -= 100000.00/3600.0;
        second_pass += 1;
    }
    printf("火车A第%d秒完全通过隧道\n",second_pass);
    can_pass = 1;
    pthread_cond_signal(&pthread_condA);
    pthread_exit(NULL);
}
void *trainB(void *avg){
    pthread_mutex_lock(&pthread_mutex);
    pthread_cond_wait(&pthread_condA, &pthread_mutex);
    printf("火车B第%d秒开始进入隧道\n",second_pass);
    double distance = 5200;
    while (distance > 0) 
    {
        distance -= 50000.0/3600.0;
        second_pass += 1;
    }
    printf("火车B第%d秒完全通过隧道\n",second_pass);
    can_pass = 2;
    pthread_mutex_unlock(&pthread_mutex);
    pthread_cond_signal(&pthread_condB);
    pthread_exit(NULL);
}

void *trainC(void *avg){
    while(1){
        if(can_pass == 2) break;
    };
    pthread_mutex_lock(&pthread_mutex);
    pthread_cond_wait(&pthread_condB, &pthread_mutex);
    printf("火车C第%d秒开始进入隧道\n",second_pass);
    double distance = 5200;
    while (distance > 0) 
    {
        distance -= 25000.0/3600.0;
        second_pass += 1;
    }
    can_pass = 0;
    printf("火车C第%d秒完全通过隧道\n", second_pass);
    pthread_mutex_unlock(&pthread_mutex);
    pthread_exit(NULL);
}
void funcHomeword_two(){
    pthread_t pthread1,pthread2,pthread3;
    pthread_mutex_init(&pthread_mutex, NULL);
    pthread_cond_init(&pthread_condA, NULL);
    pthread_cond_init(&pthread_condB, NULL);

    if (pthread_create(&pthread1, 0, trainA, 0) !=0 || 
        pthread_create(&pthread2, 0, trainB, 0) !=0 ||
        pthread_create(&pthread3, 0, trainC, 0) != 0){
        printf("pthread_create error:%s\n",strerror(errno));
        return ;
    }
   
    pthread_join(pthread1,0);
    pthread_join(pthread2,0);
    pthread_join(pthread3,0);

    pthread_cond_destroy(&pthread_condA);
    pthread_cond_destroy(&pthread_condB);
    pthread_mutex_destroy(&pthread_mutex);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值