在终端中读取 noon 命名管道

Read noonnamed pipe in terminal

你好。 我有非常简单的 C 程序。我在程序中创建管道(标准,非命名)。我可以在终端中读取现有进程的管道(使用 > 或 cat 流吗?)。我试过了,但我的命令什么也没做。我知道 tkat 我可以创建命名管道,它对于外部 I/O 来说非常容易。 我有 /proc/number/fd 的管道数量 为什么我需要它?仅来自调试(但不仅如此,我知道 gdb 可以查看管道)。当我 fork 进程时,子进程继承了 pts(终端)和 std io/out。改变点是可能的,但这是不好的方法。所以我将打开下一个终端并在其中流式传输现有流程 pipie。 这是可能的(而且体面,令人眼花缭乱的方式让我不感兴趣)或者我必须使用命名管道?

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

是的,你可以。例子 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