Linux进程通信(一)——pipe管道

本章内容

  • 采用pipe管道如何进行进程之间的通信
  • pipe管道进程通信的规则和限制
  • Linux中pipe管道的实现机制和管理pipe管道的结构体

什么是进程通信

进程通信就是两个进程之间进行数据交换,在Linux中有好几种可以进行进程通信的方式,在这篇文章中我们主要介绍最基本的进程通信方式——pipe管道。


进程通信的途径

进程之间交换信息的唯一途径就是传送打开的文件。


管道(pipe)

管道是一种最古老也是最基本的系统IPC形式,所有的Linux系统都提供此种通信机制。但是管道有以下两个局限性:

  • 它是半双工的,即数据一个管道上的数据只能在一个方向上流动,如果要实现双向通信,就必须在两个进程之间建立两个管道;
  • 管道只能在具有公共祖先的两个进程之间使用;
  • -

pipe的实现机制

管道是由内核管理的一个缓冲区,它的一端连接一个进程的输出,另一端连接一个进程的输入。管道的缓冲区不需要很大,它被设计为环形的数据结构,当两个进程都终止后,管道的生命周期也会被结束。

管道的创建

管道是通过调用pipe函数创建的。


#include <unistd.h>
int     pipe(int fd[2]);

它由输出型参数fd返回两个文件描述符,fd[0]为读而打开,fd[1]为写而打开,fd[1]的输出是fd[0]的输入,当管道创建成功后pipe函数返回0,如果创建失败则返回-1,fd[0]和fd[1]之间的关系如下图:(pipe1)

如何通过pipe进行通信

上面我们在单个进程中建立了管道,但是实际上,单个进程中的管道是没有什么用的,通常,进程会先调用pipe函数产生管道,接着调用fork()函数,fork函数会将父进程的相关数据结构继承到子进程中,这样就使子进程中的文件描述符表中的fd[0]和fd[1]指向父进程所指向的管道文件,这样就能实现两个进程之间的通信了。上面的过程如下图:
这里写图片描述

利用pipe通信的相关规则

对于一个从子进程到父进程的管道(子进程写,父进程读),父进程关闭fd[1],子进程关闭fd[0],当管道的一段被关闭后(在上面的基础上关闭管道的一端)下列两条规则起作用:

  1. 当读一个写段已经被关闭的管道时,在所有的数据都被读取后,read返回0(read返回0表示已经读到文件结束符);
    下面我们进行验证:
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
    //create pipe
    int fd[2]={0,0};
    if(pipe(fd)!=0){
        //create false
        perror("pipe");
        exit(1);
    }
    // pipe create success
    pid_t id=fork();
    if(id==0){
        //child -->write fd[1]
        printf("Child\n");
        sleep(2);
        const char* msg="Hello,leap\n";
        close(fd[0]);
        int count=3;
        while(count--){
            ssize_t size=write(fd[1],msg,strlen(msg));
            printf("size:%d\n",size);
            //if(count--){
            //  sleep(1);
            //}
            sleep(1);
            printf("child is writing...\n");
        }
        close(fd[1]);
        exit(0);
    }
    else{
        //father -->read fd[0]
        printf("Father\n");
        sleep(2);
        close(fd[1]);
        char buf[1024];
        int count=3;
        while(1){
            ssize_t Len=read(fd[0],buf,1024);
            //printf("Len::%d\n",Len);
            printf("Father is reading...\n");
            if(Len>0){
                //read success
                buf[Len]='\0';
                printf("child say:%s",buf);
            }
            else if(Len==0){
                //read end of file
                printf("Read the end of pipe\n");
                break;
            }
            else{
                perror("read");
                exit(1);
            }
        }
        close(fd[0]);
        int status=0;
        pid_t _pid=waitpid(id,&status,0);
        if(_pid==id){
            printf("Wait success for child\n");
            printf("Exit code:%d,Exit signal:%d\n",(status>>8)&0xff,status&0xff);
        }
        else{
            perror("wait");
        }
        exit(0);
    }
    return 0;
}

