有名管道,创建两个发送接收端,父进程写入管道1和管道2,子进程读取管道2和管道1.

有名管道

#include <myhead.h>
int main(int argc, const char *argv[])
{
	if(mkfifo("./my_fifo_1",0664)==-1)
	{
		perror("mkfifo_1");
		return -1;
	}
	if(mkfifo("./my_fifo_2",0664)==-1)
	{
	perror("mkfifo_2");
	return -1;
	}
	return 0;
}

就收发送

#include <myhead.h>
//
//有名管道,创建两个发送接收端,
//父进程写入管道1和管道2,
//子进程读取管道2和管道1
//
int main(int argc, const char *argv[])
{
	pid_t pid1=fork();
	if(pid1==0)
	{
	int fd2;
	fd2=open("./my_fifo_2",O_RDONLY);
	if(fd2==-1)
	{
	perror("open2");
	return -1;
	}
	char buff1[1024];
	while(1)
	{
		int len=read(fd2,buff1,sizeof(buff1));
		write(1,buff1,len);
		if(strcmp(buff1,"~")==0)
		{
		break;
		}
	}
	close(fd2);
	}
	else if(pid1>0)
	{
	int fd1;
	fd1=open("./my_fifo_1",O_WRONLY);
	if(fd1==-1)
	{
	perror("open");
	return -1;
	}
	char buff[1024];
	while(1)
	{
		int len=read(0,buff,sizeof(buff));
		write(fd1,buff,len);
		if(strcmp(buff,"~")==0)
		{
		break;
		}
	}
	close(fd1);
	}
	else 
	{
	perror("fork");
	return -1;
	}
	return 0;
}

2

#include <myhead.h>
//
//有名管道,创建两个发送接收端,
//父进程写入管道1和管道2,
//子进程读取管道2和管道1
//
//
int main(int argc, const char *argv[])
{
	pid_t pid2=fork();
	if(pid2==0)
	{
	int fd2;
	fd2=open("./my_fifo_1",O_RDONLY);
	if(fd2==-1)
	{
		perror("open2");
		return -1;
	}
	char buff1[1024];
	while(1)
	{
		int len=read(fd2,buff1,sizeof(buff1));
		write(1,buff1,len);
		if(strcmp(buff1,"~")==0)
		{
		break;
		}
	}
	}
	else if(pid2>0)
	{
		int fd1;
		fd1=open("./my_fifo_2",O_WRONLY);
		if(fd1==-1)
		{
			perror("open1");
			return -1;
		}
		char buff[1024];
		while(1)
		{
			int len=read(0,buff,sizeof(buff));
			write(fd1,buff,len);
			if(strcmp(buff,"~")==0)
			{
			break;
			}
		}
		close(fd1);
	}
	else
	{
	perror("fork");
	return -1;
	}
	return 0;
}

思维导图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用有名管道(也称为FIFO)实现两个进程之间的通讯。有名管道是一种特殊的文件类型,可以被多个进程共享读写。下面是一个使用有名管道实现进程通讯的示例程序: 进程1:发送消息 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #define PIPE_NAME "my_pipe" int main() { int fd; char message[100]; // 创建有名管道 mkfifo(PIPE_NAME, 0666); printf("Enter message: "); fgets(message, sizeof(message), stdin); // 打开管道,以只写方式写入消息 fd = open(PIPE_NAME, O_WRONLY); write(fd, message, strlen(message) + 1); close(fd); return 0; } ``` 进程2:接收消息 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #define PIPE_NAME "my_pipe" int main() { int fd; char message[100]; // 打开管道,以只读方式读取消息 fd = open(PIPE_NAME, O_RDONLY); read(fd, message, sizeof(message)); close(fd); printf("Received message: %s", message); // 删除管道 unlink(PIPE_NAME); return 0; } ``` 在这个示例程序中,进程1创建了一个有名管道,并向管道写入一条消息。进程2打开同一个管道,并从中读取消息。注意,在使用有名管道时,需要确保管道文件存在,并且读写进程的打开方式要匹配。如果读写进程的打开方式不匹配,程序将会阻塞。另外,使用完毕后,需要删除管道文件,以免造成文件系统的混乱。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值