Note that there are some explanatory texts on larger screens.

plurals
  1. POReading from FIFO in C: select() doesn't return?
    primarykey
    data
    text
    <p>I've got a C program I'm writing. Here's what it does:</p> <ul> <li>Create n fifos using mkfifo</li> <li>Open them for read (with the O_NONBLOCK flag set)</li> <li>Open them for write</li> <li>Spawn a thread</li> </ul> <p>In the thread, run in a loop:</p> <ul> <li>Create an fd_set of the file descriptors for all n fifos</li> <li>Call select(n, &amp;my_set, NULL, NULL, NULL)</li> <li>For each fd ready for I/O (FD_ISSET(fd, &amp;my_set)): <ul> <li>Read a string from the fd (read(fd, buf, buf_len))</li> <li>Print the string</li> <li>If string == "kill", mark the fd as dead and remove it from the list (n--)</li> <li>If n == 0, terminate the thread</li> </ul></li> </ul> <p>In the main program:</p> <ul> <li>For i = 0 to n <ul> <li>Write to fds[i] with a string (write(fds[i], buf, buf_len))</li> </ul></li> <li>For i = 0 to n <ul> <li>Write to fds[i] with the string "kill"</li> </ul></li> <li>Join on the thread I created</li> <li>Exit</li> </ul> <p>The behavior I'm seeing is that select() will return once with a length of 1, being the first fd in the list. The second time through the loop, select will just sit there forever.</p> <p>Here's my output:</p> <pre><code>thread created Waiting on 4 file descriptors &gt; Wrote 'Hello to target 0 from writer 0' to 0 &gt; Wrote 'Hello to target 0 from writer 1' to 1 &gt; Wrote 'Hello to target 1 from writer 0' to 2 &gt; Wrote 'Hello to target 1 from writer 1' to 3 &gt; Sending kill to 0:0 (#0) &gt; Sending kill to 0:1 (#1) &gt; Sending kill to 1:0 (#2) &gt; Sending kill to 1:1 (#3) &lt; Got string: 'Hello to target 0 from writer 0' Waiting on 4 file descriptors ^C </code></pre> <p>The OS is Linux, in case it matters.</p> <p>Link to the code: <a href="https://dl.getdropbox.com/u/188590/fifotest.c" rel="nofollow noreferrer">https://dl.getdropbox.com/u/188590/fifotest.c</a> (Sorry it's a bit heinous)</p> <p>Thanks, Nathan</p>
    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.
 

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