Thread: Need Help in Creating Pipes in Linux

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Need Help in Creating Pipes in Linux

    Hi

    I was trying to learn pipe but I the program is not giving me the expected output.

    The program is as below:

    Code:
    //#include <iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    #include<sys/types.h>
    #include<unistd.h>
    #include<unistd.h>
    #include <string.h>
    
    
    
    int main(int argc, char **argv) {
      int pfd[2];
      char buf[30];
      pipe(pfd);
      
      if(fork())
      {
        printf("\n Child writing to pipe");
        write(pfd[0],"Testing Pipe",24);
        printf("\n Child exiting\n");
        exit(0);
      }
      
      else{
        memset(buf,0x00,sizeof(buf));
        printf("\n Parent Reading");
        read(pfd[1],buf,5);
        printf("\n PArent REad %s\n",buf);
        
      }
      
        return 0;
    }
    Can Anybody help me what I am doing wrong?

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Are you sure you're reading and writing to the proper file descriptors? Compare with this example:

    6.2.2 Creating Pipes in C

    Also in your line for write, where does 24 come from? It seems that will access beyond the end of your string buffer. It might cause UB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New article on creating shared libraries on Linux with GCC
    By webmaster in forum Linux Programming
    Replies: 8
    Last Post: 01-23-2012, 07:57 PM
  2. creating an array of pipes
    By arkashkin in forum Linux Programming
    Replies: 1
    Last Post: 08-30-2010, 09:22 AM
  3. creating movies on linux
    By Corrington_j in forum Linux Programming
    Replies: 6
    Last Post: 07-19-2005, 09:11 PM
  4. Creating large document in linux
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-16-2005, 08:48 AM
  5. windows in linux (creating)
    By Raven Arkadon in forum Linux Programming
    Replies: 3
    Last Post: 09-01-2002, 08:51 AM