title: All threads blocked by empty pipe read
body: <p>I'm trying to teach myself about multi-threading and multi-process programming in C (Linux). I wrote a short program which spawns a new thread which goes to a routine that tries to do a blocking read from an empty FIFO, while the main thread continues and prints to STDOUT. (Note: I did create a FIFO using <code>mkfifo newfifo</code> in my terminal before executing the program)</p>
  
  <p>I was expecting the program to print to screen "Main thread", and then block while waiting for me to put data in the FIFO. Instead, the entire process is blocked, and the message "Main thread" only prints after I've put data into the FIFO.</p>
  
  <p>Am I missing something here? Shouldn't the main thread continue to run even though the spawned thread is blocked? I tried a test using fork and creating a child process and got the same result (both processes blocked by read from empty FIFO).</p>
  
  <p>Code is below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;pthread.h&gt;
  
  #define NEWPIPE "./newfifo"
  
  typedef struct __attribute__ ((__packed__)) {
    int reserved      :30;
    int threadStarted :1;
    int msgRcved      :1;
  } Status;
  
  void* thread_routine(int fd, char* buffer, Status* myStatus)
  {
    int great_success = 0; 
  
    myStatus-&gt;threadStarted = 1;
    printf("Side thread\n");
  
    great_success = read(fd, buffer, sizeof(buffer));
  
    if (great_success &lt; 0) {
      printf("pipe failed\n");
    } else {
      myStatus-&gt;msgRcved = 1;
    }
  }
  
  void main()
  {
    int fd;
    int cnt = 0;
    char buffer[20];
    Status* myStatus;
    pthread_t thread_id;
  
    myStatus = (Status*) malloc(sizeof(Status));
    myStatus-&gt;reserved      = 0;
    myStatus-&gt;threadStarted  = 0;
    myStatus-&gt;msgRcved      = 0;
  
    fd = open(NEWPIPE, O_RDONLY);
  
    pthread_create(&amp;thread_id, 
                    NULL, 
                    (void *) thread_routine(fd, buffer, myStatus), 
                    NULL);
  
    printf("Main thread\n");
  
    while (!myStatus-&gt;threadStarted) {
      printf("Main thread: side thread started!\n");
    }
  
    while (!myStatus-&gt;msgRcved) {
      sleep(1);
      cnt++;
    } 
  
    printf("buffer (cnt = %d): %s\n", cnt, buffer);
  
  }
  </code></pre>
  
  <p><strong>Edit: Latest Code</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;pthread.h&gt;
  
  #define NEWPIPE "./newfifo"
  
  struct Status {
    unsigned int reserved      :30;
    unsigned int threadStarted :1;
    unsigned int msgRcved      :1;
  };
  
  void* thread_routine(void *arg)
  {
    int great_success = 0; 
    int fd;
    char buffer[20];
    struct Status* myStatus;
  
    fd = open(NEWPIPE, O_RDONLY);
  
    myStatus = arg;
  
    myStatus-&gt;threadStarted = 1;
    printf("Side thread\n");
  
    while (1) {
      great_success = read(fd, buffer, 20);
  
      if (great_success &lt; 0) {
        printf("pipe failed\n");
      } else {
        printf("buffer : %s\n", buffer);
        printf("great_success = %d\n", great_success);
        great_success = 0;
      }
    }
  }
  
  void main()
  {
    int cnt = 0;
    struct Status* myStatus;
    pthread_t thread_id;
  
    myStatus = (struct Status*) malloc(sizeof(struct Status));
    myStatus-&gt;reserved      = 0;
    myStatus-&gt;threadStarted  = 0;
    myStatus-&gt;msgRcved      = 0;
  
    pthread_create(&amp;thread_id, 
                    NULL, 
                    &amp;thread_routine,
                    (void *) myStatus);    // arguments to pass to the function!
  
  
    printf("Main thread\n");
  
    while (!myStatus-&gt;msgRcved) {
      printf("Main thread: side thread started!\n");
  
      if (myStatus-&gt;threadStarted) {
        printf("spawned thread started!\n");
      }
  
      sleep(1);
    }
  
    pthread_exit(NULL);
  
  }
  </code></pre>