Thread: bidirectional pipes

  1. #16
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by Elkvis View Post
    since you're redirecting stdin and stdout in both the parent and the child, try something like this:
    Code:
    std::string s;
    std::getline(std::cin, s, '\0'); //could just say 0 but '\0' makes it clear that it's a char value
    I tried that and works.The only problem appears when I send s in execl command(The problem appeared when I changed the type to string)
    this is the message from the compiler:

    cannot pass objects of non-POD type 'struct std::string' through '...'; call will abort at runtime

  2. #17
    Registered User
    Join Date
    May 2011
    Posts
    116
    What does the error Broken Pipe means?
    I send data through the pipe,the child accepts it then it writes back to the pipe and the parent accepts the message as well.But when I try to resend information to the child it returns that message.
    In the parent's side I have:

    Code:
    for(int i=0;i<3;i++){
       cout<<"Send a message to the child"<<endl;
       //cin whatever the child sends back
    }
    It runs only for one loop and then that message appears...

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I believe that error means that the pipe has been closed at one end.

  4. #19
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by Elkvis View Post
    I believe that error means that the pipe has been closed at one end.
    yes I figured it out..well,besides that error that doesn't appear any more,the parent only manages to write to the pipe and read from it only once.That means that while the parent does:

    Code:
    //parent sends s (message) to the bidirectional pipe
    cout<<s<<endl;
    //parent reads any data the child sends 
    //cin is due to dup2 that has been used
    getline(cin, str, '\0');
    //parent writes everything that was received by the child through the
    //bidirectional pipe to a file
    myfile << str<<endl;
    the child does:

    Code:
    //reads message s from the bidirectional pipe which has been sent //from the parent
    cin.getline(s,LINESIZE);
    //then the child reads data from an ifstream and writes them to the bidirectional pipe for the parent to receive that data
    while(if.getline(line,LINESIZE))
        cout<<line<<endl;

    everything works fine for one loop.That means
    parent writes message to pipe--->child reads message from pipe--->child writes many data to pipe--->parent reads all data the child sent through the pipe


    but when I try to send a new message from the parent process the whole thing seems to reach a deadlock..
    The way I try to send a message through the pipe again is the following:

    Code:
    for(int j=0;j<3;j++){
       cout<<s<<endl;
       getline(cin, str, '\0'); 
       myfile << str<<endl;}
    but as I said only goes on for one loop,for 3 loops as above it waits...

  5. #20
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    perhaps you're not sending a '\0' at the end of the string the second time

  6. #21
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by Elkvis View Post
    perhaps you're not sending a '\0' at the end of the string the second time
    Actually in the parent code and out of the loop I have:

    Code:
    char *s;
    s = new char[LINESIZE];
    and inside the loop s is allocated :

    Code:
    //sss() returns a char*
    s = sss();
    How can I check that?And if that so ,why does it work when the for-loop doesn't exist?Because with or without the loop the function sss()
    returns a char*,so if it was for that then it shouldn't be working at all.right?


    I also have to say that not only the parent has a loop but also the child's getline is in a loop in order to receive data more than once
    Last edited by quo; 06-06-2012 at 08:02 AM.

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    THERE IS NO SUCH THING AS A BIDIRECTIONAL PIPE.

    man 7 pipe

    On some systems (but not Linux), pipes are bidirectional: data can be transmitted in both directions between the pipe ends. According to POSIX.1-2001, pipes only need to be unidirectional. Portable applications should avoid reliance on bidirectional pipe semantics.
    Not only is it non-standard, it doesn't even work. It's not surprising you're having problems while attempting the impossible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #23
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by brewbuck View Post
    THERE IS NO SUCH THING AS A BIDIRECTIONAL PIPE.
    Not only is it non-standard, it doesn't even work. It's not surprising you're having problems while attempting the impossible.
    What I have done is not correct then?
    I didn't quite understand what you suggested to do

  9. #24
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I didn't quite understand what you suggested to do
    O_o

    He says:

    The pipe "FlowRight" communicates from parent to child.
    The pipe "FlowLeft" communicates from child to parent.

    The pipe "FlowRight" can not communicate from child to parent.
    The pipe "FlowLeft" cant not communicate from parent to child.

    If you want to communicate in both directions you need two pipes or one socket.

    Soma

  10. #25
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    did you guys read this whole thread? he is using two pipes. unless his current code is radically different from what he posted in #7, he's calling pipe() twice, and dup2()-ing his stdin and stdout in both parent and child to the proper ends of each pipe.

  11. #26
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    did you guys read this whole thread?
    Have you?

    I was only explaining what brewbuck said which was said only as a response to "parent sends s (message) to the bidirectional pipe" which was a comment in the source.

    If he is using two separate pipes he isn't trying to use a "bidirectional pipe" so that's fine, but by that measure, if he is using a two pipes he shouldn't make the comment that he is using a "bidirectional pipe" which isn't a thing because it leads to confusion and wastes times.

    [Edit]
    To be clear, I have no idea about the real current code and neither do you and neither does brewbuck. We only know what he has posted. The problem comes when he says things like "write to bidirectional pipe" or "read from bidirectional pipe".
    [/Edit]

    Soma
    Last edited by phantomotap; 06-06-2012 at 01:42 PM.

  12. #27
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I'll agree that it can cause confusion to refer to a pipe as bidirectional, because they generally are not, but calling a guy out only because of the comments in his code seems a little pedantic to me, and if his mental image of the situation is such that cin/cout together constitute a "bidirectional pipe," then I don't see it really being a confusion of ideas.

  13. #28
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    calling a guy out only because of the comments in his code seems a little pedantic to me
    *shrug*

    I don't think that's what happened here.

    I don't see it really being a confusion of ideas
    The ideas in his head isn't an issue; the ideas as he conveys them is an issue because he is trying to get help for a communication issue using pipes because people will naturally assume that he is trying to use a "bidirectional pipe" when he isn't.

    Soma

  14. #29
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    all the disagreement aside...

    what does your current code look like?

  15. #30
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by Elkvis View Post
    all the disagreement aside...

    what does your current code look like?
    thank you guys for your help,actually it was very confusing the way I was trying to do it when it came to many child processes so I changed it and used 2 fifo pipes one to send and one to receive data,where no deadlock occured..
    thanks again for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bidirectional loop? (Increase/decrease depending on values)
    By BlueGooGames in forum C Programming
    Replies: 4
    Last Post: 01-08-2011, 12:08 PM
  2. 2 way pipes
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-25-2010, 04:21 AM
  3. pipes in c
    By ajal1 in forum C Programming
    Replies: 6
    Last Post: 10-29-2005, 03:29 PM
  4. pipes
    By moi in forum C Programming
    Replies: 8
    Last Post: 08-11-2004, 01:59 PM
  5. Need some help with pipes please.
    By carrja99 in forum C Programming
    Replies: 1
    Last Post: 05-05-2004, 04:13 PM