有名管道&&信号捕获

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 <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	umask(0);//文件掩码
	//创建用于发送信息的管道文件
	if(mkfifo("./myfifo1", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//创建用于接收信息的管道文件
	if(mkfifo("./myfifo2", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//只写打开管道文件1
	int op1 = open("./myfifo1", O_WRONLY);
	if(op1 < 0)
	{
		perror("open");
		return -1;
	}
	//只读打开管道文件2
	int op2 = open("./myfifo2", O_RDONLY);
	if(op2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("准备就绪\n");
	char send[128] = "";
	char accept[128] = "";
	int res = 0;
	while(1)
	{
		//发送信息
		bzero(send, 128);
		printf("A要发送的信息--->");
		fgets(send, 128, stdin);
		send[strlen(send)-1] = '\0';
		write(op1, send, 128);
		//接收信息
	 	bzero(accept, 128);
		res = read(op2, accept, 128);
		send[strlen(send)-1];
		if(strcmp(accept, "quit")==0)
		{
			write(op1, "quit", 5);
			break;
		}
		printf("A接收到的信息--->%s\n",accept);
	}
	close(op1);
	close(op2);
	return 0;
}

B进程代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	umask(0);//文件掩码
	//创建用于发送信息的管道文件
	if(mkfifo("./myfifo1", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//创建用于接收信息的管道文件
	if(mkfifo("./myfifo2", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//只读打开管道文件1
	int op1 = open("./myfifo1", O_RDONLY);
	if(op1 < 0)
	{
		perror("open");
		return -1;
	}
	//只写打开管道文件2
	int op2 = open("./myfifo2", O_WRONLY);
	if(op2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("准备就绪\n");
	char send[128] = "";
	char accept[128] = "";
	int res = 0;
	while(1)
	{
		//接收信息
		bzero(accept, 128);
		res = read(op1, accept, 128);
		if(strcmp(accept, "quit")==0)
		{
			write(op2, "quit", 5);
			break;
		}
		printf("B接收到的信息--->%s\n",accept);
		//发送信息
		bzero(send, 128);
		printf("B要发送的信息--->");
		fgets(send, 128, stdin);
		send[strlen(send)-1] = '\0';
		write(op2, send, 128);
	}
	close(op1);
	close(op2);

	return 0;
}

 测试结果

 

 

2.捕获2、3、20号信号

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
typedef void (*sighandler_t)(int);
void A(int sig) //2号
{
	printf(".........%d.........\n",sig);
}
void B(int sig)//3号
{
	printf(".........%d.........\n",sig);
}
void C(int sig)//20号
{
	printf(".........%d.........\n",sig);
	exit(1);//退出
}
int main(int argc, const char *argv[])
{
	//捕获2号信号
	sighandler_t s = signal(2, A);
	if(s ==SIG_ERR)
	{
		perror("signal");
		return -1;
	}
	//捕获3号信号
	sighandler_t s2 = signal(3, B);
	if(s ==SIG_ERR)
	{
		perror("signal");
		return -1;
	}
	//捕获20号信号
	sighandler_t s3 = signal(20, C);
	if(s ==SIG_ERR)
	{
		perror("signal");
		return -1;
	}
	while(1)
	{
		printf(">>>>main<<<<\n");
		sleep(1);
	}
	return 0;
}

结果:

ubuntu@ubuntu:day7$ gcc hw2.c 
ubuntu@ubuntu:day7$ ./a.out 
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
^C.........2.........
>>>>main<<<<
^C.........2.........
>>>>main<<<<
^C.........2.........
>>>>main<<<<
>>>>main<<<<
^\.........3.........
>>>>main<<<<
^\.........3.........
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
^Z.........20.........
ubuntu@ubuntu:day7$ 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

傾语

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

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

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

打赏作者

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

抵扣说明:

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

余额充值