8.4 IO进程之间的通信(IPC)——有名管道fifo、信号signal

题目一、要求AB进程做通信

A进程发送一句话,B进程接收打印
然后B进程发送给A进程一句话,A进程接收打印
重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

代码示例

A进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, const char *argv[])
{
	umask(0);
	//创建有名管道
	if(mkfifo("./myfifo",0777) < 0)
	{
		if(errno != 17) 		//如果errno==17代表管道文件已经存在,是合法错误不处理
		{
			perror("mkfifo");
			return -1;
		}
	}
	//printf("mkfifo success\n");
	if(mkfifo("./myfifo2",0777) < 0)
	{
		if(errno != 17) 		
		{
			perror("mkfifo");
			return -1;
		}
	}
	int fd = open("./myfifo",O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	//printf("open readonly success\n");
	int fd2 = open("./myfifo2",O_WRONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}


	char buf[128]="";
	ssize_t res;
	while(1)
	{
		//接收B进程的消息
		bzero(buf,sizeof(buf));
		res = read(fd,buf,sizeof(buf));
		if(res == 0)
		{
			printf("对方进程已退出\n");
			break;
		}else if(res < 0)
		{
			perror("read");
			return -1;
		}
		printf("已接收>>>%s\n",buf);
		if(strcmp(buf,"quit") == 0)
			break;
		//向B进程发送信息
		bzero(buf,sizeof(buf));
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';

		if(write(fd2,buf,sizeof(buf)) < 0)
		{
			perror("write");
			return -1;
		}
		if(strcmp(buf,"quit") == 0)
			break;
		//printf("write success\n");
	}
	close(fd);
	close(fd2);
	return 0;
}
B进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, const char *argv[])
{
	umask(0);
	//创建有名管道
	if(mkfifo("./myfifo",0777) < 0)
	{
		if(errno != 17) 		//如果errno==17代表管道文件已经存在,是合法错误不处理
		{
			perror("mkfifo");
			return -1;
		}
	}
	//printf("mkfifo success\n");
	if(mkfifo("./myfifo2",0777) < 0)
	{
		if(errno != 17) 		
		{
			perror("mkfifo");
			return -1;
		}
	}
	int fd = open("./myfifo",O_WRONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	//printf("open writeonly success\n");
	int fd2 = open("./myfifo2",O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	char buf[128]="";
	ssize_t res;
	while(1)
	{
		//向A进程发送信息
		bzero(buf,sizeof(buf));
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';
		if(write(fd,buf,sizeof(buf)) < 0)
		{
			perror("write");
			return -1;
		}
		//printf("write success\n");
		if(strcmp(buf,"quit") == 0)
			break;
		//接收A进程的信息
		bzero(buf,sizeof(buf));
		res = read(fd2,buf,sizeof(buf));
		if(res == 0)
		{
			printf("对方进程已退出\n");
			break;
		}else if(res < 0)
		{
			perror("read");
			return -1;
		}
		printf("已接收>>>%s\n",buf);
		if(strcmp(buf,"quit") == 0)
			break;
	}
	close(fd);
	close(fd2);
	return 0;
}

测试结果

在这里插入图片描述

题目二、捕获2)3)20)号信号

代码示例

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

typedef void (*sighandler_t)(int);

void my_signal_2(int sig)
{
	printf("this is 2)signal %d\n",sig);
}
void my_signal_3(int sig)
{
	printf("this is 3)signal %d\n",sig);
}

void my_signal_20(int sig)
{
	printf("this is 20)signal %d\n",sig);
}

int main(int argc, const char *argv[])
{
	sighandler_t s = signal(2,my_signal_2);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	s = signal(3,my_signal_3);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	s = signal(20,my_signal_20);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	//printf("%p %d \n",s,__LINE__);

	while(1)
	{
		printf("this is main\n");
		sleep(1);
	}
	return 0;
}

测试结果

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名社畜L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值