Using pipes in Linux with C

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-05-10T16:03:12Z Indexed on 2010/05/10 16:14 UTC
Read the original article Hit count: 308

Filed under:
|
|
|

Hi, I'm doing a course in Operating Systems and we're supposed to learn how to use pipes to transfer data between processes.

We were given this simple piece of code which demonstrates how to use pipes,but I'm having difficulty understanding it.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main()
{
      int  pipefd [2], n;
      char buff[100] ;


      if( pipe( pipefd) < 0)
      {
        printf("can not create pipe \n");
      }
      printf("read fd = %d, write fd = %d \n", pipefd[0], pipefd[1]);
      if ( write (pipefd[1],"hello world\n", 12)!= 12)
      {
        printf("pipe write error \n");
      }
      if(  ( n = read ( pipefd[0] , buff, sizeof ( buff)  ) ) <= 0 )
      {
        printf("pipe read error \n");
      }
      write ( 1, buff, n ) ;
exit (0);
  }

What does the write function do? It seems to send data to the pipe and also print it to the screen (at least it seems like the second time the write function is called it does this).

Does anyone have any suggestions of good websites for learning about topics such as this, FIFO, signals, other basic linux commands used in C?

© Stack Overflow or respective owner

Related posts about pipes

Related posts about c