Search code examples
clinuxprocesspipepts

Read noonnamed pipe in terminal


Hellow. I have very simple C program. I create pipe in program (standard, non-named). Can I read pipe of existing process in terminal (stream with > or cat?). I try it but my command do nothing. Im know tkat i can create named pipe who is very easy for external I/O. I have number of pipe for /proc/number/fd Why I need it? Just from debug (but not only, i know that gdb can look pipe). When i fork process, children inherits pts (terminal) and std io/out. Change pts is possible but it is bad way. So I will open next terminal and stream existing process pipie in it. It is possible (and decent, dizzy way dont interesting me) or I must used named pipe?


Solution

  • Can I read pipe of existing process in terminal (stream with > or cat?)

    Yes, you can. Example rnpit.c:

    #include <string.h>
    main()
    {
        int pipefd[2];
        pipe(pipefd);
        write(pipefd[1], "pipe", strlen("pipe"));
        sleep(99);  // give us time to read the pipe
    }
    

    >rnpit&
    [1] 1077
    >ll /proc/${!}/fd
    total 0
    lrwx------ 1 armali ARNGO_res4 64 Apr  4 09:22 0 -> /dev/pts/5
    lrwx------ 1 armali ARNGO_res4 64 Apr  4 09:22 1 -> /dev/pts/5
    lrwx------ 1 armali ARNGO_res4 64 Apr  4 09:22 2 -> /dev/pts/5
    lr-x------ 1 armali ARNGO_res4 64 Apr  4 09:22 3 -> pipe:[399466140]
    l-wx------ 1 armali ARNGO_res4 64 Apr  4 09:22 4 -> pipe:[399466140]
    >cat /proc/${!}/fd/3
    pipe