2009-12-29 11:46管道在unix类系统中是一个很重要的概念,文件的重定向就是使用管道来实现的,在shell中也提供了"|"来直接使用管道,那么在C中,如何使用管道呢?
管道在unix中是通过pipe函数来实现的。
头文件: #include <unistd.h>
函数定义:int pipe(int filedes[2]);
函数说明:
函数返回文件描述符,其中filedes[0]为管道的写入端,filedes[1]为管道的读取端
函数实例(一)pipe的简单使用:
#include <unistd.h>
#include <stdio.h>
int main( void )
{
int filedes[2];
char buf[80];
pid_t pid;
pipe( filedes );
if ( (pid=fork()) > 0 )
{
printf( "This is in the father process,here write a string to the pipe.\n" );
char s[] = "Hello world , this is write by pipe.\n";
write( filedes[1], s, sizeof(s) );
close( filedes[0] );
close( filedes[1] );
}
else
{
printf( "This is in the child process,here read a string from the pipe.\n" );
read( filedes[0], buf, sizeof(buf) );
printf( "%s\n", buf );
close( filedes[0] );
close( filedes[1] );
}
waitpid( pid, NULL, 0 );
return 0;
}
函数输出结果:
This is in the child process,here read a string from the pipe.
This is in the father process,here write a string to the pipe.
Hello world , this is write by pipe.
函数实例(二)使用dups文件重定向和父子进程的数据交互:
在shell命令下输入:
#cat file | grep "string"
grep 命令将使用cat命令的输出作为自己的输入,这个就是简单的用管道实现文件重定向的例子。
为了实现父子进程间的管道,我们需要将父进程的标准输出重定向到管道的输入端,将子进程的标准输入重定向到管道的标准输出端,这样父进程的输出就可以作为子进程的输入来使用了。
实现文件重定向:
只需要将管道的输入端(输出端)复制到文件描述符0(1)即可
dup2(fd1, 0); /* 复制fd1到文件描述符0中,更改标准输入为fd1,这里fd1应为管道的输入端 */
dup2(fd2, 1); /* 复制fd2到文件描述符1中,更改标准输出为fd2,这里fd2应为管道的输出端 */
dup2(fd3, 2); /* 复制fd3到文件描述符2中,更改标准错误输出为fd3 */
看一个例子:
#include <unistd.h>
#include <stdio.h>
int main()
{
int fildes[2];
pid_t pid;
int i, j;
char buf[256];
if (pipe(fildes) < 0 || (pid = fork()) < 0) /* 创建管道和子进程 */
{
fprintf(stderr, "error!n");
return 1;
}
if (pid == 0)
{
close(fildes[1]);
dup2(fildes[0], 0);
close(fildes[0]);
gets(buf);
fprintf(stderr, "child:[%s]n", buf);
return 2;
}
close(fildes[0]);
dup2(fildes[1], 1);
close(fildes[1]);
puts("Hello!");
return 0;
}
在上例中,父进程的输出被重定向到管道输入端,子进程从管道的输出端读数据。
详细解决方案
UNIX管道编程——使用pipe函数,dup函数,dup2函数
热度:48 发布时间:2024-01-15 22:27:29.0
相关解决方案
- 学java学什么开发平台好?unix/linux解决方法
- sco unix 上的informix怎么建立新的数据库
- sco unix 5.0.7 +informix online 5.0忽然用esql不能编译了,请高人指点
- 请问 unix 上 informax 表中的数据怎么 export 出文本中
- sco unix 5.0.7 +informix online 5.0突然用esql不能编译了,请高人指点。该怎么处理
- sco unix 下的informix怎么建立新的数据库
- 请问 unix 下 informax 表中的数据怎么 export 出文本中
- unix 下操作sybase 有关问题.各位不吝赐教
- unix 安装db2 大家有遇到类似的有关问题吗?如何解决啊
- unix shell报错: Segmentation Fault解决方案
- 玩LINUX/UNIX/SOLARIS的请进.只给有意义回答的给分
- unix shell报错: Segmentation Fault解决方法
- Hp Unix 或Linux下用tar归档,Aix Unix无法解归档,该怎么处理
- 为什么PHP的时间戳被成为 Unix 时间戳?解决方案
- WebSphere概略文件管理(unix)
- ,unix+mysql+tomcat网页都打不开啊
- 正版oracle 10g for unix aix ,12张光盘只用了1张?该怎么处理
- unix shell 逐行读文件并赋值给变量解决方法
- unix shell 调用sqlplus 遍历表,该如何处理
- unix 上安装java的有关问题
- unix 下的java 程序设计入门有关问题
- client.cd("");怎么使用 怎么经入子目录(UNIX)
- java.io.IOException: write failed: EPIPE (Broken pipe),该怎么解决
- 在android使用httpclient时出现“SocketException: Broken Pipe”的解决办法
- Unix 下字符匹配的有关问题
- sco unix 5.0.7 +informix online 5.0突然用esql不能编译了,请高人指点。解决方案
- (2.3)Unix/Linux操作系统体系结构,信号编程初步
- 文件流管道(pipe)
- 【pytorch】BrokenPipeError: [Errno 32] Broken pipe
- unix time 和北京时间相互转换的公式