管道
<!--[if !supportLists]-->1、<!--[endif]-->函数列表
<!--[if !supportLists]-->Ø <!--[endif]-->int pipe(int filedes[2]);//#include <unistd.h>
创建一个管道,返回的两个文件描述字filedes[0]和filedes[1]代表了管道,管道将以此两个文件描述字被使用。其中filedes[0]用来读,filedes[1]用来写。
有权限的每个进程都可以使用管道,因此对于每个进程而言,都拥有此管道的两个文件描述字,用于读写管道。且一个进程中对文件描述字的操作(例如关闭)不影响其它进程中对此管道的使用。
下面的图示单个进程和两个进程中的管道。
<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:356.25pt; height:127.5pt'> <v:imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.emz" o:title="" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
<!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:297pt;height:132pt'> <v:imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image003.emz" o:title="" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
<!--[if !supportLists]-->Ø <!--[endif]-->ssize_t write(int fd, const void *buf, size_t count);
<!--[if !supportLists]-->Ø <!--[endif]-->ssize_t read(int fd, void *buf, size_t count);
<!--[if !supportLists]-->Ø <!--[endif]-->int close(int fd); //#include <unistd.h>
操作文件(广义的:包括普通文件、系统设备和管道)的读写关闭函数。其中fd为文件描述字。
<!--[if !supportLists]-->Ø <!--[endif]-->FILE *popen(const char *command, const char *type); // #include <stdio.h>
调用进程调用此函数,此函数将创建一个进程(shell命令进程)。其中command为具体的shell命令行,type的含义如下:
“r”:调用进程对管道进行读操作,读取的内容是shell命令行的执行结果;
“w”:调用进程对管道进行写操作,shell命令行进程对管道进行读操作;
<!--[if !supportLists]-->Ø <!--[endif]-->int pclose(FILE *stream);
关闭由popen打开的文件。
<!--[if !supportLists]-->2、 <!--[endif]-->实例解析
<!--[if !supportLists]-->Ø <!--[endif]-->simplepipe
单管道半双工双进程程序,父进程向管道写进数据,子进程读取数据。
<!--[if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" style='width:335.25pt;height:140.25pt'> <v:imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image005.png" o:title="" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
<!--[if !supportLists]-->Ø <!--[endif]-->mainpipe
双管道半双工双进程,父进程对pipe1写进文件路径名,子进程对pipe1读取文件路径名,然后子进程对pipe2写进文件内容,最后父进程对pipe2读取文件内容。
<!--[if gte vml 1]><v:shape id="_x0000_i1028" type="#_x0000_t75" style='width:414.75pt;height:195.75pt'> <v:imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image007.emz" o:title="" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
mainpipe.c作用:
创建子进程,关闭两进程对应的管道文件描述字,具体如下:
父进程关闭 fd1[0]和fd2[1];
子进程关闭 fd1[1]和fd[0];
然后调用server和client函数,实现读写管道。
server.c作用:
子进程对pipe1读取路径名,对pipe2写进文件内容
while ( (n = Read(fd, buff, MAXLINE)) > 0) Write(writefd, buff, n); //不断地读取文件的内容,并对pipe2写进文件的内容 //因为文件内容可能很大,需要多次的写pipe2 |
client.c作用:
父进程对pipe1写进路径名,对pipe2读取文件内容
while ( (n = Read(readfd, buff, MAXLINE)) > 0) Write(STDOUT_FILENO, buff, n); //不断地对pipe2读取数据 //因为有进程(子进程)在不断地向pipe2写进数据 |
<!--[if !supportLists]-->Ø <!--[endif]-->mainpopen
由popen函数创建管道,特点是方便用于与shell命令之间的数据交互;隐藏了进程创建的细节。
FILE *fp; snprintf(command, sizeof(command), "cat %s", buff); fp = Popen(command, "r"); //创建管道,调用进程对管道读操作,取得cat命令的内容,保存在fp文件中 while (Fgets(buff, MAXLINE, fp) != NULL) //取出fp文件中的内容 { printf("buff after popen==%s/n",buff); Fputs(buff, stdout); //输出到标准输出 } |
<!--[if !supportLists]-->3、 <!--[endif]-->小结
<!--[if !supportLists]-->Ø <!--[endif]-->管道用于有亲缘关系的进程(父子进程)间进行数据交互
<!--[if !supportLists]-->Ø <!--[endif]-->一个管道有两个文件描述字,对管道有操作权限的每个进程都拥有自己的两个文件描述字,且一个进程对其文件描述字的操作(例如关闭)不影响其它进程对应的文件描述字
<!--[if !supportLists]-->Ø <!--[endif]-->创建管道的方式有两种:pipe和popen。其中后者专门用于与shell命令行进行交互;
<!--[if !supportLists]-->Ø <!--[endif]-->全双工的管道实际上是对两个文件操作字都有了读和写的操作。在半双工管道使用过程中,要关闭不使用的文件描述字。