管道分类:根据进程的相互关系,可以分为匿名管道与命名管道。
1、匿名管道:管道是父进程和子进程间,或是子进程与子进程间单向的通讯机制,即一个进程发送数据到管道,另外一个进程从管道中读出数据。如果需要双向,或是多项通信机制,则需要建立两个活多个管道。系统负责两件事:一是写入管道的数据和读出管道的数据的顺序是相同的,二是数据不会在管道中丢失,除非某个进程过早的退出。
建立管道函数为
int pipe(int pipe[2]); //其中pipe[0]是读取数据的描述字,pipe[1]是写数据的描述字
上述匿名管道是与进程密切相关的。只有有关系的进程才能使用他们。至于两个不相关的进程则需要用到有名字的管道,即命名管道。
2、命名管道:又称FIFO(FIRST IN FIRST OUT)。它是文件系统中的特殊文件(注意是文件哦,一般我们可以把它放在/tmp/xxxx里)。不同的进程打开相同的命名管道实现类似匿名管道的数据通信。
Unix/Linux 文件系统包括普通文件,文件目录,与特殊文件(FIFO是其中之一),FIFO文件像普通文件一样,也有读写打开等常用功能。
建立命名管道的函数为mkfifo()
FIFO创建后,可以用open(),close(),read(),write(),unlink()等流操作函数操作。
特点:1.FIFO是作为一个特殊的设备文件存在;
2.不同祖先进程的进程之间可以共享数据;
3.使用完后FIFO将继续保存。
实例1匿名管道
include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> void read_data( int pipes[ ] ){ int c; int rc; close( pipes[ 1 ] ); //由于此函数只负责读,因此将写描述关闭(资源宝贵) while( (rc = read(pipes[ 0 ], &c, 1)) > 0 ){ //阻塞,等待从管道读取数据 putchar( c ); //int 转为 unsiged char 输出到终端 } exit( 0 ); } void write_data( int pipes[ ] ){ int c; int rc; close( pipes[ 0 ] ); //关闭读描述字 while( (c=getchar()) > 0 ){ rc = write( pipes[ 1 ], &c, 1 ); //写入管道 if( rc == -1 )...{ perror ("Parent: write"); close( pipes[ 1 ] ); exit( 1 ); } } close( pipes[ 1 ] ); exit( 0 ); } int main( int argc, char *argv[ ] ){ int pipes[ 2 ]; pid_t pid; int rc; rc = pipe( pipes ); //创建管道 if( rc == -1 ){ perror( "pipes" ); exit( 1 ); } pid = fork( ); switch( pid ){ case -1: perror( "fork" ); exit( 1 ); case 0: read_data( pipes ); //相同的pipes default: write_data( pipes ); //相同的pipes } return 0; } |