IO进程线程->进程间的通信->day7

目录

一、 AB通信函数

1.1 A进程函数

1.2 B进程函数

 1.3 执行结果

 三、捕获信号

3.1 捕获函数

3.2  执行结果


练习

1)要求AB进程做通信
        1. A进程发送一句话,B进程接收打印
        2. 然后B进程发送给A进程一句话,A进程接收打印
        3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

(个人能力有限,希望某位大神鼎力相助)
3)捕获2)3)20)号信号

一、 AB通信函数

我采用的是有名通信,建立两个有名通道,A在通道1只能读,通道2只能写,B反过来。

完成A先发送,B打印,然后再写入,B等到A写入,然后打印,再写入,循环往复,直到输出quit,退出所以程序。


1.1 A进程函数

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



int main(int argc, const char *argv[])
{
	umask(0);
	if(mkfifo("./mkfifo1",0777) < 0)
	{
		if(17 != errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	umask(0);
	if(mkfifo("./mkfifo2",0777) < 0)
	{
		if(17 != errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("创建成功\n");
	int fd_w=open("./mkfifo1",O_WRONLY);
	if(fd_w<0)
	{
		perror("open");
		return -1;
	}
	int fd_r=open("./mkfifo2",O_RDONLY);
	if(fd_r<0)
	{
		perror("open");
		return -1;
	}
	printf("打开管道成功\n");
	char str[128]="";
	ssize_t res=0;
	while(1)
	{
		printf("A线程写入:\n");
		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1]='\0';
		if(write(fd_w,str,sizeof(str))<0)
		{
			perror("write");
			return -1;
		}
		if(strcasecmp(str,"quit") == 0)
		{
			break;
		}
		printf("线程A写入成功\n");

		bzero(str,sizeof(str));
		res=read(fd_r,str,sizeof(str));
		if(res<0)
		{
			perror("read");
			return -1;
		}else if(0 == res)
		{
			printf("线程B退出\n");
			break;
		}
		if(strcasecmp(str,"quit") == 0)
		{
			break;
		}
		printf("线程B:%s\n",str);

	}
	close(fd_r);
	close(fd_w);
	printf("程序结束\n");

	return 0;
}

1.2 B进程函数

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


int main(int argc, const char *argv[])
{
	umask(0);
	if(mkfifo("./mkfifo1",0777) < 0)
	{
		if(17 != errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	umask(0);
	if(mkfifo("./mkfifo2",0777) < 0)
	{
		if(17 != errno)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("创建成功\n");
	int fd_r=open("./mkfifo1",O_RDONLY);
	if(fd_r<0)
	{
		perror("open");
		return -1;
	}
	int fd_w=open("./mkfifo2",O_WRONLY);
	if(fd_w<0)
	{
		perror("open");
		return -1;
	}
	printf("打开管道成功\n");
	char str[128]="";
	ssize_t res=0;
	while(1)
	{
		bzero(str,sizeof(str));
		res=read(fd_r,str,sizeof(str));
		if(res<0)
		{
			perror("read");
			return -1;
		}else if(0 == res)
		{
			printf("线程A退出\n");
			break;
		}
		if(strcasecmp(str,"quit") == 0)
		{
			break;
		}
		printf("线程A:%s\n",str);

		printf("B线程写入:\n");
		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1]='\0';
		if(write(fd_w,str,sizeof(str))<0)
		{
			perror("write");
			return -1;
		}
		if(strcasecmp(str,"quit") == 0)
		{
			break;
		}
		printf("线程B写入成功\n");

	}
	close(fd_r);
	close(fd_w);
	printf("程序结束\n");

	return 0;
}

 1.3 执行结果


 三、捕获信号

        很简单,就函数的应用和了解编号标识下是什么信号,还有第一次从默认函数中捕获信号,是拿不到默认函数的首地址的。


3.1 捕获函数

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

typedef void (*sighandler_t)(int);

void han1(int sig)
{
	printf("新处理函数:%d\n",sig);
}

void han3(int sig)
{
	printf("新处理函数:%d\n",sig);
}

void han20(int sig)
{
	printf("新处理函数:%d\n",sig);
}

void handler_1(int sig)
{
	printf("默认处理函数:%d\n",sig);
}

int main(int argc, const char *argv[])
{
	printf("默认处理函数地址:%p 捕获2:%p 捕获3:%p 捕获20:%p\n",handler_1,han1,han3,han20);//默认处理函数地址和新处理函数地址
	/****捕获2******/
	sighandler_t s=signal(2,han1);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("[%d]: %p\n",__LINE__,s);

	s=signal(2,handler_1);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("[%d]: %p\n",__LINE__,s);
	/****捕获3******/
	s=signal(3,han3);
	if(SIG_ERR == s)
	{   
		perror("signal");
		return -1;
	}
	printf("[%d]: %p\n",__LINE__,s);

	s=signal(3,handler_1);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("[%d]: %p\n",__LINE__,s);

	/****捕获20******/
	s=signal(20,han20);
	if(SIG_ERR == s)
	{   
		perror("signal");
		return -1;
	}
	printf("[%d]: %p\n",__LINE__,s);

	s=signal(20,handler_1);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	printf("[%d]: %p\n",__LINE__,s);

	while(1)
	{
		sleep(1);
	}

	return 0;
}

3.2  执行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值