Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The parent pid sends a string to the child pid, which the child prints out. The child then sleeps for 2 seconds before sending a string to the parent. Instead of locking up completaly the parent prints "Waiting...\n" until there is input for it to read.

I wrote this to encorperate it into a larger Tk script which I don't want freezing when it forks off to download information.

NOTE: This does not work on Windows (hence the name "Unix Sockets")!

The code is a based on of "pipe2" from perlipc. See also: IO::Socket, IO::Select, and perlipc.

#!/usr/bin/perl -w use strict; use Socket; use IO::Socket; use IO::Select; socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!"; CHILD->autoflush(1); PARENT->autoflush(1); my $pid; my $s = IO::Select->new(); $s->add(\*CHILD); if ( $pid = fork ) { close PARENT; print CHILD "Parent Pid $$ is sending this\n"; #chomp(my $line = <CHILD>); my $line; while ( 1 ) { if ( $s->can_read(0) ) { foreach my $handle ( $s->can_read(0) ) { recv($handle, $line, 1024, 0); } last; } print "Waiting...\n"; } chomp $line; print "Parent Pid $$ just read this: '$line'\n"; close CHILD; waitpid($pid, 0); } else { die "cannot fork: $!" unless defined $pid; close CHILD; chomp(my $line = <PARENT>); print "Child Pid $$ just read this: '$line'\n"; sleep 2; print PARENT "Child Pid $$ is sending this\n"; close PARENT; exit; }

In reply to Non-Blocking Bidirectional Communication using Unix Sockets by dooberwah

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others romping around the Monastery: (4)
    As of 2024-07-18 20:30 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.