练习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;//定义全局变量隧道长度5000mint train_long=200;//定义全局变量火车长度200mvoid *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);}}