- 论坛徽章:
- 0
|
借楼主的地盘问一个问题
我在思考楼主碰到的问题的时候,自己写了个测试程序
结果屏幕打出
bbb
this will not appear STDOUT_FILENO
write to STDOUT_FILENO
不太明白的是this will not appear STDOUT_FILENO 这句话为什么也送到了终端显示,而且为什么会显示在bbb之后
程序如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main(void)
- {
- int fdcopy = 9;
- int fd[2];
- if (pipe(fd) == -1)
- {
- printf("pipe() error\n");
- exit(0);
- }
-
- if (dup2(STDOUT_FILENO, fdcopy) == -1)
- {
- printf("dup2() error\n");
- exit(0);
- }
- if (dup2(fd[1], STDOUT_FILENO) == -1)
- {
- printf("dup2() error for pipe[1]\n");
- exit(0);
- }
- write(STDOUT_FILENO, "aaa\n", sizeof("aaa\n"));
- printf("this will not appear STDOUT_FILENO\n");
- if (dup2(fdcopy, STDOUT_FILENO) == -1)
- {
- printf("restore STDOUT_FILENO error\n");
- exit(0);
- }
- write(STDOUT_FILENO, "bbb\n", sizeof("bbb\n"));
- printf("write to STDOUT_FILENO\n");
- return 0;
- }
复制代码 |
|