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\t%s\t%d\n",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!\n");
        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\n",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\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
            return -1;
        }
    }
     if( (fd = open(FIFO_FILE,O_RDONLY | O_NONBLOCK)) == -1){
            fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
            return -1;
    }

    while(1){
        memset(buf,0,sizeof(buf));
        if(read(fd,buf,sizeof(buf)) == -1){
            fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
            return -1;
        }
        sleep(1);
        printf("read is %s !\n",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\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
            return -1;
    }

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

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值