pipe与FIFO功能类似,都是单向通信。管道两端必须都open以后才可以通信,否则先open的一端默认阻塞。
pipe作为无名管道,仅用于近亲进程,例如父子进程,同一个进程内的线程等等。
FIFO作为有名管道,可用于两个独立进程的通信。
介绍如下:
http://man7.org/linux/man-pages/man7/pipe.7.html
pipe
http://man7.org/linux/man-pages/man2/pipe.2.html
mkfifo
http://man7.org/linux/man-pages/man1/mkfifo.1.html
dup/dup2
http://man7.org/linux/man-pages/man2/dup.2.html
示例代码:
#define FIFO_PATH "xxx"
static int stdinfd_fifo = -1;
static int stdoutfd_fifo = -1;
int rc = 0;
/* define 2 FIFOs for stdin and stdout, stderr not covered here */
static char IOfifos[2][64];
rc = mkdir(FIFO_PATH, 0751);
if((rc != 0) && (errno != EEXIST))
{
fprintf(stderr, "failed to create dir[%s], errno[%d], error[%s].\n",
FIFO_PATH, errno, strerror(errno));
return -1;
}
sprintf(IOfifos[0], "my_process_stdin");
rc = mkfifo(IOfifos[0], 0666);
if((rc != 0) && (errno != EEXIST))
{
fprintf(stderr, "mkfifo failed for IOfifos[0]\n");
return -1;
}
sprintf(IOfifos[1], "my_process_stdout");
rc = mkfifo(IOfifos[1], 0666);
if((rc != 0) && (errno != EEXIST))
{
fprintf(stderr, "mkfifo failed for IOfifos[1]\n");
return -1;
}
stdinfd_fifo = open(IOfifos[0], O_RDONLY);
if (stdinfd_fifo < 0)
{
...
}
stdoutfd_fifo = open(IOfifos[1], O_WRONLY);
if (stdoutfd_fifo < 0)
{
...
}
rc = dup2(stdinfd_fifo, 0); /* make stdin 0 pointing to FIFO descriptor. */
if (rc != 0)
{
......
}
rc = close(stdinfd_fifo);
if (rc != 0)
{
......
}
rc = dup2(stdoutfd_fifo, 1); /* make stdout 1 pointing to FIFO descriptor. */
if (rc != 0)
{
......
}
rc = close(stdoutfd_fifo);
if (rc != 0)
{
......
}
注意,close(stdinfd_fifo)和close(stdoutfd_fifo)并不会关闭管道文件,
因为还有其他文件描述符(0和1)指向管道文件。
https://linux.die.net/man/3/close
When all file descriptors associated with a pipe or FIFO special file are closed,
any data remaining in the pipe or FIFO shall be discarded.
open默认是阻塞的,可以通过设置O_NONBLOCK变为非阻塞。
查看管道容量:
/proc/sys/fs/pipe-max-pages (only in Linux 2.6.34)
An upper limit, in pages, on the capacity that an unprivileged
user (one without the CAP_SYS_RESOURCE capability) can set for
a pipe.
The default value for this limit is 16 times the default pipe
capacity (see above); the lower limit is two pages.
This interface was removed in Linux 2.6.35, in favor of
/proc/sys/fs/pipe-max-size.
/proc/sys/fs/pipe-max-size (since Linux 2.6.35)
The maximum size (in bytes) of individual pipes that can be
set by users without the CAP_SYS_RESOURCE capability. The
value assigned to this file may be rounded upward, to reflect
the value actually employed for a convenient implementation.
To determine the rounded-up value, display the contents of
this file after assigning a value to it.
The default value for this file is 1048576 (1 MiB). The
minimum value that can be assigned to this file is the system
page size. Attempts to set a limit less than the page size
cause write(2) to fail with the error EINVAL.
Since Linux 4.9, the value on this file also acts as a ceiling
on the default capacity of a new pipe or newly opened FIFO.
/proc/sys/fs/pipe-user-pages-hard (since Linux 4.5)
The hard limit on the total size (in pages) of all pipes
created or set by a single unprivileged user (i.e., one with
neither the CAP_SYS_RESOURCE nor the CAP_SYS_ADMIN
capability). So long as the total number of pages allocated
to pipe buffers for this user is at this limit, attempts to
create new pipes will be denied, and attempts to increase a
pipe's capacity will be denied.
When the value of this limit is zero (which is the default),
no hard limit is applied.
/proc/sys/fs/pipe-user-pages-soft (since Linux 4.5)
The soft limit on the total size (in pages) of all pipes
created or set by a single unprivileged user (i.e., one with
neither the CAP_SYS_RESOURCE nor the CAP_SYS_ADMIN
capability). So long as the total number of pages allocated
to pipe buffers for this user is at this limit, individual
pipes created by a user will be limited to one page, and
attempts to increase a pipe's capacity will be denied.
When the value of this limit is zero, no soft limit is
applied. The default value for this file is 16384, which
permits creating up to 1024 pipes with the default capacity.
参考: