From 6db5ba9acf7487b4fad592445e74bdb516d7f9c7 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 23 Jan 2024 20:57:30 +0100 Subject: [PATCH] Improve explanation of fifos. Fifos are introduced as one of the seven file types but the explanation given there does not include typical use cases. Add a paragraph about fifos in the section on pipes and redirections to elaborate a little how fifos facilitate inter process communication. Suggested-by: Andrea Movilli --- Unix_Concepts.m4 | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Unix_Concepts.m4 b/Unix_Concepts.m4 index be51cc9..7f0b5d8 100644 --- a/Unix_Concepts.m4 +++ b/Unix_Concepts.m4 @@ -1790,8 +1790,7 @@ SUBSECTION(«Pipes and Redirections»)

The pipe(2) system call takes no arguments and creates two file descriptors for the calling process which are tied -together as a unidirectional first in, first out data channel that -works just like a fifo, but without any files being involved. One +together as a unidirectional first in, first out data channel. One file descriptor is the read end of the pipe, the other is the write end. Data written to the write end is buffered by the kernel and can be obtained by reading from the read end.

@@ -1803,6 +1802,18 @@ a copy of both pipe file descriptors. Hence the parent process can communicate with the child by writing a message to the write end of the pipe for the child to read.

+

This approach depends on file descriptor inheritance across +fork(2), so it does not work in the situation +where neither process is an ancestor of the other. Files of +type fifo (named pipes) overcome this restriction. To +establish a connection between two unrelated processes, +both processes call open(2) to obtain a file +descriptor which is associated with the fifo. One process passes +the O_WRONLY flag to open the file for writing while +the other passes O_RDONLY to open it for reading. The +two processes may then communicate in the same way as with the +pipe(2)/fork(2) approach.

+

The POSIX dup(2) and dup2(2) system calls allow a process to manipulate the entries of its file descriptor array. In particular the standard file descriptors 0, 1, and 2 can be @@ -1827,12 +1838,6 @@ with wc(1). Since ls(1) writes to stdout and wc(1) reads from stdin, wc(1) processes the output of ls(1).

-

Note that this trick does not work to establish a connection -between two existing processes because it depends on file -descriptor inheritance across fork(2). In the general -case one has to fall back to sockets or fifos to create the data -channel.

- SUBSECTION(«Stdio»)

The POSIX standard requires a compliant Unix system to provide -- 2.39.2