将stdout重定向到POSIX C中的stdin

时间:2018-01-15 10:21:51

标签: c io posix

我正在尝试编写一个将stdout重定向到stdin的程序,主要是为了更好地了解dup2()的工作原理。我尝试编写一个简单的程序,如果你printf()某个东西,你可以稍后scanf()回来,但没有设法让它工作。首先,我尝试使用dup2(0, 1),认为它会将stdout(1)重定向到stdin(0)。这没有用。我认为既然一个用于输入而另一个用于输出它们可能不兼容,所以我尝试使用管道来执行此操作,如下所示:

#include <stdio.h>
#include <unistd.h>
int main() {
    int pipe_desc[2];
    pipe(pipe_desc);
    dup2(pipe_desc[0], 0);
    dup2(pipe_desc[1], 1);
    printf("foo");
    char bar[20];
    scanf("%s", bar);
    fprintf(stderr, "%s", bar);
}

但是,现在即使printf,我理解它应该将"foo"放入管道,当我调用scanf时,程序只挂在那里,并且都不会读取来自stdin,也不是来自管道。怎么了?这段代码究竟发生了什么?这样的重定向是否可以在没有管道(或任何其他辅助结构)的情况下完成?

1 个答案:

答案 0 :(得分:1)

你的作品如何,代码中有解释。

#include <stdio.h>
#include <unistd.h>
int main() {
        int pipe_desc[2];
        pipe(pipe_desc);
        printf("pid = %d \n",getpid());
        /* above statement creates 2 descriptors 3 & 4. 3 is for read and 4 is for write**/
        dup2(pipe_desc[0], 0);
        /** 3 is duplicates with 0(stdin) i.e if you try to do scanf, it won't scan from keyboard, it will scan from pipe(read end, for that make sure something should be there in pipe  **/
        dup2(pipe_desc[1], 1);
        /** 4 is duplicates with 1 (stdout) i.e if you try to write onto screen, it won't. It will try into pipe write end **/
        /** SO TILL NOW FD DETAILS
                0,3 --> PIPE_DESC[0]
                1,4 --> PIPE_DESC[1] 
        **/
        #if 1
        fflush(stdout);
        printf("foo\n");//it won't print 
        char bar[20];
        scanf("%s", bar);//it won't scan bcz 0 is poiting to pipe_desc[0]
        #endif
        /** YOU CAN CHECK USING OTHER TERMINAL "ls -l /proc/pid/fd/" **/

        fprintf(stderr, "%s", bar);// fd 2 is still available.So foo gets printed on screen
}

在程序运行时打开另一个终端,使用

查看fd详细信息
root@xyz-PC:/home/achal/s_flow/cpp# ls -l /proc/4503/fd
total 0
lr-x------ 1 root root 64 Jan 15 16:45 0 -> pipe:[782489]
l-wx------ 1 root root 64 Jan 15 16:45 1 -> pipe:[782489]
lrwx------ 1 root root 64 Jan 15 16:45 2 -> /dev/pts/9
lr-x------ 1 root root 64 Jan 15 16:45 3 -> pipe:[782489]
l-wx------ 1 root root 64 Jan 15 16:45 4 -> pipe:[782489]
相关问题