title: Write pid to fifo - C
body: <p>I've got two C files, server.c and client.c.  The server has to create a fifo file and constantly read in it, waiting for input. The client gets its PID and writes the PID in the fifo.
  This is my server file which I launch first:</p>
  
  <pre><code>int main(){
    int fd;
    int fd1;
      int bytes_read;
      char * buffer = malloc(5);
      int nbytes = sizeof(buffer);
  
      if((fd = mkfifo("serverfifo",0666)) == -1) printf("create fifo error");
      else printf("create fifo ok");
  
      if ((fd1 = open("serverfifo",O_RDWR)) == -1) printf("open fifo error");
      else{
          printf("open fifo ok"); 
          while(1){
              bytes_read = read(fd,buffer,nbytes);
              printf("%d",bytes_read);
              }
          }
  
  return(0);
  }
  </code></pre>
  
  <p>And my client file :</p>
  
  <pre><code>int main(){
  
      int fd;
      int pid = 0;
      char *fifo;
      int bytes;
  
      if ((pid = getpid()) == 0)  printf("pid error");
      char pid_s[sizeof(pid)];
      sprintf(pid_s,"%d",pid); 
  
  
     if ((fd = open ("serverfifo",O_RDWR)) == -1)printf("open fifo error");
     else {
      printf("open fifo ok");
  
          bytes = write(fd,pid_s, sizeof(pid_s));
          printf("bytes = %d",bytes);
  
     }
  
      close(fd);
  return(0);
  }
  </code></pre>
  
  <p>The two main problems I'm getting are: When I write the pid to the file it returns the number of bytes I wrote so it looks okay but when I check the properties of the fifo file it says 0 bytes. The second problem is the read doesn't work.  If I do a printf before it shows, but after it doesn't and the read isn't returning anything it just freezes.
  I realise there are a lot of similar posts on the site but I couldn't find anything that helped.
  I'm using Ubuntu and GCC compiler with CodeBlocks.</p>