分类: LINUX
2008-04-03 10:19:16
#include
int dup( int oldfd );
int dup2( int oldfd, int targetfd )
int fd1, fd2;
...
fd2 = dup( fd1 );
int oldfd;
oldfd = open("app_log", (O_RDWR | O_CREATE), 0644 );
dup2( oldfd, 1 );
close( oldfd );
示例代码3:利用C实现命令的流水线操作的代码
1: #include
2: #include
3: #include
4:
5: int main()
6: ...{
7: int pfds[2];
8:
9: if ( pipe(pfds) == 0 ) ...{
10:
11: if ( fork() == 0 ) ...{
12:
13: close(1);
14: dup2( pfds[1], 1 );
15: close( pfds[0] );
16: execlp( "ls", "ls", "-1", NULL );
17:
18: } else ...{
19:
20: close(0);
21: dup2( pfds[0], 0 );
22: close( pfds[1] );
23: execlp( "wc", "wc", "-l", NULL );
24:
25: }
26:
27: }
28:
29: return 0;
30: }