博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
管道 PIPE
阅读量:7058 次
发布时间:2019-06-28

本文共 2341 字,大约阅读时间需要 7 分钟。

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

grep 'colin' bigfile.txt | head

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

grep 'colin' bigfile.txt > tmpfile; head tmpfile

pipes

管道

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

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

eg:

pipeline

示例

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

#include 
#include
#include
#include
#include
#include
#include
#include
/* #include
*/#include
/* 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
#include
#include
#include
#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH#define MAXLINE 1024

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("
\n", buff); } printf("
\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;}

输出

shell> g++ pipe.cpp -o pipe.outpipe.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 

参考

你可能感兴趣的文章
使用windeployqt打包发布Qt桌面应用程序
查看>>
玩转js之——new方法的模拟实现
查看>>
Cookie学习笔记
查看>>
11月17日云栖精选夜读:继浸没液冷后,阿里再推国内首个锂电池服务器
查看>>
Django 静态文件渲染
查看>>
3.06-js保留小数点后两位
查看>>
证书配置
查看>>
Maven pom.xml 配置详解
查看>>
Linux基础(day9)
查看>>
7.4 yum工具用法
查看>>
Chrome 62强化了隐私安全 在无痕窗口模式下HTTP网页都会显示不安全
查看>>
11.15 shell介绍 ,命令历史,命令补全和别名,通配符,输入输出重定向
查看>>
如何加入Xamarin表单工具栏和UINavigationBar、梯度
查看>>
深入理解JVM垃圾收集机制,下次面试你准备好了吗
查看>>
eclipse for C++ linux问题汇总
查看>>
智能媒体管理产品文档转换/预览功能介绍(4)--快速搭建
查看>>
购物车的原理及Java实现(仿京东实现原理)
查看>>
hadoop项目开发案例方案汇总
查看>>
【OpenCV系列】【四】操作像素的三种方式
查看>>
【2018.05.07学习笔记】【linux基础知识10.6-10.10】
查看>>