Thread: Problem with pipe IPC

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    3

    Problem with pipe IPC

    I'm currently studying IPC, I have a first program

    A: Do an exec for B and wait

    B: Receive through a fifo a string from a third program "C" and have to resend it to A

    I was thinking to open a pipe in A before the exec, then passing fd[1] to B as an argument

    Code:
    if(pipe(fd)==-1){
        perror("Pipe Failed");
        myExit(EXIT_FAILURE);
    }
    
    close(fd[1]);   
    sprintf(fdString,"%d",fd[1]);
    
        .......
    
    if((int)(pid=fork())>0)
    
        waiting(status);    
    
    else if(pid==0){    
    
        close(fd[0]);   
    
        execl("./B","B",fdString,(char*)0);
        perror("Exec failed");
        myExit(EXIT_FAILURE);
    
    }
    Then in B:

    Code:
    int fd=atoi(argv[1]);
    
        //Receive string from C 
    
        len=strlen(string)+1;
    
    if(write(fd,&len,sizeof(int))==-1){
        perror("Error on writing length");
        exit(EXIT_FAILURE); 
    }
    
    if(write(fd,&string,len)==-1){
        perror("Error on writing string");
        exit(EXIT_FAILURE);     
    }

    My problem now is reading this string in A. I was thinking to send a SIGUSR1 to A as soon as the string is written by B on the pipe and having in A something like:


    Code:
    signal(SIGUSR1, signalHandler);
        ........
        static void signalHandler(int signo){
        switch(signo){
        case SIGUSR1:
            listen();
            break;
        default: break;
    
        }
    }
        ........
    static void listen(){
    
        int len;
    
        if(read(fd[0],&len,sizeof(int))<sizeof(int)){
            perror("Error reading len");
            myExit(EXIT_FAILURE);   
        }
    
        char string[len];
    
        if(read(fd[0],string,len)<len){
            perror("Error reading string");
            myExit(EXIT_FAILURE);   
        }
        printf("String: %s with length %d\n", string, len);
    }

    However what I get is "Error reading len: Success" , what's wrong ?

    Sorry if my English is bad, any help is appreciated, thanks!
    Last edited by cifz; 07-18-2012 at 02:48 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In the first code block on line 6 you close the output side of the pipe, but you haven't forked yet, so the output will also be closed for B. Fork first, then close the unneeded side of the pipe.

    Instead of waiting in the parent, why not just do the read? It will block until it gets something.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Along with what oogabooga said, may I suggest this fine tutorial on *nix IPCs: Beej's Guide to Unix IPC.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    Quote Originally Posted by oogabooga View Post
    In the first code block on line 6 you close the output side of the pipe, but you haven't forked yet, so the output will also be closed for B. Fork first, then close the unneeded side of the pipe.

    Instead of waiting in the parent, why not just do the read? It will block until it gets something.
    Ooh, thank you a lot! I definitely need more exercise!

    I need to do extra work, not just what I've explained here, so I need to use wait syscall

    Along with what oogabooga said, may I suggest this fine tutorial on *nix IPCs: Beej's Guide to Unix IPC.
    Thanks, I will!
    Last edited by cifz; 07-18-2012 at 04:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exec and Pipe problem
    By Matteaus in forum C Programming
    Replies: 2
    Last Post: 09-06-2010, 09:02 AM
  2. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  3. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  4. named pipe problem
    By fnoyan in forum Linux Programming
    Replies: 0
    Last Post: 05-28-2006, 05:54 AM
  5. Another pipe() problem.
    By LightsOut06 in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2005, 10:38 PM