进程间通信机制

实现AB进程对话,要求AB进程能够随时收发。
使用多进程

//A process
#include "/home/ubuntu/li/IO/head.h"
void zom(int sig)
{
	while(waitpid(-1,NULL,WNOHANG));
	//返回值为正数,继续回收
	//返回值为0,说明还有子进程,子进程未结束
	//返回值为负数,没有子进程
}
int main(int argc, const char *argv[])
{
	//回收僵尸进程
	sighandler_t s = signal(17,zom);
	if(SIG_ERR == s)
	{
		ERR_MSG("signal");
		return -1;
	}


	if(mkfifo("./fifo1",0775)<0)
	{
		printf("line = %d\n",__LINE__);
		if(errno!=17){
			ERR_MSG("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2",0775)<0)
	{
		printf("line = %d\n",__LINE__);
		if(errno!=17){
			ERR_MSG("mkfifo");
			return -1;
		}
	}
	pid_t cpid = fork();
	int fd_r = open("./fifo1",O_RDONLY);
	int fd_w = open("./fifo2",O_WRONLY);
	if(fd_r<0||fd_w<0)
	{
		ERR_MSG("open");
		return -1;
	}

	if(cpid>0)
	{
		//写
		char buf[20];
		while(1){
			bzero(buf,sizeof(buf));
			printf("请输入:");
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1] = 0;
			if(write(fd_w,buf,sizeof(buf))<0)
			{
				ERR_MSG("write");
				return -1;
			}
			printf("成功写入\n");
			if(!strcmp(buf,"quit"))
				break;
		}
		kill(cpid,9);
	}
	else if(0 == cpid)
	{
		//读
		char buf[20];
		while(1)
		{
			ssize_t res;
			bzero(buf,sizeof(buf));
			res = read(fd_r,buf,sizeof(buf));
			if(res<0)
			{
				ERR_MSG("read");
				return -1;
			}
			else if(0 == res)
			{
				printf("对端结束\n");
				break;
			}
			printf("\nres = %ld,buf:%s",res,buf);
			fflush(stdout);
			if(strcmp(buf,"quit")==0)
				break;
		}
		kill(getppid(),9);
		exit(0);
	}
	else
	{
		ERR_MSG("fork");
		return -1;
	}

	close(fd_r);
	close(fd_w);
	return 0;
}
//B process
#include "/home/ubuntu/li/IO/head.h"
void zom(int sig)
{
	while(waitpid(-1,NULL,WNOHANG));
	//返回值为正数,继续回收
	//返回值为0,说明还有子进程,子进程未结束
	//返回值为负数,没有子进程
}
int main(int argc, const char *argv[])
{
	//回收僵尸进程
	sighandler_t s = signal(17,zom);
	if(SIG_ERR == s)
	{
		ERR_MSG("signal");
		return -1;
	}


	if(mkfifo("./fifo1",0775)<0)
	{
		printf("line = %d\n",__LINE__);
		if(errno!=17){
			ERR_MSG("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2",0775)<0)
	{
		printf("line = %d\n",__LINE__);
		if(errno!=17){
			ERR_MSG("mkfifo");
			return -1;
		}
	}


	pid_t cpid = fork();
	int fd_w = open("./fifo1",O_WRONLY);
	int fd_r = open("./fifo2",O_RDONLY);
	if(fd_r<0||fd_w<0)
	{
		ERR_MSG("open");
		return -1;
	}

	if(cpid>0)
	{
		//写
		char buf[20];
		while(1){
			bzero(buf,sizeof(buf));
			printf("请输入:");
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1] = 0;
			if(write(fd_w,buf,sizeof(buf))<0)
			{
				ERR_MSG("write");
				return -1;
			}
			printf("成功写入\n");
			if(!strcmp(buf,"quit"))
				break;
		}
		kill(cpid,9);//杀死子进程

	}
	else if(0 == cpid)
	{
		//读
		char buf[20];
		while(1)
		{
			ssize_t res;
			bzero(buf,sizeof(buf));
			res = read(fd_r,buf,sizeof(buf));
			if(res<0)
			{
				ERR_MSG("read");
				return -1;
			}
			else if(0 == res)
			{
				printf("对端结束\n");
				break;
			}
			printf("\nres = %ld,buf:%s",res,buf);
			fflush(stdout);
			int a = strcmp(buf,"quit");
			if(strcmp(buf,"quit")==0)
				break;
		}
		kill(getppid(),9);//通知父进程结束
		exit(0);
	}
	else
	{
		ERR_MSG("fork");
		return -1;
	}

	close(fd_r);
	close(fd_w);
	return 0;
}
//head.h
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <errno.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
typedef void (*sighandler_t)(int);
#define ERR_MSG(msg) do{\
	fprintf(stderr,"line:%d\n",__LINE__);\
	perror(msg);\
}while(0)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值