20240811

/*
第一题:
创建2个线程,
1#线程:负责通过文件IO向文件中写入数据
2#线程:负责从该文件中读取数据
使用互斥锁实现:一定是先写入数据,再读取数据
*/

#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>                                               
                                                                  
pthread_mutex_t ma;                                               
pthread_mutex_t mb;                                               
                                                                  
void *task(void *arg)                                             
{                                                                 
    pthread_mutex_lock(&mb);                                      
    int fd = open("./a.txt",O_RDONLY);                            
    char buf[32] = {0};                                           
    read(fd,buf,31);                                              
    printf("%s\n",buf);                                           
    close(fd);                                                    
    pthread_mutex_unlock(&ma);                                    
}                                                                 
                                                                  
                                                                  
typedef struct sockaddr_in addr_in_t;                             
typedef struct sockaddr addr_t;                                   
typedef struct sockaddr_un addr_un_t;                             
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,task,0);                                 
    pthread_detach(id);                                           
                                                                  
    pthread_mutex_lock(&ma);                                      
                                                                  
    int fd = open("./a.txt",O_WRONLY | O_TRUNC | O_CREAT,0666);   
                                                                  
    char arr[32] = "abcdef";                                      
                                                                  
    write(fd,arr,strlen(arr));                                    
                                                                  
    close(fd);                                                    
                                                                  
    pthread_mutex_unlock(&mb);                                    
                                                                  
    sleep(1);                                                     
    return 0;                                                     
}
/*
第二题:
创建5个线程,使用互斥锁安排这5个线程同步运行:123451234512345.....
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.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;

//定义5个互斥锁
pthread_mutex_t ma;
pthread_mutex_t mb;
pthread_mutex_t mc;
pthread_mutex_t md;
pthread_mutex_t me;

void* task1(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mb);
        printf("B\n");
        sleep(1);
        pthread_mutex_unlock(&mc);
    }
}

void* task2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mc);
        printf("C\n");
        sleep(1);
        pthread_mutex_unlock(&md);
    }
}


void* task3(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&md);
        printf("D\n");
        sleep(1);
        pthread_mutex_unlock(&me);
    }
}


void* task4(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&me);
        printf("E\n");
        sleep(1);
        pthread_mutex_unlock(&ma);
    }
}

int main(int argc, const char* argv[]) 
{
    //初始化互斥锁
    pthread_mutex_init(&ma,0);
    pthread_mutex_init(&mb,0);
    pthread_mutex_init(&mc,0);
    pthread_mutex_init(&md,0);
    pthread_mutex_init(&me,0);

    //上锁
    pthread_mutex_lock(&mb);
    pthread_mutex_lock(&mc);
    pthread_mutex_lock(&md);
    pthread_mutex_lock(&me);

    //创建4个子线程
    pthread_t id1,id2,id3,id4;
    pthread_create(&id1,0,task1,0);
    //将线程的属性设置成分离式属性
    pthread_detach(id1);

    pthread_create(&id2,0,task2,0);
    pthread_detach(id2);
    pthread_create(&id3,0,task3,0);
    pthread_detach(id3);
    pthread_create(&id4,0,task4,0);
    pthread_detach(id4);


    while(1)//死循环
    {
        pthread_mutex_lock(&ma);//给自己上锁
        printf("A\n");
        sleep(1);
        pthread_mutex_unlock(&mb);//给mb解锁
    }

    return 0;
}
/*
第三题
有2条隧道,一条快速隧道,一条普通隧道。有5列火车,3列复兴号,2列绿皮扭扭车
要求,复兴号2条隧道都能走,绿皮车只能走普通隧道
模拟火车过隧道的场景
*/

#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 mb;
pthread_mutex_t ma;
 
void *task(void *arg)
{
	while (1)
	{
		if (pthread_mutex_lock(&ma) == 0)
		{
 
			printf("复兴号F2在快速车道通过\n");
			sleep(1);
			pthread_mutex_unlock(&ma);
		}
		else
		{
			if (pthread_mutex_lock(&mb) == 0)
			{
				printf("复兴号F2在普通车道通过\n");
				sleep(2);
				pthread_mutex_unlock(&mb);
			}
		}
	}
}
 
void *task2(void *arg)
{
	while (1)
	{
		pthread_mutex_lock(&mb);
		printf("绿皮车G1在普通车道通过\n");
		sleep(2);
		pthread_mutex_unlock(&mb);
	}
}
 
void *task3(void *arg)
{
	while (1)
	{
		pthread_mutex_lock(&mb);
		printf("绿皮车G2在普通车道通过\n");
		sleep(2);
		pthread_mutex_unlock(&mb);
	}
}
 
void *task4(void *arg)
{
	while (1)
	{
		pthread_mutex_lock(&mb);
		printf("绿皮车G3在普通车道通过\n");
		sleep(2);
		pthread_mutex_unlock(&mb);
	}
}
 
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&ma, 0);
 
	pthread_mutex_init(&mb, 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(id3);
 
	pthread_create(&id4, 0, task4, 0);
	pthread_detach(id4);
 
	while (1)
	{
		if (pthread_mutex_lock(&ma) == 0)
		{
 
			printf("复兴号F1在快速车道通过\n");
			sleep(1);
			pthread_mutex_unlock(&ma);
		}
		else
		{
			if (pthread_mutex_lock(&mb) == 0)
			{
				printf("复兴号F1在普通车道通过\n");
				sleep(2);
				pthread_mutex_unlock(&mb);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值