Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

interprocess communication

by mevans (Initiate)
on May 15, 2014 at 14:54 UTC ( [id://1086159]=perlquestion: print w/replies, xml ) Need Help??

mevans has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out how to pass data from one process to another. I've read perlipc but I am still missing something or misunderstood something. Would any one be able to set me straight on what I'm doing wrong?

#!/usr/bin/perl pipe(READER, WRITE); $pid =fork(); if ($pid) { print "\nIn parent"; $val1 = 100; while($num = <READER>) { print "\nin while"; print $num; } close(READER); } else { print "\nIn child"; $val2 = 150; print WRITE $val2; close(WRITE); print "\nChild closed write"; } do { $forkvar = waitpid (-1, WNOHANG); } while ($forkvar < 0);

Update:everything seems to be working now. Thanks for you assistance

Replies are listed 'Best First'.
Re: interprocess communication
by morgon (Priest) on May 15, 2014 at 15:23 UTC
    I don't have time to run your code, but there is at least one thing you do wrong:

    Your parent-process uses the angle-operator to read from the pipe which means it looks for newlines. Your child however does not supply newlines.

    So try

    print WRITE "$val2\n";
    in your child.
Re: interprocess communication
by moritz (Cardinal) on May 15, 2014 at 17:30 UTC

    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"; }
Re: interprocess communication
by mr_mischief (Monsignor) on May 15, 2014 at 19:21 UTC
    Please don't erase and replace your answered questions. Someone else might have the same issue. You've ruined search and threading. Below is something akin to the original.

    I'm trying to figure out how to pass data from one process to another. I've read perlipc but I am still missing something or misunderstood something. Would any one be able to set me straight on what I'm doing wrong?

     #!/usr/bin/perl pipe(READER, WRITE); $pid =fork(); if ($pid) { print "\nIn parent"; $val1 = 100; while($num = <READER>) { print "\nin while"; print $num; } close(READER); } else { print "\nIn child"; $val2 = 150; print WRITE $val2; close(WRITE); print "\nChild closed write"; } do { $forkvar = waitpid (-1, WNOHANG); } while ($forkvar < 0);
      Yes, please put the original question text back into your post, then add the fact that you solved it after that, perhaps with a brief summary.
Re: interprocess communication
by kcott (Archbishop) on May 16, 2014 at 06:20 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1086159]
Approved by igelkott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2024-04-23 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found