`
kenby
  • 浏览: 717117 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Linux进程通信 之 管道

阅读更多

 

 

管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。 


管道是Linux支持的最初Unix IPC形式之一,具有以下特点:

管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道;

只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程);

单独构成一种独立的文件系统:管道对于管道两端的进程而言,就是一个文件,但它不是普通的文件,它不属于某种文件系统,而是自立门户,单独构成一种文件系统,并且只存在与内存中。

数据的读出和写入:一个进程向管道中写的内容被管道另一端的进程读出。写入的内容每次都添加在管道缓冲区的末尾,并且每次都是从缓冲区的头部读出数据。


管道两端用描述字fd[0]以及fd[1]来描述,管道的两端是固定了任务的。fd[0]只能用于读,称其为管道读端;另一端则只能用于写,由描述字fd[1]来表示,称其为管道写端。


从管道中读取数据:

如果管道的写端不存在,则认为已经读到了数据的末尾,读函数返回的读出字节数为0;

当管道的写端存在时,如果请求的字节数目大于PIPE_BUF,则返回管道中现有的数据字节数,如果请求的字节数目不大于PIPE_BUF,则返回管道中现有数据字节数(此时,管道中数据量小于请求的数据量);或者返回请求的字节数(此时,管道中数据量不小于请求的数据量)。

如果管道没有数据,且管道的写端口是打开状态,则读操作被阻塞直到有数据写入为止。


向管道中写入数据:

对于设置了阻塞标志的写操作:

当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。如果此时管道空闲缓冲区不足以容纳要写入的字节数,则进入睡眠,直到当缓冲区中能够容纳要写入的字节数时,才开始进行一次性写操作。

当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。FIFO缓冲区一有空闲区域,写进程就会试图向管道写入数据,写操作在写完所有请求写的数据后返回。

对于没有设置阻塞标志的写操作:

当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。在写满所有FIFO空闲缓冲区后,写操作返回。

当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。如果当前FIFO空闲缓冲区能够容纳请求写入的字节数,写完后成功返回;如果当前FIFO空闲缓冲区不能够容纳请求写入的字节数,则返回EAGAIN错误,提醒以后再写;


管道的主要局限性正体现在它的特点上:

只支持单向数据流;

只能用于具有亲缘关系的进程之间;

没有名字;

管道的缓冲区是有限的(管道制存在于内存中,在管道创建时,为缓冲区分配一个页面大小);

管道所传送的是无格式字节流,这就要求管道的读出方和写入方必须事先约定好数据的格式,比如多少字节算作一个消息(或命令、或记录)等等;


有名管道以FIFO的文件形式存在于文件系统中。这样,即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过FIFO相互通信


 

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

int main(int argc, const char **argv)
{
    int len;
    pid_t pid;
    int pfd[2];
    char buffer[1024] = {0};

    if (pipe(pfd) < 0) {
        printf("pipe error\n");
        exit(0);
    }

    if ((pid = fork()) < 0) {
        printf("fork error\n");
        exit(0);
    }

    if (pid == 0) {      /* 子进程 */
        write(pfd[1], "hello\n", 6);
        close(pfd[1]);
        sleep(2);
    } else {               /* 父进程 */
        close(pfd[1]);
        while ((len = read(pfd[0], buffer, 1023)) > 0) {
            buffer[len] = '\0';
            printf("len = %d, %s", len, buffer);
        }
        printf("read done\n");
        wait(pid);      /* 等待子进程退出才能看到效果 */
    }
    return 0;
}


1 只有管道写端的引用计数变成0,才会关闭管道得写端.

2 进程结束后,自动关闭打开的文件
3 如果父进程先退出,而且没有调用wait等待子进程, 那么子进程就会变成僵死进程


 

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

int main(int argc, const char **argv)
{
    int len;
    pid_t pid;
    int pfd[2];
    char buffer[1024] = {0};

    char *envp[] = {"PATH=/tmp", "QUERY_STRING=m=18&n=19", NULL};
   
    if (pipe(pfd) < 0) {
        printf("pipe error\n");
        exit(0);
    }

    if ((pid = fork()) < 0) {
        printf("fork error\n");
        exit(0);
    }

    if (pid == 0) {      /* 子进程 */
        close(pfd[1]);
        while ((len = read(pfd[0], buffer, 1023)) > 0) {
            buffer[len] = '\0';
            printf("len = %d, %s", len, buffer);
        }
    } else {               /* 父进程 */
        write(pfd[1], "hello\n", 6);
        close(pfd[1]);
        wait(pid);
    }
    return 0;
}

 1 如果父进程不关闭管道的写端, 即便子进程关闭了管道的写端, 还是会阻塞在read上


 

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

int main(int argc, const char **argv)
{
    int len;
    pid_t pid;
    int pfd[2];
    char buffer[1024] = {0};

    if (pipe(pfd) < 0) {
        printf("pipe error\n");
        exit(0);
    }

    if ((pid = fork()) < 0) {
        printf("fork error\n");
        exit(0);
    }

    if (pid == 0) {      /* 子进程 */
            len = read(pfd[0], buffer, 10);
            printf("## %s", buffer);
            len = write(pfd[1], "from child hello\n", 17);
    } else {               /* 父进程 */
        len = write(pfd[1], "from parent hello\n", 18);
        sleep(1);
        len = read(pfd[0], buffer, 20);
        printf("@@ %s", buffer);
        close(pfd[1]);
        close(pfd[0]);
        wait(pid);
    }
    return 0;
}

  1 使用一个管道进行双向通信是不行的, 会产生混乱, 要想使用管道实现双向通信,必须创建两个管道


 

#include <unistd.h>   
#include <sys/types.h>   
#include <sys/wait.h>   
#include <stdio.h>   
#include <stdlib.h>   
#include <string.h>   

int main()   
{   
    int fd1[2],fd2[2],cld_pid,status;   
    char buf[200], len;   

    if (pipe(fd1) == -1) {// 创建管道1   
        printf("creat pipe1 error\n");   
        exit(1);   
    }   
    if (pipe(fd2) == -1) {// 创建管道2   
        printf("creat pipe2 error\n");   
        exit(1);   
    }   

    if ((cld_pid=fork()) == 0) {//子进程   
        close(fd1[1]); // 子进程关闭管道1的写入端   
        close(fd2[0]); // 子进程关闭管道1的读出端   

        //子进程读管道1   
        len = read(fd1[0],buf,sizeof(buf));   
        printf("%s",buf);   

        //子进程写管道2   
        strcpy(buf,"hi, father, this is your son!\n");   
        write(fd2[1],buf,strlen(buf));   

        exit(0);   
    }   
    else {//父进程
        close(fd1[0]); // 父进程关闭管道1的读出端   
        close(fd2[1]); // 父进程关闭管道2的写入端   

        //父进程写管道1   
        strcpy(buf,"hey, son, I'm your father\n");   
        write(fd1[1],buf, strlen(buf));   

        //父进程读管道2   
        len = read(fd2[0],buf,sizeof(buf));   
        printf("%s",buf);   
        exit(0);   
    }  
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics