第一题: 创建2个线程, 1#线程:负责通过文件IO向文件中写入数据 2#线程:负责从该文件中读取数据 使用互斥锁实现:一定是先写入数据,再读取数据
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.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>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t ma;
pthread_mutex_t mb;
void *run(void* arg)
{
while(1)
{
pthread_mutex_lock(&mb);
int fd=open("./write.txt",O_RDONLY);
char buf2[128]={0};
int res=read(fd,buf2,128);
if(res==0){break;}
printf("读取到的内容为:%s\n",buf2);
pthread_mutex_unlock(&ma);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mb,0);
pthread_mutex_lock(&mb);
pthread_mutex_init(&ma,0);
pthread_t id;
pthread_create(&id,0,run,0);
pthread_detach(id);
int fd=open("./write.txt",O_WRONLY | O_CREAT |O_TRUNC,0666);
pthread_mutex_lock(&ma);
char buf[128]={0};
scanf("%s",buf);
int res=write(fd,buf,strlen(buf));
printf("写入的文件为: %s\n",buf);
pthread_mutex_unlock(&mb);
close(fd);
while(1);
return 0;
}
第二题: 创建5个线程,使用互斥锁安排这5个线程同步运行:123451234512345.....
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.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>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int a = 0;
pthread_mutex_t mb;
pthread_mutex_t ma;
pthread_mutex_t mc;
pthread_mutex_t md;
pthread_mutex_t me;
void* task(void* arg){
while(1){
pthread_mutex_lock(&mb);
printf("2\n");
sleep(1);
pthread_mutex_unlock(&mc);
}
}
void* task2(void* arg){
while(1){
pthread_mutex_lock(&mc);
printf("3\n");
sleep(1);
pthread_mutex_unlock(&md);
}
}
void* task3(void* arg){
while(1){
pthread_mutex_lock(&md);
printf("4\n");
sleep(1);
pthread_mutex_unlock(&me);
}
}
void* task4(void* arg){
while(1){
pthread_mutex_lock(&me);
printf("5\n");
sleep(1);
pthread_mutex_unlock(&ma);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&me,0);
pthread_mutex_lock(&me);
pthread_mutex_init(&md,0);
pthread_mutex_lock(&md);
pthread_mutex_init(&mc,0);
pthread_mutex_lock(&mc);
pthread_mutex_init(&mb,0);
pthread_mutex_lock(&mb);
pthread_mutex_init(&ma,0);
pthread_t id,id2,id3,id4;
pthread_create(&id,0,task,0);
pthread_detach(id);
pthread_create(&id2,0,task2,0);
pthread_detach(id2);
pthread_create(&id3,0,task3,0);
pthread_detach(id2);
pthread_create(&id4,0,task4,0);
pthread_detach(id2);
while(1){
pthread_mutex_lock(&ma);
printf("1\n");
sleep(1);
pthread_mutex_unlock(&mb);
}
return 0;
}
第三题 有2条隧道,一条快速隧道,一条普通隧道。有5列火车,3列复兴号,2列绿皮扭扭车 要求,复兴号2条隧道都能走,绿皮车只能走普通隧道 模拟火车过隧道的场景
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.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>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t ma;
typedef enum{
FX;
LC;
}TrainType;
typedef struct{
TrainType type;
char name[20];
}Train;
void *run(void *arg) {
Train *train = (Train *)arg;
if (train->type == FX) {
printf("%s (复兴号) 正在通过快速隧道...\n", train->name);
sleep(1); // 假设通过隧道需要1秒
} else if (train->type == LC) {
pthread_mutex_lock(&ma);
printf("%s (绿皮) 正在通过普通隧道...\n", train->name);
sleep(2);
pthread_mutex_unlock(&ma);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t id[5];
Train trains[] = {
{FX, "复兴号1"},
{FX, "复兴号2"},
{FX, "复兴号3"},
{LC, "绿皮A"},
{LC, "绿皮B"}
};
// 创建线程
for (int i = 0; i < 5; i++) {
pthread_create(&id[i],0,run,0);
}
// 等待所有线程完成
for (int i = 0; i < 5; i++) {
pthread_join(id[i], NULL);
}
printf("所有火车都已通过隧道。\n");
return 0;
}