管道
<!--[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]-->全双工的管道实际上是对两个文件操作字都有了读和写的操作。在半双工管道使用过程中,要关闭不使用的文件描述字。
分享到:
相关推荐
该程序是我写的博客“一起talk C栗子吧(第八十八回:C语言实例--使用管道进行进程间通信一)”的配套程序,共享给大家使用
该程序是我写的博客“一起talk C栗子吧(第九十回:C语言实例--使用管道进行进程间通信三)”的配套程序,共享给大家使用
该程序是我写的博客“一起talk C栗子吧(第八十九回:C语言实例--使用管道进行进程间通信二)”的配套程序,共享给大家使用
android IPC及原理简介 Linux 系统中进程间通信的方式有:socket, named pipe,message queque, signal,share memory。Java系统中的进程间通信方式有socket, named pipe等。android应用程序理所当然可以应用JAVA的IPC...
Named-pipe communication for IPC (CSNamedPipeCommunication) - VS2012 跨进程管道通信 C#
IPC 读书笔记及测试程序 PIPE FIFO Mutex Conditional Variable Semophore
linux 进程间通信 pipe和fifo实现 。(此为博客http://blog.csdn.net/shallnet 文章对应源码下载)
EasyPipes 用于NamedPipe和基于TCP的IPC的简单C#库针对.NET Standard 2.0编写,应该与大多数现代.NET版本兼容。用法服务器使用情况: using EasyPipes ;// IService defines the IPC interfacepublic interface ...
demo socket pipe
轻量级库,用于通过.NET Core中IPC的命名管道和匿名管道进行方法调用。 支持与回调的双向通信。 客户端到服务器的呼叫 var pipeServer = new PipeServer ( new NetJsonPipeSerializer (), " mypipe " , () => ...
它的工作方式只是通过弥合Windows命名管道( \\.\pipe\discord-ipc-0 )和Unix域套接字( /run/user/{userid}/discord-ipc-0 )之间的差距。 理想情况下,这样的事情将在wine本身中实现,应该可以添加某种“映射的...
消息(端口,节点,邀请,句柄等) (实际的.mojom IDL方法调用) 控制消息(读/写X字节) 音频同步消息( \pipe\chrome.sync.xxxxx ) 也欢迎您查看。 但是,该项目将看不到任何没有通过管道传输的内容,这主要是...
这个错误是在Master进程向子进程send消息时发现pipe已经关闭了的情况下由Master进程报出来的。 2019-12-16 14:33:02,pid-170096, Error [ERR_IPC_CHANNEL_CLOSED]: channel closed at ChildProcess.target.send ...
Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间。...2、管道(Pipe) 管道用来连接不同进程之间的数据流。 (1)在两个程序之间传递数据的最简单的方法是使用popen()和pclose()函数:
Linux中进程的通信方式有信号,管道,共享内存,消息队列socket等。其中管道是*nix系统进程间通信的...单个进程中的管道几乎没有任何作用,通常会先调用pipe,然后调用fork,从而创建从父进程到子进程的IPC通道。 Linu
linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、信号(signal)、消息队列、共享内存、信号量、套接字(socket)
linux下进程间通信的几种主要方式:管道(pipe)和有名管道(FIFO)、信号(signal)、消息队列、共享内存(shared memory)、信号量(semaphore)、套接字(socket),本文对这些做简单介绍
npm install named-pipe 该库创建Linux管道,而无需使用C ++绑定并通过Shell执行。 依存关系 没有任何! 您的外壳需要支持mkfifo 。 在外壳中尝试mkfifo --help ,看看是否可以访问它。 凉爽的! 我该如何处理管道...