11.15 io day7

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;


int flag = 0; 		//0,1
int flag2=0;
void* callBack_A(void* arg)
{
	int i=10;
	while(i--)
	{
		/******临界区***********/
		pthread_mutex_lock(&mutex);
		//如果当前时机不是我要访问共享资源的时机,我需要去休眠 
		if(flag != 0)
		{
			pthread_cond_wait(&cond, &mutex);
		}

		printf("A\n");
		flag2 = 1;
		flag =1;
		pthread_cond_signal(&cond2);

		pthread_mutex_unlock(&mutex);
		/******临界区***********/
	}
	pthread_exit(NULL);
}

void* callBack_B(void* arg)
{
	int i=10;

	while(i--)
	{
		/******临界区***********/
		pthread_mutex_lock(&mutex);

		if(flag2 != 1)
		{
			pthread_cond_wait(&cond2, &mutex);
		}
		printf("B\n");
		flag = 1;
		flag2=0;
		pthread_cond_signal(&cond);

		pthread_mutex_unlock(&mutex);

		/******临界区***********/
	}
	pthread_exit(NULL);
}
void* callBack_C(void* arg)
{
	int i=10;
	while(i--)
	{
		/******临界区***********/
		pthread_mutex_lock(&mutex);

		if(flag != 1)
		{
			pthread_cond_wait(&cond, &mutex);
		}
	
		printf("C\n");
		flag = 0;

		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, callBack_A, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	if(pthread_create(&tid2, NULL, callBack_B, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&tid3, NULL, callBack_C, NULL) != 0)
	{
		perror("pthread_create");
		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(&cond2);
	return 0;
}

 演示:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int pfd[2]={0};
	int pfd2[2]={0};
	if(pipe(pfd)<0)
	{
		perror("pipe");
		return -1;
	}
	if(pipe(pfd2)<0)
	{
		perror("pipe");
		return -1;
	}
	pid_t pid=0;
	pid=fork();
	if(pid>0)
	{
		close(pfd[0]);//只写,不读
		close(pfd2[1]);//只读,不写
		char s[128]="";
		ssize_t res=0;
		while(1)
		{
			bzero(s,sizeof(s));
			fprintf(stderr,"父发送");
			fgets(s,sizeof(s),stdin);
			s[strlen(s)-1]='\0';
			if(strcmp(s,"quit")==0)
			{
				res=write(pfd[1],s,sizeof(s));
				wait(NULL);
			}
			res=write(pfd[1],s,sizeof(s));
			if(res<0)
			{
				perror("write");
				return -1;
			}


			bzero(s,sizeof(s));
			res=read(pfd2[0],s,sizeof(s));
			if(res<0)
			{
				perror("read");
				return -1;
			}
			if(strcmp(s,"quit")==0)
			{
				exit(0);
			}
			printf("父打印:%s\n",s);

		}
		wait(NULL);
	}
	else if(pid==0)
	{
		close(pfd[1]);//只读,不写
		close(pfd2[0]);//只写,不读
		char s[128]="";
		ssize_t res=0;
		while(1)
		{
			bzero(s,sizeof(s));
			res=read(pfd[0],s,sizeof(s));
			if(res<0)
			{
				perror("read");
				return -1;
			}
			if(strcmp(s,"quit")==0)
			{
				_exit(0);
			}
			if(res!=0)
			{
				printf("子打印:%s\n",s);
			}

			bzero(s,sizeof(s));
			fprintf(stderr,"子发送:");
			fgets(s,sizeof(s),stdin);
			s[strlen(s)-1]='\0';
			if(strcmp(s,"quit")==0)
			{
				res=write(pfd2[1],s,sizeof(s));
				_exit(0);
			}
			res=write(pfd2[1],s,sizeof(s));
			if(res<0)
			{
				perror("write");
				return -1;
			}
		}
	}
	
	return 0;
}

演示:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值