Linux 进程通信之管道

管道是单向的、先进先出的,它把一个进程的输出和还有一个进程的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,还有一个进程(读进程)从管道的头部读出数据。数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。管道提供了简单的流控制机制,进程试图读空管道时,进程将堵塞。相同,管道已经满时,进程再试图向管道写入数据。进程将堵塞。


管道包含无名管道和有名管道两种。无名管道仅仅能用于父进程和子进程间的通信,而有名管道能够用于同一系统中的随意两个进程间的通信。

  • 无名管道由pipe()函数创建
int pipe(int pipefd[2]);

#当一个无名管道建立时,它会创建两个文件描写叙述符:
#pipefd[0]  用于读管道,
#pipefd[1]  用于写管道。

#pipe()函数创建的管道默认是打开的.

演示样例程序:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>


int main(int argc, char** argv){
    int pipe_fd[2];
    pid_t pid;
    if( pipe(pipe_fd) == -1){
        fprintf(stderr, "%s	%s	%d
",strerror(errno), __FILE__,__LINE__);
        exit(-1);
    }
    if(( pid = fork()) == 0){
        close(pipe_fd[0]);
        write(pipe_fd[1],"hello ",6);
        write(pipe_fd[1],"  world",7);

        printf("Child process write done!
");
        close(pipe_fd[1]);
        exit(0);
    }else if( pid > 0){
        close(pipe_fd[1]);
        sleep(3);
        char buf[20];
        memset(buf,0,20);
        read(pipe_fd[0],buf,20);
        printf("Parent process in : %s
",buf);
        close(pipe_fd[0]);
    }


    return 0;
}

#对于无名管道的读、写、关闭操作,与普通文件的操作是一样的。
  • 有名管道的创建有两种方式
    1、使用shell命令,格式例如以下:
mkfifo [OPTION] FILENAME

2、在程序中调用mkfifo()函数

int mkfifo(const char *pathname, mode_t mode);

mkfifo()函数会根据參数 pathname 建立特殊的FIFO文件(有名管道),该文件必须不存在,而參数mode为该文件的权限。

mkfifo()建立的FIFO文件其它进程都能够用读写一般的文件方式存取。mkfifo()建立的FIFO文件默认是关闭的,当使用open()函数打开文件时须要注意和普通文件的差别:
1、不能以O_RDWR模式打开FIFO文件进行读写操作。这样做的行为是没有定义的。由于管道是单向的,假设须要在程序之间双向传递数据,使用一对有名管道就可以。
2、对标志位的 O_NONBLOCK 标志的使用方法。
O_RDONLY、O_WRONLY和O_NONBLOCK标志共同拥有四种合法的组合方式:
flags=O_RDONLY:open将会调用堵塞。除非有另外一个进程以写的方式打开同一个FIFO。否则一直等待。
flags=O_WRONLY:open将会调用堵塞。除非有另外一个进程以读的方式打开同一个FIFO。否则一直等待。
flags=O_RDONLY|O_NONBLOCK:假设此时没有其它进程以写的方式打开FIFO,此时open也会成功返回,此时FIFO被读打开,而不会返回错误。


flags=O_WRONLY|O_NONBLOCK:马上返回。假设此时没有其它进程以读的方式打开,open会失败打开。此时FIFO没有被打开。返回-1。

演示样例程序:

#fifo_read.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define FIFO_FILE   "/tmp/myfifo"

int main(int argc, char *argv[]){
    int fd ;
    char buf[128];
    if(access(FIFO_FILE,F_OK) != 0){
        if(mkfifo(FIFO_FILE, 0644) == -1){
            fprintf(stderr,"%s	%s	%d
",strerror(errno), __FILE__,__LINE__);
            return -1;
        }
    }
     if( (fd = open(FIFO_FILE,O_RDONLY | O_NONBLOCK)) == -1){
            fprintf(stderr,"%s	%s	%d
",strerror(errno), __FILE__,__LINE__);
            return -1;
    }

    while(1){
        memset(buf,0,sizeof(buf));
        if(read(fd,buf,sizeof(buf)) == -1){
            fprintf(stderr,"%s	%s	%d
",strerror(errno), __FILE__,__LINE__);
            return -1;
        }
        sleep(1);
        printf("read is %s !
",buf);
    }

    unlink(FIFO_FILE);
    return 0;
}
# fifo_write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define FIFO_FILE   "/tmp/myfifo"

int main(int argc, char *argv[]){
    int fd ;

     if( (fd = open(FIFO_FILE,O_WRONLY | O_NONBLOCK)) == -1){
            fprintf(stderr,"%s	%s	%d
",strerror(errno), __FILE__,__LINE__);
            return -1;
    }

    if (write(fd,"Hello world",12) == -1){
         fprintf(stderr,"%s	%s	%d
",strerror(errno), __FILE__,__LINE__);
            return -1;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/zhchoutai/p/7097318.html