title: Read / Write through a pipe in C
body: <p>I started today working with pipe() and fork() and exec() in C, and I now have a problem:</p>
  
  <p>The main program, creates two pipes and forks. The child process does an exec() to another program that now is only a test program which reads from stdin, communicates with its parent and writes through stdout ther results. The main program is supposed to recive data, communicate with a SQLite 3 database and return data using pipes. This part of the problem is solved, the pipes are open and closed properly and there's communication.</p>
  
  <p>Then the problem is, in the child process (the one which is called with exec()) in a certain point, I have this:</p>
  
  <pre><code>printf("Strid sent: %s\n", strid);
  write(4,strid,sizeof(strid));
  
  printf("Str sent: %s\n", str);
  write(4,str,sizeof(str));
  </code></pre>
  
  <p>And the parent should be reading properly with this part:</p>
  
  <pre><code>read(select[0],strid, sizeof(strid));
  printf("Strid recived: %s\n",strid);
  int id = atoi(strid);
  printf("Id recived: %d\n",id);
  
  read(select[0],buffer, sizeof(buffer));
  printf("Buffer recived: %s\n",buffer);
  </code></pre>
  
  <p>But what I recive whith those printf is:</p>
  
  <pre><code>Strid sent: 1
  Str sent: start
  Strid recived: 1
  Id recived: 1
  Buffer recived: 7� (and other strange characters)
  </code></pre>
  
  <p>As you can see, the problem is in the receiving of the second command (and that part is copied as is, there's no other code into it).</p>
  
  <p>I have to say too that "buffer" variable, which recives str, is declared as char buffer[20] and has not been used before the read()</p>
  
  <p>Thank you in advance!</p>