管道有自己的一套文件系统,它不映射到磁盘,而只是存在内存里。
每个一个管道都有一个缓冲空间,大小因系统而异。
ulimit -a | grep pipe
pipe size (512 bytes, -p) 8
管道的读写支持block特性。
pipe的阻塞可以通过int fcntl(int fd, int cmd, ... /* arg */ )修改
匿名管道pipe
用于有公共祖先的进程间的通信
#include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> int main() { int pfd[2]; if (pipe(pfd) == -1) { perror("pipe"); exit(-1); } pid_t pid = fork(); if (pid == 0) { // child close(pfd[1]); sleep(10); // father is exit already, but the data it sent is there char rdbuf[1024]; int len = read(pfd[0], rdbuf, sizeof(rdbuf)); printf("child read %d\n", len); printf("%s\n", rdbuf); close(pfd[0]); } else if (pid > 0) { // father, self close(pfd[0]); int flag = fcntl(pfd[1], F_GETFL, 0); flag |= O_NONBLOCK; if (fcntl(pfd[1], F_SETFL, flag) == -1) { perror("fcntl"); exit(-1); } char wrbuf[] = "hello pipe!"; int len = write(pfd[1], wrbuf, sizeof(wrbuf)); printf("father write %d\n", len); close(pfd[1]); } else { perror("fork"); } return 0; }
命令管道fifo
用于任意进程间的通信
#define _OPEN_SYS #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> int main() { char fn[]="temp.fifo"; char out[20]="FIFO's are fun!", in[20]; int rfd, wfd; if (mkfifo(fn, S_IRWXU) == -1) { if (errno == EEXIST) { perror("mkfifo wanning"); } else { perror("mkfifo error"); exit(-1); } } chmod(fn, 666); // give w permission to group and other // either O_RDONLY or O_WRONLY if ((rfd = open(fn, O_RDONLY|O_NONBLOCK)) == -1) { perror("open() error for read end"); exit(-1); } if ((wfd = open(fn, O_WRONLY)) == -1) { perror("open() error for write end"); exit(-1); } if (write(wfd, out, strlen(out)+1) == -1) { perror("write() error"); exit(-1); } if (read(rfd, in, sizeof(in)) == -1) { perror("read() error"); exit(-1); } printf("read '%s' from the FIFO\n", in); close(wfd); close(rfd); // unlink a fifo unlink(fn); return 0; }
参考
http://linux.die.net/man/3/mkfifo
http://www.cppblog.com/jackdongy/archive/2013/01/07/197055.html
相关推荐
用Linux语言编程,关于管道进出的,有详细的注释
基于Linux管道技术的编程方法研究.pdf
linux管道专题编程笔记
整理的Linux下的管道编程技术文档。包含管道技术模型,信号和消息的区别 ,管道和命名管道的区别 ,管道编程技术,进程间通信管道编程,与管道相关的系统命令等内容;
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
本书全面介绍了Linux编程相关的知识,内容涵盖Linux基本知识、如何建立Linux开发环境、Linux开发工具、Linux文件系统、文件I/O操作、设备文件、进程与进程环境、守护进程、基本进程间通信方法、管道与命名管道等。...
《linux/unix系统编程手册(上、下册)》总共分为64章,主要讲解了高效读写文件,对信号、时钟和定时器的运用,创建进程、执行程序,编写安全的应用程序,运用posix线程技术编写多线程程序,创建和使用共享库,运用...
编写程序实现进程的管道通信。用系统调用pipe( )建立一管道,二个子进程P1和P2分别向管道各写一句话: Child 1 is sending a message! Child 2 is sending a message! 父进程从管道中读出二个来自子进程的信息并...
管道分类:根据进程的相互关系,可以分为匿名管道与命名管道。 1、匿名管道:管道是父进程和子进程间,或是子进程与子进程间单向的通讯机制,即一个进程发送数据到管道,另外一个进程从管道中读出数据。如果需要...
《Linux/UNIX系统编程手册(上、下册)》总共分为64章,主要讲解了高效读写文件,对信号、时钟和定时器的运用,创建进程、执行程序,编写安全的应用程序,运用POSIX线程技术编写多线程程序,创建和使用共享库,运用...
《linux/unix系统编程手册(上、下册)》总共分为64章,主要讲解了高效读写文件,对信号、时钟和定时器的运用,创建进程、执行程序,编写安全的应用程序,运用posix线程技术编写多线程程序,创建和使用共享库,运用...
linux系统编程--进程间通信--管道;
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
Linux网络编程(总共41集) 讲解Linux网络编程知识,分以下四个篇章。 Linux网络编程之TCP/IP基础篇 Linux网络编程之socket编程篇 Linux网络编程之进程间通信篇 Linux网络编程之线程篇 Linux网络编程之TCP/IP...
I/O操作、设备文件、进程与进程环境、守护进程、基本进程间通信方法、管道与命名管道、POSIX IPC、Linux下的多线程、Linux网络编程、网络嗅探器、Linux图形界面开发基础、GTK+图形界面编程、界面布局与按钮构件、GTK...
第2章 Linux下的C语言编程环境 2.1 Linux编程简介 2.2 Linux下的C语言开发环境 2.3 编辑器的使用 2.4 编译器gcc的使用 2.5 LinuxC程序的开发过程 2.6 make工具及其使用 2.7 使用autoconf 2.8 使用automake ...
第2章 Linux编程环境 14 2.1 Linux环境下的编辑器 14 2.1.1 vim使用简介 14 2.1.2 使用vim建立文件 15 2.1.3 使用vim编辑文本 16 2.1.4 vim的格式设置 18 2.1.5 vim配置文件.vimrc 19 2.1.6 使用其他...
《linux/unix系统编程手册(上、下册)》总共分为64章,主要讲解了高效读写文件,对信号、时钟和定时器的运用,创建进程、执行程序,编写安全的应用程序,运用posix线程技术编写多线程程序,创建和使用共享库,运用...
6.1.1 可编程中断控制器 61 6.1.2 初始化中断处理数据结构 61 6.1.3 中断处理 62 6.2 设备驱动程序 63 6.2.1 测试与中断 64 6.2.2 直接存储器访问(DMA) 65 6.2.3 存储器 66 6.2.4 设备驱动程序与内核的接口 66 6.2.5...
第2章 Linux编程环境 14 2.1 Linux环境下的编辑器 14 2.1.1 vim使用简介 14 2.1.2 使用vim建立文件 15 2.1.3 使用vim编辑文本 16 2.1.4 vim的格式设置 18 2.1.5 vim配置文件.vimrc 19 2.1.6 使用其他...