3/6作业

代码示例展示了如何创建并使用FIFO(命名管道)来实现在进程A和进程B之间进行数据交换,进程间可以随时发送和接收消息。每个进程都通过打开两个FIFO(一个用于读,一个用于写)来实现这一功能,并且使用信号处理函数处理进程间的交互,当接收到特定信号时,进程会终止通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现AB进程对话,要求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>
#include <sys/wait.h>
#include <stdlib.h>
typedef void (*sighandler_t)(int);
void callBack(int sig)
{
	while(waitpid(-1,NULL,WNOHANG));
}
int main(int argc, const char *argv[])
{
	sighandler_t s = signal(17,callBack);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	if(mkfifo("./fifo",0775)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("fifo success\n");
	if(mkfifo("./fifo1",0775)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("fifo1 success\n");
	int fd_r=open("./fifo",O_RDONLY);
	if(fd_r<0)
	{
		perror("open");
		return -1;
	}
	printf("open fifo rdonly success __%d__\n",__LINE__);

	int fd_w=open("./fifo1",O_WRONLY);
	if(fd_w<0)
	{
		perror("open");
		return -1;
	}
	printf("open fifo wronly success __%d__\n",__LINE__);

	pid_t cpid=fork();
	//管道读取
	char buf[20]="";
	ssize_t res=0;	
	if(cpid>0)
	{
		while(1)
		{
			bzero(buf,sizeof(buf));
			res=read(fd_r,buf,sizeof(buf));
			if(res<0)
			{
				perror("read");
				return -1;
			}
			else if(0==res)
			{
				printf("对端进程退出\n");
				break;
			}
			printf("res=%ld buf=%s\n",res,buf);
			if(strcmp(buf,"quit")==0)
			{
				break;
			}
			kill(cpid,9);
		}
	}

	//管道写入
	else if(0==cpid)
	{
		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)
			{
				perror("write");
				return -1;
			}
			printf("写入成功\n");
			if(strcmp(buf,"quit")==0)
			{
				break;
			}
			kill(getppid(),9);
			exit(0);
		}
	}
	else
	{
		perror("fork");
		return -1;
	}
	close(fd_r);
	close(fd_w);
	return 0;
}

进程b

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>
typedef void (*sighandler_t)(int);
void callBack(int sig)
{
	while(waitpid(-1,NULL,WNOHANG));
}
int main(int argc, const char *argv[])
{
	sighandler_t s = signal(17,callBack);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	if(mkfifo("./fifo",0775)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("fifo success\n");	
	if(mkfifo("./fifo1",0775)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("fifo1 success\n");

	int fd_w=open("./fifo",O_WRONLY);
	if(fd_w<0)
	{
		perror("open");
		return -1;
	}
	printf("open fifo wronly success __%d__\n",__LINE__);

	int fd_r=open("./fifo1",O_RDONLY);
	if(fd_r<0)
	{
		perror("open");
		return -1;
	}
	printf("open fifo rdonly success __%d__\n",__LINE__);

	pid_t cpid=fork();
	//管道写入
	char buf[20]="";
	ssize_t res=0;
	if(cpid>0)
	{
		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)
			{
				perror("write");
				return -1;
			}
			printf("写入成功\n");
			if(strcmp(buf,"quit")==0)
			{
				break;
			}
			kill(cpid,9);
		}
	}
	//管道读取
	else if(0==cpid)
	{
		while(1)
		{
			bzero(buf,sizeof(buf));
			res=read(fd_r,buf,sizeof(buf));
			if(res<0)
			{
				perror("read");
				return -1;
			}
			else if(0==res)
			{
				printf("对端进程退出\n");
				break;
			}
			printf("res=%ld buf=%s\n",res,buf);
			if(strcmp(buf,"quit")==0)
			{
				break;
			}
			kill(getppid(),9);
			exit(0);
		}
	}
	else
	{
		perror("fork");
		return -1;
	}
	close(fd_w);
	close(fd_r);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值