LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Wierd behaviour using fork() and pipe() (https://www.linuxquestions.org/questions/programming-9/wierd-behaviour-using-fork-and-pipe-132361/)

cybermix 01-07-2004 08:02 AM

Wierd behaviour using fork() and pipe()
 
I have a problem that is wierd. I intend to have a program, having a parent doing some task, and a child that should serve as a helper of the parent. They shall communicate using two pipes, one from the application (parent) to the helper, another pipe would be the other way round.

The parent is suppose able to send data to the child by writing to helper.wPipe, and the child may reply by writing to its pipe (h2a[_PIPE_W]). For the parent side, I have a loop with select() waiting for messages from helper.rPipe. The same goes to child with a loop waiting for a2h[_PIPE_R]. However wierd things happen: when I write to helper.wPipe (parent), I got read on helper.rPipe, instead of a2h[_PIPE_R]. Ocassionally when I recompile it get to work correctly again. Anyone know the reason?

thanks!



Code:

#define _PIPE_R 0
#define _PIPE_W 1

int a2h[2], h2a[2];
if (pipe(a2h) != 0)        // Create pipe from application to helper.
        return false;
if (pipe(h2a) != 0)        // Create pipe from helper to application.
        return false;

switch (fork())
{
        case -1:
                std::cerr << "Helper fork(1) error.\n";
                return false;
        case 0:
        {
                dAppHelper dah;
                close(h2a[_PIPE_R]);
                close(a2h[_PIPE_W]);
                dah.run(a2h[_PIPE_R], h2a[_PIPE_W]);
                exit(0);
        }
        default:
                break;
}

// Close file handlers that is not of current process concern.
close(h2a[_PIPE_W]);
close(a2h[_PIPE_R]);
helper.rPipe = h2a[_PIPE_R];
helper.wPipe = a2h[_PIPE_W];


kev82 01-07-2004 10:37 AM

i cant see anything wrong with that and i cant imagine how the pipes are getting crossed, are you sure they are? all i can reccomend is each time data is read/written log the pid, pipefd, and data to a file and see at what points it occurs.

also you say sometime when you recompile it works fine, see if you can produce 2 binaries, one that works and one that doesnt and find out what the difference is

cybermix 01-07-2004 11:15 AM

Too bad I was unable to regenerate the copy that runs without error.

Apparantly I have first pipe (a2h) having 5,6 and second pipe (h2a) having 7,8
parent polling from 7, and child polling from 5.

When parent sends something to child, it writes to 6, and suppose 5 will has something to 'read' when the child select(). Surprisingly, instead of 5 having something to 'read', parent reports that 7 has to 'read' when the parent select(). (Note: the select() is implemented separately on both parent and client.)

In diagram it doesn't really make sense how written stuff to 6 'jumps' to 7. I am using Fedora Core 1. Is this system specific? (Does not seems to be since this chuck of code is quite widely used)

Code:

      |(5)  X-+--->(5)|
      |(6)----+-X  (6)|
parent |              | child
      |(7)<---+-X  (7)|
      |(8)  X-+----(8)|

Thanks in advanced!

infamous41md 01-07-2004 12:26 PM

where's the rest of the code? can you reproduce this in a simple example?

cybermix 01-08-2004 03:44 AM

The select() is implemented in a separated class using FD_SET, FD_CLR, FD_ISSET. It's quite long and involve some other additional classes too. I am using the class also to poll for socket messages and it works fine.

I've added additional code and actually finds out that read from 7 by the parent has nothing. This would mean the data actually goes to the correct place, while the select doesn't seems to. I shall try to review my code for the event class now... and maybe later decided to move to socketpair if things still doesn't works...


Thanks anyway..


All times are GMT -5. The time now is 03:25 AM.