08.04有名管道

1)要求AB进程做通信

  1. A进程发送一句话,B进程接收打印

  2. 然后B进程发送给A进程一句话,A进程接收打印

  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

A进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>

int main(int argc, const char *argv[])
{
	//管道1
	if(mkfifo("./myfifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//管道2
	if(mkfifo("./fifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("mkfifo success\n");

	int fd = open("./myfifo",O_WRONLY);    //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	
	int fd1 = open("./fifo",O_RDONLY);  //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	ssize_t res = 0;
	char buf[128];
	while(1)
	{
		//输入管道1
		printf("A-->B:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
	
		
		if(write(fd,buf,sizeof(buf)) < 0)
		{
			perror("write");
			return -1;
		}
		if(strcasecmp(buf,"quit") == 0)
			break;
		//接收管道2
		bzero(buf,sizeof(buf));	
		res = read(fd1,buf,sizeof(buf));
		
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("接收结束\n");
			break;
		}
		printf("A_get:%ld %s\n",res,buf);
        if(strcasecmp(buf,"quit") == 0)
			break;
	}

	close(fd);
	close(fd1);
	return 0;
}

B进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>

int main(int argc, const char *argv[])
{
	//管道1
	if(mkfifo("./myfifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//管道2
	if(mkfifo("./fifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("mkfifo success\n");

	int fd = open("./myfifo",O_RDONLY);  //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	
	int fd1 = open("./fifo",O_WRONLY);  //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	ssize_t res = 0;
	char buf[128];

	while(1)
	{
		//接收管道1
		bzero(buf,sizeof(buf));	
		res = read(fd,buf,sizeof(buf));
		
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("接收结束\n");
			break;
		}
		printf("B get:%ld %s\n",res,buf);
        if(strcasecmp(buf,"quit") == 0)
			break;

		//写入管道2
		printf("B-->A:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		
		if(write(fd1,buf,sizeof(buf)) < 0)
		{
			perror("write");
			return -1;
		}
		if(strcasecmp(buf,"quit") == 0)
			break;

	}

	close(fd);
	close(fd1);
	return 0;
}

 图中1代表B进程,2代表A进程

2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

A进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>

int fd,fd1;
ssize_t res = 0;
char buf[128];

void* putB(void* arg)
{
	//写入管道2
	while(1)
	{
		

		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

		if(write(fd1,buf,sizeof(buf)) < 0)
		{
			perror("write");
			return NULL;
		}

	}

	pthread_exit(NULL);
}


void* getA(void* arg)
{

	//接收管道1
	while(1)
	{
		
		bzero(buf,sizeof(buf));	
		res = read(fd,buf,sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
		{
			printf("接收结束\n");
			break;
		}
		printf("B get:%ld %s\n",res,buf);
		
	}
	pthread_exit(NULL);

}


int main(int argc, const char *argv[])
{
	//管道1
	if(mkfifo("./myfifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//管道2
	if(mkfifo("./fifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("mkfifo success\n");

	fd = open("./myfifo",O_RDONLY);  //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	fd1 = open("./fifo",O_WRONLY);  //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	//创建线程
	
	pthread_t tid1,tid2;
	if(pthread_create(&tid2,NULL,putB,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	if(pthread_create(&tid1,NULL,getA,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	

	close(fd);
	close(fd1);
	return 0;
}

B进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>


int fd,fd1;
ssize_t res = 0;
char buf[128];


void* getB(void* arg)
{
	//接收管道2
	while(1)
	{
		

		bzero(buf,sizeof(buf));	
		res = read(fd1,buf,sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
		{
			printf("接收结束\n");
			break;
		}
		printf("A_get:%ld %s\n",res,buf);
	

	}

	pthread_exit(NULL);
}

void* putA(void* arg)
{
	//输入管道1
	while(1)
	{
		

		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;


		if(write(fd,buf,sizeof(buf)) < 0)
		{
			perror("write");
			return NULL;
		}

		


	}

	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//管道1
	if(mkfifo("./myfifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//管道2
	if(mkfifo("./fifo",0664) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("mkfifo success\n");

	fd = open("./myfifo",O_WRONLY);    //读写
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	fd1 = open("./fifo",O_RDONLY);  //读写
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}

	//创建线程
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,putA,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	if(pthread_create(&tid2,NULL,getB,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	



	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);


	

	close(fd);
	close(fd1);
	return 0;
}

捕获2)3)20)号信号

#include<stdio.h>	
#include<signal.h>
#include<unistd.h>

typedef void (*sighandler_t)(int);

void handler1(int sig)
{
	printf("%d %d\n",sig,__LINE__);
}

void handler2(int sig)
{
	printf("%d %d\n",sig,__LINE__);
}

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

	//捕获20号信号SIGINT
	sighandler_t s = signal(20,handler1);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("%p %d\n",s,__LINE__);

	s = signal(20,handler2);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("%p %d\n",s,__LINE__);
	
	while(1)
	{
		printf("main");
		sleep(1);
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值