程序让子进程写三次字符串然后关闭子进程fd[1],即关闭管道的写端,不关闭父进程的fd[0],即管道的读端。
这里写图片描述

  1. 如果写一个读端已经被关闭的管道,则会产生相关信号对写段的进程进行终止,如果忽略该信号或捕捉该信号并从处理程序返回,则write会返回-1,errno会设置为EPIPE;
    下面我们进行验证:
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
    //create pipe
    int fd[2]={0,0};
    if(pipe(fd)!=0){
        //create false
        perror("pipe");
        exit(1);
    }
    // pipe create success
    pid_t id=fork();
    if(id==0){
        //child -->write fd[1]
        printf("Child\n");
        sleep(2);
        const char* msg="Hello,leap\n";
        close(fd[0]);
        int count=5;
        while(1){
            ssize_t size=write(fd[1],msg,strlen(msg));
            printf("size:%d\n",size);
            if(count--){
                sleep(1);
            }
            printf("child is writing...\n");
        }
        close(fd[1]);
    }
    else{
        //father -->read fd[0]
        printf("Father\n");
        sleep(2);
        close(fd[1]);
        char buf[1024];
        int count=3;
        while(count--){
            ssize_t Len=read(fd[0],buf,1024);
            //printf("Len::%d\n",Len);
            printf("Father is reading...\n");
            if(Len>0){
                //read success
                buf[Len]='\0';
                printf("child say:%s",buf);
            }
            else if(Len==0){
                //read end of file
                printf("Read the end of pipe\n");
            }
            else{
                perror("read");
                exit(1);
            }
        }
        close(fd[0]);
        int status=0;
        pid_t _pid=waitpid(id,&status,0);
        if(_pid==id){
            printf("Wait success for child\n");
            printf("Exit code:%d,Exit signal:%d\n",(status>>8)&0xff,status&0xff);
        }
        else{
            perror("wait");
        }
        exit(0);
    }
    return 0;
}

代码的意图是这样:我们让write端(子进程)一直写字符串msg,而read端(父进程)先读三次然后在关闭掉父进程的fd[0],这样就形成了子进程一直写,而父进程没有在读的情况。结果如下:
这里写图片描述
我们发现父进程关闭掉fd[0]后子进程被异常终止了,我们从子进程的退出码和退出信号码发现它是被13号信号(SIGPIPE)所终止的,所以写一个读端关闭的管道这对PIPE来说并不成立,操作系统会在读端关闭后向写端的进程发送SIGPIPE使进程被终止。

  1. 如果管道的读端和写端都没有关闭,但是管道的写端没有再向管道写数据了。这时如果管道中没有数据了,那么在此read进程会产生阻塞,直到管道中有数据了才读取数据并返回。
  2. 如果有指向管道读端的文件描述符没有关闭,而持有管道读端的没有从管道中读数据,这时有进程向管道中写数据,如果管道被写满再向管道写数据是,再次write会导致进程阻塞,直到管道中有空间了才会继续向管道中写数据并返回。

pipe管道容量

我们可以通过* man 7 pipe*;来查询管道的容量pipe_capacity

## Linux的管道实现机制
从本质上说,管道也是一种文件,但它又和一般的文件有所不同,管道可以克服使用文件进行通信的两个问题,具体表现为:
管道是一个固定大小的缓冲区,在Linux中,该缓冲区的大小为一页,即4kb,使它的大小不会像普通文件那样不加检验的增长。在Linux中,内核使用struct pipe_inode_info结构体来描述一个管道,这个结构体定义在pipe_fs_i.h中。

struct pipe_inode_info结构体

struct pipe_inode_info {
//管道等待队列,当pipe为空/满时指向等待的读者和写者
    wait_queue_head_t wait;            
//pipe中非空缓冲区的数量和当前pipe的入口
    unsigned int nrbufs, curbuf;
//临时释放的页也叫高速缓存区页框指针
    struct page *tmp_page;
//读进程的标志或ID号
    unsigned int readers;
//写进程的标志或ID号
    unsigned int writers;
//在等待队列中睡眠的写进程的个数
    unsigned int waiting_writers;
//reader的总数
    unsigned int r_counter;
//writer的总数
    unsigned int w_counter;
//用于通过信号进行异步I/O通知
    struct fasync_struct *fasync_readers;

    struct fasync_struct *fasync_writers;
//pipe对应的inode
    struct inode *inode;
//pipe的环形缓冲区
    struct pipe_buffer bufs[PIPE_BUFFERS];
};

缓冲区的个数

#define PIPE_BUFFERS (16)

管理缓冲区的结构

struct pipe_buffer {
//包含当前pipe_buffer数据的页
    struct page *page;   
//页中所包含的数据的偏移量,长度
    unsigned int offset, len;
//与buffer相关的操作
    const struct pipe_buf_operations *ops;
//pipe_buffer标志
    unsigned int flags;
    unsigned long private;
};
  • 17
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值