ITeye博客

c中的管道及复制描述符

sealbird · 2011-01-10 17:22 · C++ · 编程语言 · C · C++ · Blog · Unix · C# · Markdown

1、

       #include <stdio.h>
	   #include <stdlib.h>
	   #include <unistd.h>
	   int main()
	   {       
		   printf("======0========");
		   sleep(4);
		   int pfds[2]; 
		   if ( pipe(pfds) == 0 ){ 
				 if ( fork() == 0 )  { 
					 close(1);//关闭管道的写入端(在子进程中)
					 dup2( pfds[1], 1 );//复制管道的写入端(在子进程中)
					 close( pfds[0] );//关闭管道的读入端(在子进程中)
					 printf("======1========");
					 execlp( "ls", "ls", "-1", NULL ); 
				 } else {
					  close(0);//关闭管道的读入端(在父进程中)
					 dup2( pfds[0], 0 );//复制管道的读入端(在父进程中)
					 close( pfds[1] );//关闭管道的写入端(在父进程中)
					  printf("======2========");
					 execlp( "wc", "wc", "-l", NULL ); 
					 
				 }
		  } 
		  return 0;
	  }

2、

#include <stdio.h>
#include <unistd.h>
void main()
{
	int filedes[2];
	char buffer[80];
	pipe(filedes);
	if(fork()>0){
		/* 父进程*/
		char s[ ] = "hello!\n";
		write(filedes[1],s,sizeof(s));
	}
	else{
		/*子进程*/
		read(filedes[0],buffer,80);
		printf("%s",buffer);
	}
}

3、 from http://hi.baidu.com/featherain/blog/item/c2a6ab8217a521dc9023d9e7.html

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;                           
}

在上例中,父进程的输出被重定向到管道输入端,子进程从管道的输出端读数据。 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。