Linux-C语言-利用有名管道简单实现两个进程间的全双工通信

  1. 有名管道特点:

        1.有名管道是对无名管道的改进,它可以使互不相关的两个进程互相通信,并且在文件系统中可见,可以通过文件名来找到。

        2.半双工的通信方式,进程通过文件IO来操作有名管道。

        3.有名管道遵循先进先出原则,不支持lseek()。

  1.       2.有名管道的创建

                  配合IO即可实现两个进程间的全双工通信。

                3.代码

                1.父进程发,子进程收,(父进程间收发,子进程间收发)

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<sys/wait.h>
int main(int argc, const char *argv[])
{
	unlink("myfifo1");
	unlink("myfifo2");
	if(mkfifo("myfifo1",0777)<0)
	{
		perror("fifo1 create error");
		return -1;
	}
	else
	{
		printf("mkfifo1 OK!\n");
	}
	if(mkfifo("myfifo2",O_CREAT|0777)<0)
	{
		perror("fifo2 create error");
		return -1;
	}
	else
	{
		printf("mkfifo2 OK!\n");
	}
	char buf[20]={'\0'};
	pid_t pid;
	pid=fork();
	if(pid<0)
	{
		perror("fork error");
		return -1;
	}
	else if(0==pid)
	{
		int fw2=open("myfifo2",O_RDONLY);
		if(fw2<0)
		{
			perror("open myfifo2 error");
			return -1;
		}
		while(1)
		{
			memset(buf,0,sizeof(buf));
			read(fw2,buf,sizeof(buf));
			printf("%s",buf);
			if(strncmp(buf,"exit",4)==0)
			{
				break;
			}
		}
		close(fw2);
		exit(0);
	}
	else if(pid>0)
	{
		int fw1=0;
		fw1=open("myfifo1",O_WRONLY);
		if(fw1<0)
		{
			perror("open myfifo1 error");
			return -1;
		}
		while(1)
		{
            waitpid(-1,NULL,WNOHANG);
			fgets(buf,sizeof(buf),stdin);
			write(fw1,buf,sizeof(buf));
			if(strncmp(buf,"exit",4)==0)
			{
				break;
			}
		}
		close(fw1);
	}
	return 0;
}

                2.父进程收,子进程发

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<sys/wait.h>
int main(int argc, const char *argv[])
{
	char buf[20]={'\0'};
	pid_t pid;
	pid=fork();
	if(pid<0)
	{
		perror("fork error");
		return -1;
	}
	else if(0==pid)
	{
		int fw2=open("myfifo2",O_WRONLY);
		if(fw2<0)
		{
			perror("open myfifo2 error");
			return -1;
		}
		while(1)
		{
			fgets(buf,sizeof(buf),stdin);
			write(fw2,buf,sizeof(buf));
			if(strncmp(buf,"exit",4)==0)
			{
				break;
			}
		}
		close(fw2);
		exit(0);
	}
	else if(pid>0)
	{
		int fw1=0;
		fw1=open("myfifo1",O_RDONLY);
		if(fw1<0)
		{
			perror("open myfifo1 error");
			return -1;
		}
		while(1)
		{
			waitpid(-1,NULL,WNOHANG);
			memset(buf,0,sizeof(buf));
			read(fw1,buf,sizeof(buf));
			printf("%s",buf);
			if(strncmp(buf,"exit",4)==0)
			{
				break;
			}
		}
		close(fw1);
	}
	return 0;
}

效果如下:

 退出时要先退出子进程,再退父进程,否则子进程就无法退出,变为孤儿进程,继续运行,需要利用信号将其杀死。

Linux系统下使用C语言编程,可以通过命名管道(也称为FIFO文件)来实现两个进程的双向通信。命名管道是一种文件类型,允许不相关的进程进行通信,它提供了一种数据交换的机制。 要使用命名管道实现双向通信,你需要按照以下步骤进行: 1. 创建命名管道:使用`mkfifo`系统调用创建FIFO文件,为后续进程通信提供一个共同的通道。 2. 打开FIFO文件:进程使用`open`系统调用以读或写模式打开FIFO文件。 3. 进行读写操作:进程使用`read`和`write`系统调用来进行数据的接收和发送。 4. 关闭FIFO文件:数据传输完成后,使用`close`系统调用来关闭FIFO文件。 5. 删除FIFO文件:如果需要,可以在进程结束前删除FIFO文件,使用`unlink`系统调用。 下面是一个简化的代码示例,展示了如何使用C语言创建并打开命名管道,以及如何进行基本的读写操作: ```c #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> int main() { // 创建命名管道 const char *fifo_name = "my_fifo"; mkfifo(fifo_name, 0666); // 打开管道进行写操作 int fd_write = open(fifo_name, O_WRONLY); if (fd_write == -1) { perror("open write"); exit(EXIT_FAILURE); } // 打开管道进行读操作 int fd_read = open(fifo_name, O_RDONLY); if (fd_read == -1) { perror("open read"); close(fd_write); exit(EXIT_FAILURE); } // 写入数据到管道 const char *message = "Hello from the writer!"; write(fd_write, message, sizeof(message)); // 从管道读取数据 char buffer[100]; int len = read(fd_read, buffer, sizeof(buffer)); if (len > 0) { buffer[len] = '\0'; // 确保字符串结束 printf("Received message: %s\n", buffer); } // 关闭管道 close(fd_write); close(fd_read); // 删除命名管道 unlink(fifo_name); return 0; } ``` 在实际使用中,你需要确保两个进程分别以读模式和写模式打开同一个命名管道文件,这样才能实现双向通信
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值