Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inter process comunication (again..)

Status
Not open for further replies.

byleth

Programmer
Feb 27, 2004
70
PT
ok, i have a father process and a bunch of childs :)p) :


for (my $1 ; $i < $n ; $i++) {

my $pipe = new IO::pipe;
my $child = fork();

if ($child == 0) { #child

my $resp = something();

#child writes the result to the pipe

$pipe->writer();
print $pipe $resp;
exit 0;

} else { #father

#father reads and concatenates all the answers in
#one

$pipe->reader();

while (<$pipe>) {
$res = $res . $_;
}

wait();

}
}


this works fine, the problem is that the childs don't execute concurrently (don't know if i'm expressing right).

the father waits for each child to finish and then another process starts.

Is this a consequence of the pipe, because i think that without the read-write-pipe process the childs execute 'simultaneously'.

thanks...

 
Hello,

Sorry if I am being really stupid here but surely the line:

if ($child == 0) { #child

should actualy be:

if ($child != 0) { #child

Surely the child should run if it has a PID not equal to 0.

If the fork() command has worked the $child variable should have a PID which is not ever going to be zero.

as it stands I do not think the child process actually does anything when it runs, in fact looking at it it looks like you might leave rogue child processes running as they never get through the "if" statement.

Hope this has been of some use.

Mark G
 
It doesn't work that way. The fork function returns 0 to the child process and the child's PID to the parent. So if $child is equal to 0 you are in the child's process, and if it is greater then 0 you are in the parent's process...
 
i never user IO::pipe but i read something about it and it looks like pipe->reader() is blocking by default if you want to make it unblocking you have to call
$pipe->blocking(0)
after $pipe->reader() .

hope this help.

 
If you allow the child processes to run concurrently, how are you going to arbitrate access to the pipe?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top