linuxC多进程通讯---无名管道pipe

1、原理

1、pipe作为linux中最基础的进程间通信机制,经常在shell中使用,例如ps aux | grep aaa 即建立了一个管道,而linux 下C程序同样可以通过系统调用pipe在父子进程间使用管道功能。
读取一个空的pipe将导致read操作阻塞,直到有数据被写入到pipe中;向一个已经满的pipe写数据将导致write操作阻塞,直到pipe中的数据被读取出去;
2、如果想避免read和write阻塞,可通过fcntl将pipefd设置成O_NONBLOCK,read和write无论是否成功,都将直接返回,这时需要判断read和write返回错误码,判断操作是否成功。

3、如果有多个进程同时向一个pipe写入时,只有在每个进程写入的数据长度都小于PIPE_BUF时,才可以保证pipe写入的原子性,不然可能会出现数据错乱的情况;
4、管道作为半双工通道,如果想实现双向通信,则需要打开两个管道,一个从父进程->子进程,另一个从子进程->父进程。

2、头文件

#include <unistd.h>

3、函数原型

int pipe(int pipefd[2]);

4、工作原理

 通过函数参数返回两个描述符(fd),pipefd[0] 用来读,pipefd[1]用来写, 写入到pipefd[1]的数据将可以从pipefd[0]读出,管道作为半双工通道,数据只能沿一个方向前进;

在这里插入图片描述

5、返回值

pipe函数返回0表示调用成功,返回-1表示调用失败;

6、举例

单向通信

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define handle_error(msg) \
    {perror(msg);exit(EXIT_FAILURE);}

int main (int argc, char *argv[])
{
	int pipe_fd[2]; 
	if (pipe (pipe_fd) == -1) 
        handle_error("pipe");

	int ret_from_fork;
	ret_from_fork = fork ();
	if (ret_from_fork == -1)
        handle_error("fork");

	if (ret_from_fork == 0)   //child process
	{
		char str[100] = {0}; 
		printf("child process:\n input string:");
		scanf ("%s", str);
		//gets(str);
		write (pipe_fd[1], str, strlen (str)); // write data to pipe
		close (pipe_fd[1]);
		_exit (EXIT_SUCCESS);
	}

	if (ret_from_fork > 0)   // parent process
	{
		char buf[30] = {0};
		read (pipe_fd[0], buf, 30); // read data from pipe
		printf ("parent process: %s\n", buf);
		close (pipe_fd[0]);
		exit (EXIT_SUCCESS);
	}

	return 0;
}

双向通信

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define handle_error(msg) \
    {perror(msg);exit(EXIT_FAILURE);}

int main (int argc, char *argv[])
{
	int pipe_fd1[2]; 
	int pipe_fd2[2]; 
	if (pipe (pipe_fd1) == -1 || pipe (pipe_fd2) == -1) 
        handle_error("pipe");

	int ret_from_fork;
	ret_from_fork = fork ();
	if (ret_from_fork == -1)
        handle_error("fork");
	if (ret_from_fork == 0)   //child process
	{
		char read_buf1[100] = {0}; 
		char write_buf1[100] = {0}; 
		printf("child process:");
		scanf ("%s", write_buf1);
		//gets(str);
		write (pipe_fd1[1], write_buf1, strlen (write_buf1));
		close (pipe_fd1[1]);

		//sleep(5);
		read (pipe_fd2[0], read_buf1, 100);
		printf ("in child process: %s\n", read_buf1);
		close (pipe_fd2[0]);
		_exit (EXIT_SUCCESS);
	}

	if (ret_from_fork > 0)   // parent process
	{
		char read_buf2[100] = {0};
		char write_buf2[100] = {0};
		read (pipe_fd1[0], read_buf2, 100); // read data from pipe
		printf ("in parent process: %s\n", read_buf2);
		close (pipe_fd1[0]);

		printf ("parent process:");
		scanf ("%s", write_buf2);
		write (pipe_fd2[1], write_buf2, strlen (write_buf2));
		close (pipe_fd2[1]);
		exit (EXIT_SUCCESS);
	}

	return 0;
}
发布了349 篇原创文章 · 获赞 6 · 访问量 9750

猜你喜欢

转载自blog.csdn.net/qq_23929673/article/details/99670532