王杰IOday6

#include 
#include 
#include 	
#include 
int flag=0;
ssize_t res=1;
char buf[20]="";
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* callback1(void* arg)
{
	int fd=open("./01_pthread_create.c",O_RDONLY);
	if(fd<0)
	{
		ERR_MSG("open");
		return NULL;
	}
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag!=0)
		{
			pthread_cond_wait(&cond,&mutex);
		}
		res=read(fd,buf,sizeof(buf));
		if(0==res)
		{
			flag=1;
			//唤醒对方
			pthread_cond_signal(&cond);
			//解锁
			pthread_mutex_unlock(&mutex);
			close(fd);
			break;
		}
		flag=1;
		//唤醒对方
		pthread_cond_signal(&cond);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* callback2(void* arg)
{
	int fd1=open("./1.c",O_WRONLY|O_CREAT|O_TRUNC,0664);
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag!=1)
		{		
			pthread_cond_wait(&cond,&mutex);
		}
		if(0==res)
		{
			break;
		}
		write(fd1,buf,res);
		flag=0;
		//唤醒对方
		pthread_cond_signal(&cond);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
	{
		fprintf(stderr,"fault__%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
	{
		fprintf(stderr,"fault__%d__\n",__LINE__);
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	return 0;
}
#include 
#include 
#include 	
#include 
char buf1='A';
char buf2='B';
char buf3='C';
int flag=-1;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* callback1(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag>=0)
		{
			pthread_cond_wait(&cond,&mutex);
		}
		printf("%c\n",buf1);
		flag=0;
		//唤醒对方
		pthread_cond_signal(&cond1);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* callback2(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag!=0)
		{
			pthread_cond_wait(&cond1,&mutex);
		}
		
		printf("%c\n",buf2);

		flag=1;
		//唤醒对方
		pthread_cond_signal(&cond2);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* callback3(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag<=0)
		{
			pthread_cond_wait(&cond2,&mutex);
		}
		printf("%c\n",buf3);
		flag=-1;
		//唤醒对方
		pthread_cond_signal(&cond);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2,tid3;
	if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
	{
		fprintf(stderr,"fault__%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
	{
		fprintf(stderr,"fault__%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid3,NULL,callback3,NULL)!=0)
	{
		fprintf(stderr,"fault__%d__\n",__LINE__);
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	return 0;
}
#include 
#include 
#include 	
#include 
char buf[]="1234567";
sem_t sem1,sem2;
void* output(void*arg)
{
	while(1){
		if(sem_wait(&sem1)==0){//P操作
			printf("%s\n",buf);
			sem_post(&sem2);//V操作
		}
		else{	
			ERR_MSG("sem_wait");
		}
	}
}
void* turn(void*arg)
{
	while(1){
		if(sem_wait(&sem2)==0){//P操作
			for(int i=0;i

你可能感兴趣的:(c语言)