Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that '<code>dup(p[1])</code>' means you have two file descriptors pointing to the same file. It does not close <code>p[1]</code>; you should do that explicitly. Likewise with '<code>dup(p[0])</code>'. Note that a file descriptor reading from a pipe only returns zero bytes (EOF) when there are no open write file descriptors for the pipe; until the last write descriptor is closed, the reading process will hang indefinitely. If you <code>dup()</code> the write end, there are two open file descriptors to the write end, and both must be closed before the reading process gets EOF.</p> <p>You also do not need or want the <code>wait()</code> call in your code. If the <code>ls</code> listing is bigger than a pipe can hold, your processes will deadlock, with the child waiting for <code>ls</code> to complete and <code>ls</code> waiting for the child to get on with reading the data it has written.</p> <p>When the redundant material is stripped out, the working code becomes:</p> <pre><code>#include &lt;unistd.h&gt; int main(void) { int p[2]; pid_t ret; pipe(p); ret = fork(); if (ret == 0) { close(1); dup(p[1]); close(p[0]); close(p[1]); execlp("ls", "ls", "-l", (char *) 0); } else if (ret &gt; 0) { close(0); dup(p[0]); close(p[0]); close(p[1]); execlp("wc", "wc", "-l", (char *) 0); } return(-1); } </code></pre> <p>On Solaris 10, this compiles without warning with:</p> <pre><code>Black JL: gcc -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -o x x.c Black JL: ./x 77 Black JL: </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload