LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2004, 08:02 AM   #1
cybermix
LQ Newbie
 
Registered: Jan 2003
Posts: 6

Rep: Reputation: 0
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];
 
Old 01-07-2004, 10:37 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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
 
Old 01-07-2004, 11:15 AM   #3
cybermix
LQ Newbie
 
Registered: Jan 2003
Posts: 6

Original Poster
Rep: Reputation: 0
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!
 
Old 01-07-2004, 12:26 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
where's the rest of the code? can you reproduce this in a simple example?
 
Old 01-08-2004, 03:44 AM   #5
cybermix
LQ Newbie
 
Registered: Jan 2003
Posts: 6

Original Poster
Rep: Reputation: 0
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..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shutdown(2) after fork(2) behaviour changed or undefined? MScheffer Linux - Networking 3 07-15-2005 09:11 AM
Sockets, pthread, pipe, and fork -- a messaging server ggravarr Programming 3 06-28-2005 02:39 PM
how to use fork/pipe to decrement a global varialbe Y and exit when Y is 0 keiwu Programming 1 02-19-2005 11:33 PM
strange pipe behaviour brumela Linux - Newbie 8 10-27-2004 12:03 PM
Strange behaviour of fork call in Linux Uzair Programming 1 02-12-2003 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration