IO学习day6(线程同步互斥)

 思维导图

目录

 思维导图

 作业1.创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取的内容打印到终端上,类似实现cat一个文件,cat数据完毕后,要结束两个线程,提示:先读数据,读到数据后将数据打印到终端

作业2.有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的IO,要求打印ABC

 作业3:用信号量的方式实现上述代码顺序执行不允许用flag

 作业1.创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取的内容打印到终端上,类似实现cat一个文件,cat数据完毕后,要结束两个线程,提示:先读数据,读到数据后将数据打印到终端

作业2.有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的IO,要求打印ABC

/******************************作业1****************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <head.h>
#include <fcntl.h>
 
int fd;
char c;
int res=-1;
sem_t sem1,sem2;
 
void* callback1(void* arg)
{
	while(1)
	{
		if(sem_wait(&sem1) < 0)
		{
			perror("sem_wait");
			break;
		}
		if(read(fd,&c,1)==0)
		{
			res=close(fd);
			sem_post(&sem1);
			break;
		}
		if(sem_post(&sem2) < 0)
		{
			perror("sem_post");
			break;
		}
	}
	pthread_exit(NULL);
}
 
void* callback2(void* arg)
{
	while(1)
	{
		if(sem_wait(&sem2) < 0)
		{
			perror("sem_wait");
			break;
		}
		if(0 == res)
			break;
		write(1,&c,1);
		if(sem_post(&sem1) < 0)
		{
			perror("sem_post");
			break;
		}
	}
}
 
 
int main(int argc, const char *argv[])
{
	//创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取到的内容打印到终端上,类似实现cat一个文件。
	//cat数据完毕后,要结束两个线程。
	//提示:先读数据,读到数据后将数据打印到终端上。
	
	//创建信号量
	if(sem_init(&sem1, 0 ,1)<0)
	{
		perror("sem_init");
		return -1;
	}
	if(sem_init(&sem2, 0 ,0)<0)
	{
		perror("sem_init");
		return -1;
	}
	//以读的方式打开文件
	fd=open("./01_pthread_create.c",O_RDONLY);
	if(fd < 0)
	{
		ERR_MSG("open");
		return -1;
	}
 
	//创建两个线程
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
	{
		fprintf(stderr,"pthread_create faile __%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid2,NULL,callback2,NULL) != 0)
	{
		fprintf(stderr,"pthread_create faile __%d__\n",__LINE__);
		return -1;
	}
 
	pthread_join(tid1,NULL);//阻塞等待线程1
	pthread_join(tid2,NULL);//阻塞等待线程2
 
	//销毁信号量
	sem_destroy(&sem1);
	sem_destroy(&sem2);
 
	return 0;
}
/**************************************作业2************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
 
//创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量创建
pthread_cond_t cond1 =PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 =PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 =PTHREAD_COND_INITIALIZER;
//限制访问机制
int flag=1;
 
void* callback1(void* arg)
{
    while(1)
    {
        /*************临界区**************/
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag != 1)
        {
            //休眠 解锁
            pthread_cond_wait(&cond1,&mutex);
        }
        printf("A");
        flag=2;
        //唤醒
        pthread_cond_signal(&cond2);
        //解锁
        pthread_mutex_unlock(&mutex);
        /*************临界区**************/
    }
    pthread_exit(NULL);
}
void* callback2(void* arg)
{
    while(1)
    {
        /*************临界区**************/
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag != 2)
        {
            //休眠 解锁
            pthread_cond_wait(&cond2,&mutex);
        }
        printf("B");
        flag=3;
        //唤醒
        pthread_cond_signal(&cond3);
        //解锁
        pthread_mutex_unlock(&mutex);
        /*************临界区**************/
    }
    pthread_exit(NULL);
 
}
void* callback3(void* arg)
{
    while(1)
    {
        /*************临界区**************/
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag != 3)
        {
            //休眠 解锁
            pthread_cond_wait(&cond3,&mutex);
        }
        printf("C\n");
        flag=1;
        //唤醒
        pthread_cond_signal(&cond1);
        //解锁
        pthread_mutex_unlock(&mutex);
        /*************临界区**************/
    }
    pthread_exit(NULL);
 
}
 
int main(int argc, const char *argv[])
{
    //练习1.创建三个线程id号为ABC,要求三个线程循环打印自己的ID号, 运行顺序为ABCAB.....
    pthread_t A,B,C;
    if(pthread_create(&A,NULL,callback1,NULL)!=0)
    {
        fprintf(stderr,"pthread_create faild __%d__\n",__LINE__);
        return -1;
    }
    if(pthread_create(&B,NULL,callback2,NULL)!=0)
    {
        fprintf(stderr,"pthread_create faild __%d__\n",__LINE__);
        return -1;
    }
    if(pthread_create(&C,NULL,callback3,NULL)!=0)
    {
        fprintf(stderr,"pthread_create faild __%d__\n",__LINE__);                             
        return -1;
    }
 
    pthread_join(A,NULL);
    pthread_join(B,NULL);
    pthread_join(C,NULL);
 
    //销毁互斥锁
    pthread_mutex_destroy(&mutex);
    //销毁条件变量
    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    return 0;
}
                                                                                              

 

 作业3:用信号量的方式实现上述代码顺序执行不允许用flag

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
 

 
char buf[]="1234567";
 
//定义两个信号量
sem_t sem1,sem2;
 
void* callback_1(void* arg)//打印
{
	while(1)
	{
		//P操作
		if(sem_wait(&sem1)<0)
		{
			perror("sem_wait");
			return NULL;
		}
		printf("%s\n",buf);
		//V操作
		if(sem_post(&sem2) < 0)
		{
			perror("sem_post");
			return NULL;
		}
	}
}
 
void* callback_2(void* arg)//逆置 不打印
{
	char t=0;
	while(1)
	{
		//P操作
		if(sem_wait(&sem2)<0)
		{
			perror("sem_wait");
			return NULL;
		}
		for(int i=0;i<strlen(buf)/2;i++)
		{
			t=buf[i];
			buf[i] = buf[strlen(buf)-1-i];
			buf[strlen(buf)-1-i] = t;
		}
		//V操作
		if(sem_post(&sem1) < 0)
		{
			perror("sem_post");
			return NULL;
		}
	}
	pthread_exit(NULL);
}
 
int main(int argc, const char *argv[])
{
	if(sem_init(&sem1, 0, 1)<0)
	{
		perror("sem_init");
		return -1;
	}
	printf("sem_init success __%d__\n",__LINE__);
 
	if(sem_init(&sem2, 0, 0)<0)
	{
		perror("sem_init");
		return -1;
	}
	printf("sem_init success __%d__\n",__LINE__);
	
	//创建2个线程
	pthread_t tid_1,tid_2;
	if(pthread_create(&tid_1,NULL,callback_1,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_detach(tid_1);  //分离线程1
	if(pthread_create(&tid_2,NULL,callback_2,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
 
	pthread_join(tid_2,NULL);	//等待阻塞线程2
	
	sem_destroy(&sem1);   //销毁信号量
	sem_destroy(&sem2);   //销毁信号量
	return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值