0511_IO6

练习1:

 1:有一个隧道,全长5公里,有2列火车,全长200米,
                火车A时速 100公里每小时
                火车B时速 50公里每小时
                现在要求模拟火车反复通过隧道的场景(不可能2列火车都在隧道内运行)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>                                                                                                          
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
void *train_A(void *arg);
void *train_B(void *arg);
int tunnel_long=5000;//定义全局变量隧道长度5000m
int train_long=200;//定义全局变量火车长度200m
pthread_mutex_t mutex;//互斥锁
pthread_cond_t cond;//条件变量
int main(int argc, const char *argv[])
{
        pthread_cond_init(&cond,0);
        pthread_mutex_init(&mutex,0);
        pthread_t thread_one;
        pthread_t thread_two;
        if(pthread_create(&thread_one,0,train_A,0)!=0){
            perror("线程创建失败");
            return -1;
        }
        if(pthread_create(&thread_two,0,train_B,0)!=0){
            perror("线程创建失败");
            return -1;
        }
        if(pthread_detach(thread_one)!=0){
            perror("线程分离失败");
            return -1;
        }
        if(pthread_detach(thread_two)!=0){
            perror("线程分离失败");
            return -1;
        }
        while(1){
                pthread_cond_signal(&cond);
        }
    return 0;
}
void * train_A(void *arg){
    while(1){
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        float train_speed=100*1000/3600.0;//火车A的s速度
        int sumlong=train_long+tunnel_long;//火车A需要走过的长度
        unsigned int train_time=sumlong/train_speed;
        printf("火车A正在通过中\n");
        usleep(train_time*1000000);
        printf("火车A已经通过\n");
        pthread_mutex_unlock(&mutex);
    }
}
void * train_B(void *arg){
    while(1){
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        float train_speed=50*1000/3600.0;//火车B的s速度
        int sumlong=train_long+tunnel_long;//火车B需要走过的长度
        unsigned int train_time=sumlong/train_speed;
        printf("火车B正在通过中\n");
        usleep(train_time*1000000);
        printf("火车B已经通过\n");
        pthread_mutex_unlock(&mutex);
    }
}


练习2: 

   2:有一个隧道,全长5公里,有3列火车,全长200米,
                火车A时速 100公里每小时
                火车B时速 50公里每小时
                火车c时速 25公里每小时
                现在要求 火车A先通过隧道,火车B再通过隧道,最后火车C通过隧道

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <pthread.h>
 #include <semaphore.h>
 #include <wait.h>
 #include <signal.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <sys/ipc.h>
 #include <sys/sem.h>
 #include <semaphore.h>
 #include <sys/msg.h>
 #include <sys/shm.h>
 #include <sys/un.h>
 pthread_mutex_t mutex; // 互斥锁
 pthread_cond_t cond_a; // 火车A的条件变量
 pthread_cond_t cond_b; // 火车B的条件变量
 pthread_cond_t cond_c; // 火车C的条件变量
 int turn = 0; // 共享状态变量,表示轮到哪一辆火车                                                                                                                                  
 int tunnel_long=5000;//定义全局变量隧道长度5000m
 int train_long=200;//定义全局变量火车长度200m
 void *train_A(void *arg);
 void *train_B(void *arg);
 void *train_C(void *arg);
 int main(int argc, const char *argv[]) {
     pthread_mutex_init(&mutex, NULL);
     pthread_cond_init(&cond_a, NULL);
     pthread_cond_init(&cond_b, NULL);
     pthread_cond_init(&cond_c, NULL);
     pthread_t thread_a, thread_b, thread_c;
     if(pthread_create(&thread_a, 0, train_A, 0)!=0){
         perror("线程创建失败");
         return -1;
     }
    if(pthread_create(&thread_b, 0, train_B, 0)!=0){
         perror("线程创建失败");
         return -1;
    }
     if(pthread_create(&thread_c, 0, train_C, 0)!=0){
         perror("线程创建失败");
         return -1;
     }
     pthread_join(thread_a, NULL);
     pthread_join(thread_b, NULL);
     pthread_join(thread_c, NULL);
     pthread_mutex_destroy(&mutex);
     pthread_cond_destroy(&cond_a);
     pthread_cond_destroy(&cond_b);
     pthread_cond_destroy(&cond_c);
     return 0;
 }
 
 void *train_A(void *arg) {
     while (1) {
         pthread_mutex_lock(&mutex);
         while (turn != 0) {
             pthread_cond_wait(&cond_a, &mutex); // 等待轮到自己
         }
         //火车A通过隧道
         float train_speed=10000*1000/3600.0;//火车A的s速度
         int sumlong=train_long+tunnel_long;//火车A需要走过的长度
         float travel_time = sumlong / train_speed;
         printf("火车A正在通过隧道\n");
         usleep((int)(travel_time * 1000000));
         printf("火车A已经通过隧道\n");
 
         // 轮到火车B,并发出信号
         turn = 1;
         pthread_cond_signal(&cond_b);
         pthread_mutex_unlock(&mutex);
     }
 }
 
 void *train_B(void *arg) {
     while (1) {
         pthread_mutex_lock(&mutex);
         while (turn != 1) {
             pthread_cond_wait(&cond_b, &mutex); // 等待轮到自己
         }
         // 火车B通过隧道
         float train_speed=10000*1000/3600.0;//火车B的s速度
         int sumlong=train_long+tunnel_long;//火车B需要走过的长度
         float travel_time = sumlong / train_speed;
         printf("火车B正在通过隧道\n");
         usleep((int)(travel_time * 1000000));
         printf("火车B已经通过隧道\n");
 
         // 轮到火车C,并发出信号
         turn = 2;
         pthread_cond_signal(&cond_c);
         pthread_mutex_unlock(&mutex);
     }
 }
 
 void *train_C(void *arg) {
     while (1) {
         pthread_mutex_lock(&mutex);
         while (turn != 2) {
             pthread_cond_wait(&cond_c, &mutex); // 等待轮到自己
         }
 
         // 火车C通过隧道
         float train_speed=10000*1000/3600.0;//火车C的s速度
         int sumlong=train_long+tunnel_long;//火车C需要走过的长度
         float travel_time = sumlong / train_speed;
         printf("火车C正在通过隧道\n");
         usleep((int)(travel_time * 1000000));
         printf("火车C已经通过隧道\n");
 
         // 轮回到火车A,并发出信号
         turn = 0;
         pthread_cond_signal(&cond_a);
         pthread_mutex_unlock(&mutex);
     }
 }
                                                                                                                                                                                    

  • 16
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值