如何绕过C中的管道缓冲区大小限制?

我正在用C编写类似bash的shell,目前正在处理管道。

例如,当我尝试使用输入较大的管道​​时:

man gcc | grep test

管道缓冲区已满,我的程序被卡住了。 我该如何阻止这种情况的发生?

评论
  • 小草
    小草 回复

    From pipe() man page

    Since Linux 2.6.11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a system with a page size of 4096 bytes). Since Linux 2.6.35, the default pipe capacity is 16 pages, but the capacity can be queried and set using the fcntl(2) F_GETPIPE_SZ and F_SETPIPE_SZ operations.

    From fcntl() man page

    改变管道的容量

    F_SETPIPE_SZ (int; since Linux 2.6.35) Change the capacity of the pipe referred to by fd to be at least arg bytes. An unprivileged process can adjust the pipe capacity to any value between the system page size and the limit defined in /proc/sys/fs/pipe-max-size (see proc(5)). Attempts to set the pipe capacity below the page size are silently rounded up to the page size. Attempts by an unprivi‐ leged process to set the pipe capacity above the limit in /proc/sys/fs/pipe-max-size yield the error EPERM; a privileged process (CAP_SYS_RESOURCE) can override the limit.