2024/4/7 IOday6

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
#include <semaphore.h>
sem_t mutex1;//创建信号量
sem_t mutex2;//创建信号量

void* task_A(void* arg){
	float t=(5000+200)/(100/3.6);
	while(1){
		sem_wait(&mutex1);
		printf("火车A正在通过隧道,预计花费%.2f秒\n",t);
		sem_post(&mutex2);
		sleep(1);

	}
	return NULL;
}
void* task_B(void* arg){
	float t=(5000+200)/(50/3.6);
	while(1){
		sem_wait(&mutex2);
		printf("火车B正在通过隧道,预计花费%.2f秒\n",t);
		sem_post(&mutex1);
		sleep(1);

	}
	return NULL;
}

int main(int argc, const char *argv[])
{
	pthread_t id_a;
	pthread_t id_b;
	if(pthread_create(&id_a,NULL,task_A,NULL)!=0){
		perror("pthread_create:");
		return 1;
	}
	if(pthread_create(&id_b,NULL,task_B,NULL)!=0){
		perror("pthread_create:");
		return 1;
	}
	pthread_detach(id_a);
	pthread_detach(id_b);
	sem_init(&mutex1,0,1);//初始化信号量为1
	sem_init(&mutex2,0,0);//初始化信号量为0
	while(1){
	}
	return 0;
}

2:1:有一个隧道,全长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 <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
#include <semaphore.h>
sem_t mutex;
sem_t mutex_A;//创建信号量
sem_t mutex_B;//创建信号量
sem_t mutex_C;//创建信号量

void* task_A(void* arg){
	float t=(5000+200)/(100/3.6);
	while(1){
		sem_wait(&mutex_A);
		sem_wait(&mutex);
		printf("火车A正在通过隧道,预计花费%.2f秒\n",t);
		sem_post(&mutex);
		sem_post(&mutex_B);
	}
	return NULL;
}
void* task_B(void* arg){
	float t=(5000+200)/(50/3.6);
	while(1){
		sem_wait(&mutex_B);
		sem_wait(&mutex);
		printf("火车B正在通过隧道,预计花费%.2f秒\n",t);
		sem_post(&mutex);
		sem_post(&mutex_C);
	}
	
	return NULL;
}
void* task_C(void* arg){
	float t=(5000+200)/(25/3.6);
	while(1){
		sem_wait(&mutex_C);
		sem_wait(&mutex);
		printf("火车C正在通过隧道,预计花费%.2f秒\n",t);
		sem_post(&mutex);
		sem_post(&mutex_A);

	}
	return NULL;
}

int main(int argc, const char *argv[])
{
	pthread_t id_a;//创建火车A线程id
	pthread_t id_b;//创建火车B线程id
	pthread_t id_c;//创建火车C线程id
	sem_init(&mutex_A,0,1);//初始化信号量为1
	sem_init(&mutex_B,0,0);//初始化信号量为0
	sem_init(&mutex_C,0,0);//初始化信号量为0
	sem_init(&mutex,0,1);//初始化信号量为1
	if(pthread_create(&id_a,NULL,task_A,NULL)!=0){
		perror("pthread_create:");
		return 1;
	}
	if(pthread_create(&id_b,NULL,task_B,NULL)!=0){
		perror("pthread_create:");
		return 1;
	}
	if(pthread_create(&id_c,NULL,task_C,NULL)!=0){
		perror("pthread_create:");
		return 1;
	}
	pthread_detach(id_a);//分离式线程,自动回收线程A资源
	pthread_detach(id_b);
	pthread_detach(id_c);

	while(1){
		printf("111111\n");
		sleep(1);
	}

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值