您的位置:首页 > 运维架构 > Linux

linux应用编程--pipe无名管道

2017-07-11 21:30 351 查看
1、相关概念

管道(pipe)

管道是进程间通信的主要手段之一。一个管道实际上就是个只存在于内存中的文件,对这个文件的操作要通过两个已经打开文件进行,它们分别代表管道的两端。管道是一种特殊的文件,它不属于某一种文件系统,而是一种独立的文件系统,有其自己的数据结构。根据管道的适用范围将其分为:无名管道和命名管道。
无名管道

主要用于父进程与子进程之间,或者两个兄弟进程之间。在linux系统中可以通过系统调用建立起一个单向的通信管道,且这种关系只能由父进程来建立。因此,每个管道都是单向的,当需要双向通信时就需要建立起两个管道。管道两端的进程均将该管道看做一个文件,一个进程负责往管道中写内容,而另一个从管道中读取。这种传输遵循“先入先出”(FIFO)的规则。

2.创建,关闭管道的应用

2.1 首先了解下无名管道的创建函数

表头文件

#include<unistd.h>

定义函数

int pipe(int filedes[2]);

函数说明

pipe()会建立管道,并将文件描述词由参数filedes数组返回。filedes[0]为管道里的读取端,filedes[1]则为管道的写入端。

返回值

若成功则返回零,否则返回-1,错误原因存于errno中。

错误代码

EMFILE 进程已用完文件描述词最大量。

ENFILE 系统已无文件描述词可用。

EFAULT 参数filedes数组地址不合法。

2.2 实例应用

#include <unistd.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{
int pipe_fd[2];
if(pipe(pipe_fd)<0)
{
      printf("pipe create failed\n");
      return -1;
}
else 
printf("pipe create success\n");
close(pipe_fd[0]);
close(pipe_fd[1]);

}

上述创建管道后,返回了两个文件描述符,最后程序结束时需要关闭。下面这一个实例将进一步介绍管道的应用。

2.3 pipe在父子进程的通信应用

要求: 利用fork函数产生一个子进程,并通过无名管道pipe从子进程发送一些信息到父进程。

#include<unistd.h>

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define MAXSIZE 100

int main(int argc,char **argv)

{

        int fd[2],pid,line;

        char message[MAXSIZE];

        /*create pipe*/

        if(pipe(fd)==-1)

        {

                printf("create pipe failed!");

                return -1;

        }

        /*create new process*/

        else if((pid=fork())<0)

        {

                printf("not create a new process!");

                return -1;

        }

        /*child process*/

        else if(pid==0)

        {

                close(fd[0]);

                printf("child process send message!\n");

                write(fd[1],"the message is from the child!",31);

                close(fd[1]);

                exit(0);

        }

        else

        {

                wait(NULL);

                close(fd[1]);

                printf("parent process receive message is:\n ");

                line=read(fd[0],message,MAXSIZE);

                write(STDOUT_FILENO,message,line);

                printf("\n");

                close(fd[0]);

                exit(0);

        }

        return 0;

}

程序解析:

1.创建管道函数必须放于创建子进程之前

2.管道创建和创建子进程之后,父子进程都有一条管道,每个管道都有读端和写端

3.本实例中要求用子进程发送消息,父进程接收消息,所以子进程中的读端没有用到,父进程中的写端没有用到,可以关闭

  close(fd[0]);

  printf("child process send message!\n");

  close(fd[1]);

  printf("parent process receive message is:\n ");

4.子进程的写端和父进程的读端用完之后也需要关闭

   write(fd[1],"the message is from the child!",31);

   close(fd[1]);

   exit(0);

   printf("\n");

   close(fd[0]);

   exit(0);

5.子进程把信息写入管道的写端
   write(fd[1],"the message is from the child!",31);

6.父进程从管道的读端读入数据,并刷新到屏幕输出

   line=read(fd[0],message,MAXSIZE);
   write(STDOUT_FILENO,message,line);
   printf("\n");

   

                        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: