管道 PIPE

简介: 没错,就讲大家可能天天会用的“管道 | “,前者的输出是后者的输入。这里简单要提一点大家可能忽略了的一个有趣事实是,后者不用得到前者执行完毕才启动。更有趣的是,只要后者获取了足够的数据,前者便会停止执行。grep 'colin' bigfile.txt | head故而当 grep 在给定文件中找到含有给定字符串的 10行文字后,即可功成身退,因为那是 hea

没错,就讲大家可能天天会用的“管道 | “,前者的输出是后者的输入。这里简单要提一点大家可能忽略了的一个有趣事实是,后者不用得到前者执行完毕才启动。更有趣的是,只要后者获取了足够的数据,前者便会停止执行。

grep 'colin' bigfile.txt | head
AI 代码解读

故而当 grep 在给定文件中找到含有给定字符串的 10行文字后,即可功成身退,因为那是 head 的全部所需。加入没有管道机制,那就只能这样:

grep 'colin' bigfile.txt > tmpfile; head tmpfile
AI 代码解读

pipes

管道

管道(pipe)是所有Unix都愿意提供的一种进程间通信机制。管道是进程之间的一个单项数据流:一个进程写入管道的所有数据都由内核定向到另一个进程,另一个进程由此就可以从管道中读取数据。

管道被看作是打开的文件,但在已安装的文件系统中没有相应的映像。可以使用pipe()系统调用来创建一个新管道,这个系统调用会返回一对文件描述符; 然后进程通过fork()把这两个描述符传递给它的子进程,由此与子进程共享管道。进程可以在read()系统调用中使用第一个文件描述符从管道中读取数据,同样也可以在write()系统调用中使用第二个文件描述符相管道中写入数据。

eg:
pipeline

示例

ctipc.h 后面系列都使用此头文件,所以这里include了一些多余的

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/mman.h>
/* #include <mqueue.h> */
#include <sys/fcntl.h>
/* mqueueh.h is for POSIX messaging queues, and is not available on OS X.  O_NONBLOCK has nothing to do with that, and is defined in fcntl.h */
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
#define MAXLINE 1024
AI 代码解读

pipe.cpp

#define READFD 0
#define WRITEFD 1
#include "ctipc.h"
int main(int argc, char ** argv){
        int pipe_fd[2], n, ret;
        if(pipe(pipe_fd) < 0){
                printf("create pipe error\n");
                return 0;
        }
        if((ret = fork()) < 0){
                printf("fork error \n");
                return 0;
        }
        if( ret > 0){  //parent process
                char buff[10240] = {0};
                close(pipe_fd[WRITEFD]);
                while(read(pipe_fd[READFD], buff, sizeof(buff))){
                        printf("<print from parent: %s> \n", buff);
                }
                printf("<print from parent: read end>\n");
                close(pipe_fd[READFD]);
                return 0;
        }
        else{ //child process
                close(pipe_fd[READFD]);
                char *info = "[printed in child process, hello world]";
                write(pipe_fd[WRITEFD], info, strlen(info));
                return 0;
        }
        return 0;
}
AI 代码解读

输出

shell> g++ pipe.cpp -o pipe.out
pipe.cpp:26:30: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
                char *info = "[printed in child process, hello world]";
                             ^
1 warning generated.
shell>./pipe.out 
<print from parent: [printed in child process, hello world]> 
<print from parent: read end>
AI 代码解读

参考

相关文章
1pipe管道
 1进程间通信 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不 到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用 户空间拷到内核缓冲区,进程2再从内核缓冲区把数据读走,内核提供的这种机制称为进程 间通信(IPC,InterProcess Communication)。 2pip
1116 0
管道---pipe函数实例讲解
       linux程序设计,很多的开源软件中都会用到管道技术,而管道技术中pipe用的叫频繁,这几天正在看一个软件的源码,同样里面用到了pipe函数,所以驻足,编写此文档,用以加深印象。
1180 0
Signal 13 was raised(SIGPIPE管道破裂)
Signal 13 was raised(SIGPIPE管道破裂)
153 0
详解linux进程间通信-管道 popen函数 dup2函数
  前言:进程之间交换信息的唯一方法是经由f o r k或e x e c传送打开文件,或通过文件系统。本章将说明进程之间相互通信的其他技术—I P C(InterProcess Communication)。
1619 0
【Linux进程间通信】二、pipe管道
【Linux进程间通信】二、pipe管道
306 0
【Linux进程间通信】二、pipe管道
管道
管道的一个典型作用是用来格式化数据,来一个最简单的例子: {{currentTime | date:'yyyy-MM-dd HH:mm:ss'}} public currentTime: Date = new Date(); Angular里面一共内置了12个指令: enter image description here 在复杂的业务场景里面,12个指令肯定不够用,如果需要自定义指令,请查看这里的例子。
752 0