Improve explanation of fifos. master
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 23 Jan 2024 19:57:30 +0000 (20:57 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 24 Jan 2024 14:29:43 +0000 (15:29 +0100)
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 <andrea.movilli@tuebingen.mpg.de>
Unix_Concepts.m4

index be51cc9096ad4af3e9bb5721e1bd67774f776be6..7f0b5d8b13f55ba701925ed09eeb9f6c417028d3 100644 (file)
@@ -1790,8 +1790,7 @@ SUBSECTION(«Pipes and Redirections»)
 
 <p> The <code>pipe(2)</code> system call takes no arguments and
 creates two file descriptors for the calling process which are tied
 
 <p> The <code>pipe(2)</code> 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 <em>read end</em> of the pipe, the other is
 the <em>write end</em>. Data written to the write end is buffered by
 the kernel and can be obtained by reading from the read end. </p>
 file descriptor is the <em>read end</em> of the pipe, the other is
 the <em>write end</em>. Data written to the write end is buffered by
 the kernel and can be obtained by reading from the read end. </p>
@@ -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. </p>
 
 communicate with the child by writing a message to the write end of
 the pipe for the child to read. </p>
 
+<p> This approach depends on file descriptor inheritance across
+<code>fork(2)</code>, so it does not work in the situation
+where neither process is an ancestor of the other. Files of
+type <em>fifo</em> (named pipes) overcome this restriction. To
+establish a connection between two <em>unrelated</em> processes,
+both processes call <code>open(2)</code> to obtain a file
+descriptor which is associated with the fifo. One process passes
+the <code>O_WRONLY</code> flag to open the file for writing while
+the other passes <code>O_RDONLY</code> to open it for reading. The
+two processes may then communicate in the same way as with the
+<code>pipe(2)/fork(2)</code> approach. </p>
+
 <p> The POSIX <code>dup(2)</code> and <code>dup2(2)</code> 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
 <p> The POSIX <code>dup(2)</code> and <code>dup2(2)</code> 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 <code>wc(1)</code>. Since <code>ls(1)</code> writes to stdout
 and <code>wc(1)</code> reads from stdin, <code>wc(1)</code> processes
 the output of <code>ls(1)</code>. </p>
 
 and <code>wc(1)</code> reads from stdin, <code>wc(1)</code> processes
 the output of <code>ls(1)</code>. </p>
 
-<p> Note that this trick does not work to establish a connection
-between two <em>existing</em> processes because it depends on file
-descriptor inheritance across <code>fork(2)</code>. In the general
-case one has to fall back to sockets or fifos to create the data
-channel. </p>
-
 SUBSECTION(«Stdio»)
 
 <p> The POSIX standard requires a compliant Unix system to provide
 SUBSECTION(«Stdio»)
 
 <p> The POSIX standard requires a compliant Unix system to provide