IO和进程day06(线程续、同步线程互斥)

今日任务:

IO和进程day06(线程续、同步线程互斥)_第1张图片

1.代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//创建互斥锁
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;//创建条件变量
int flag=0;//0为str空,1为有str值
char str[64]={0};
void *fun1(void*arg){
	//读取
	int fd_rd=open("./1.txt",O_RDONLY);
	if(fd_rd==-1){
		perror("open");
		return NULL;
	}

	while(1){
		//上锁
		pthread_mutex_lock(&mutex);
		if(flag==0){
			memset(str,0,sizeof(str));
			if(read(fd_rd,str,sizeof(str))==0){
				flag=1;
				memset(str,0,sizeof(str));
				pthread_cond_signal(&cond);
				pthread_mutex_unlock(&mutex);
				pthread_exit(NULL);
				break;
			}
			flag=1;
			pthread_cond_signal(&cond);
		}else{
			pthread_cond_wait(&cond,&mutex);
		}
		//解锁
		pthread_mutex_unlock(&mutex);

	}
	pthread_exit(NULL);
}
void *fun2(void*arg){

	//写出
	while(1){
		pthread_mutex_lock(&mutex);

		if(flag==1){
			if(0==strlen(str)){//退出条件
				puts("read end");

				pthread_mutex_unlock(&mutex);
				pthread_exit(NULL);
				break;
			}
			printf("%s",str);
			flag=0;
			pthread_cond_signal(&cond);
		}else{
			pthread_cond_wait(&cond,&mutex);
		}

		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);

}
int main(int argc, const char *argv[])
{


	//创建两个线程,一个读取,一个写出
	pthread_t th1,th2;
	if(pthread_create(&th1,NULL,fun1,NULL)!=0){
		fprintf(stderr,"create failed");
		return -1;
	}
	if(pthread_create(&th2,NULL,fun2,NULL)!=0){
		fprintf(stderr,"create failed");
		return -1;
	}

	pthread_join(th1,NULL);
	pthread_join(th2,NULL);
	//销毁互斥锁
	pthread_mutex_destroy(&mutex);
	//摧毁条件变量
	pthread_cond_destroy(&cond);
	return 0;
}

运行结果:

IO和进程day06(线程续、同步线程互斥)_第2张图片

2.代码:

#include 
#include 
#include 
#include 


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 *fun1(void*arg){
	while(1){
		//上锁

		pthread_mutex_lock(&mutex);
		if(flag==1){
			printf("A");
			flag=2;
			pthread_cond_signal(&cond2);
			
		}else{
			pthread_cond_wait(&cond1,&mutex);
		}
//解锁
			pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void *fun2(void*arg){
	while(1){

		//上锁
		
		pthread_mutex_lock(&mutex);
		if(flag==2){
			printf("B");
			flag=3;
			pthread_cond_signal(&cond3);
			
		}else{
			pthread_cond_wait(&cond2,&mutex);
		}
//解锁
			pthread_mutex_unlock(&mutex);

	}
	pthread_exit(NULL);
}
void *fun3(void*arg){
	while(1){
		//上锁
		
		pthread_mutex_lock(&mutex);
		if(flag==3){
			printf("C");
			flag=1;
			pthread_cond_signal(&cond1);
					}else{
			pthread_cond_wait(&cond3,&mutex);
		}
//解锁
			pthread_mutex_unlock(&mutex);

	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t th1,th2,th3;
	if(pthread_create(&th1,NULL,fun1,NULL)!=0){
		fprintf(stderr,"thread create");
		return -1;
	}
	if(pthread_create(&th2,NULL,fun2,NULL)!=0){
		fprintf(stderr,"thread create");
		return -1;
	}
	if(pthread_create(&th3,NULL,fun3,NULL)!=0){
		fprintf(stderr,"thread create");
		return -1;
	}

	pthread_join(th1,NULL);
	pthread_join(th2,NULL);
	pthread_join(th3,NULL);
	//销毁锁
	pthread_mutex_destroy(&mutex);
	//销毁条件变量
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	pthread_cond_destroy(&cond3);
	return 0;
}

运行结果

IO和进程day06(线程续、同步线程互斥)_第3张图片

3.代码:

#include 
#include 
#include 
#include 
#include 

char buf[]="1234567";
sem_t sem1;
sem_t sem2;
void * fun1(void*arg){
	while(1){
	if(sem_wait(&sem1)!=0){
		perror("fun1 wait");
		return NULL;
	}
	//逆置
	for (int i=0; i

运行结果:

IO和进程day06(线程续、同步线程互斥)_第4张图片

今日思维导图

今天 的脑子不太够用

你可能感兴趣的:(java,开发语言)