王杰IOday6

#include <stdio.h>
#include <string.h>
#include <stdlib.h>	
#include <my_head.h>
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 <stdio.h>
#include <string.h>
#include <stdlib.h>	
#include <my_head.h>
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 <stdio.h>
#include <string.h>
#include <stdlib.h>	
#include <my_head.h>
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<strlen(buf)/2;i++){
				char t=buf[i];
				buf[i]=buf[strlen(buf)-1-i];
				buf[strlen(buf)-1-i]=t;
			}
			sem_post(&sem1);//V操作
		}
		else{
			ERR_MSG("sem_wait");
		}
	}
}
int main(int argc, const char *argv[])
{
	if(sem_init(&sem1,0,1)<0){
		ERR_MSG("sem_init");
	}
	if(sem_init(&sem2,0,0)<0){
		ERR_MSG("sem_init");
	}
	pthread_t tid1,tid2;
	pthread_create(&tid1,NULL,output,NULL);
	pthread_create(&tid2,NULL,turn,NULL);
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	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、付费专栏及课程。

余额充值