io进程线程day7

1)要求AB进程做通信

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

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

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

进程A
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
	if(mkfifo("./myfifo",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo1",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	int fd=open("./myfifo",O_WRONLY);
	int fd1=open("./myfifo1",O_RDONLY);
	if(fd<0 || fd1<0)
	{
		perror("open");
		return -1;
	}
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf, sizeof(buf));
		printf("请输入>>>");
		//从标准输入读取数据,其实就是从终端获取数据
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf)-1] = '\0'; 	//将获取到的字符串最后一个字节的\n修改成\0

		//向管道中写入数据
		if(write(fd, buf, sizeof(buf)) < 0)
		{
			perror("write");
			return -1;
		}
		//忽略大小写的比较
		if(strcasecmp(buf, "quit") == 0)
			break;
		bzero(buf, sizeof(buf));
		res = read(fd1, buf, sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res) //没有写端
		{
			printf("对方进程退出\n");
			break;
		}
		if(strcasecmp(buf, "quit") == 0)
				break;
		printf("read success : %s\n", buf);
	}

	close(fd);
	close(fd1);
	return 0;
}
进程B
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include <sys/wait.h>
#include<string.h>
int main(int argc, const char *argv[])
{
	if(mkfifo("./myfifo",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo1",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	int fd=open("./myfifo",O_RDONLY);
	int fd1=open("./myfifo1",O_WRONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}
	char buf[128] = "";
		ssize_t res = 0;

		while(1)
		{
			bzero(buf, sizeof(buf));

			//当管道中没有数据的时候,read函数阻塞
			res = read(fd, buf, sizeof(buf));
			if(res < 0)
			{
				perror("read");
				return -1;
			}
			else if(0 == res)
			{
				printf("写端关闭\n");
				break;
			}
			if(strcasecmp(buf, "quit") == 0)
				break;

			printf("read success : %s\n",buf);
			//从终端获取数据,发送到管道中
			bzero(buf, sizeof(buf));
			printf("请输入>>>");
			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;
}

 捕获3号信号

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
//新的处理函数
void handler(int sig)
{
	printf("this is handler %d\n", sig);
}
void handler_1(int sig)
{
	printf("this is handler_1 %d\n", sig);
}
int main(int argc, const char *argv[])
{
	printf("h:%p h_1:%p\n", handler, handler_1); //h:0x55c187a7e76a
												//h_1:0x55c187a7e78e
		//捕获2号信号SIGINT
		sighandler_t s = signal(3, handler);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("%p %d\n", s, __LINE__); //默认处理函数的首地址获取不到,所以打印NULL;
	s = signal(3, handler_1);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("%p %d\n", s, __LINE__); //0x55c187a7e76a 
	while(1)
	{
		printf("this is main\n");
		sleep(1);
	}
	return 0;
}

 同理捕获20号信号

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值