Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: interprocess communication

by moritz (Cardinal)
on May 15, 2014 at 17:30 UTC ( #1086180=note: print w/ replies, xml ) Need Help??


in reply to interprocess communication

Using the always-recommended use strict; use warnings; reveals one problem with your code:

Bareword "WNOHANG" not allowed while "strict subs" in use

Another is that only the parent process should wait for the child process; if doesn't make any sense to wait for anything in the child.

In addition, doing a loop with non-blocking waitpid is useless; a single call to a blocking waitpid makes more sense. Or if you have only one child process, wait is even simpler.

Finally, afer the fork there are two writers; one in the child, one in the parent process. You need to close the one in the writer too:

#!/usr/bin/perl # always uses these; use strict; use warnings; # and detect errors use autodie; pipe(READER, WRITE); my $pid =fork(); if ($pid) { print "In parent\n"; close WRITE; my $val1 = 100; while(my $num = <READER>) { print "in while; $num\n"; } close(READER); wait; } else { print "In child\n"; my $val2 = 150; print WRITE "$val2\n"; close(WRITE); print "Child closed write\n"; }


Comment on Re: interprocess communication
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-10-01 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (392 votes), past polls