fork() & pipe()

Discussion in 'Perl Misc' started by bgy, Oct 20, 2008.

  1. bgy

    bgy Guest

    Hi,

    I'm trying to understand fork() & pipe() mechanism.

    Concerning fork() i understood but pipe() still presents some mysteries
    for me.

    I explain, i would like to create 5 childrens (5 + 1 parent).
    To understand i try to send each own pid to the parent.

    Here is how i do this :

    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    > use IO::Handle;
    > use constant {
    > MAX_PROCESS => 2
    > };
    >
    > my ($child, $pid, @childs, @process_list, $child_pid);
    >
    > pipe(FROM_CHILD, TO_PARENT);
    >
    > for (1...MAX_PROCESS) {
    > $pid = fork();
    > if ($pid) { # Parent code
    > push(@childs, $pid);
    > close TO_PARENT;
    > $child_pid = <FROM_CHILD>;
    > close FROM_CHILD;
    > print "My child's pid : $child_pid \n";
    > push(@process_list, $child_pid);
    > }
    > else { #child
    > close FROM_CHILD;
    > print TO_PARENT $$;
    > close TO_PARENT;
    > print "New process launched (",$_,"/",MAX_PROCESS,"): [$$]\n";
    > exit(0);
    > }
    > }
    >
    > print "[$$] -> @process_list \n";
    >
    > foreach (@childs) {
    > waitpid($_,0);
    > }



    Here is what i got :

    > maanes@void:~/workspace$ ./fork.pl
    > New process launched (1/2): [32497]
    > My child's pid : 32497
    > print() on closed filehandle TO_PARENT at ./fork.pl line 26.
    > New process launched (2/2): [32498]
    > readline() on closed filehandle FROM_CHILD at ./fork.pl line 19.
    > Use of uninitialized value in concatenation (.) or string at

    ../fork.pl line 21.
    > My child's pid :
    > Use of uninitialized value in join or string at ./fork.pl line 33.
    > [32496] -> 32497



    So for the first child it works properly.
    But i have to admit that i don't understand this error.
    So if someone could help me to fix this...

    Thanks.
    bgy, Oct 20, 2008
    #1
    1. Advertising

  2. bgy

    Guest

    bgy <> wrote:
    >
    > >
    > > pipe(FROM_CHILD, TO_PARENT);
    > >
    > > for (1...MAX_PROCESS) {
    > > $pid = fork();
    > > if ($pid) { # Parent code
    > > push(@childs, $pid);
    > > close TO_PARENT;


    You close the pipe the first time through the loop, so it is no longer
    open the 2nd time through.

    If you want all children to talk on the same pipe, then don't close it
    in the loop. If you want different pipes for each child, then create the
    pipe inside the loop, not before it.

    ....
    > > print TO_PARENT $$;


    print TO_PARENT "$$\n";

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Oct 20, 2008
    #2
    1. Advertising

  3. bgy

    bgy Guest

    > You close the pipe the first time through the loop, so it is no longer
    > open the 2nd time through.
    >
    > If you want all children to talk on the same pipe, then don't close it
    > in the loop. If you want different pipes for each child, then create the
    > pipe inside the loop, not before it.
    >
    > ...
    >> > print TO_PARENT $$;

    >
    > print TO_PARENT "$$\n";
    >
    > Xho
    >


    Thanks for the help !
    It works now.
    I did what you told me, place the pipe inside the loop !

    But still one mystery, what is the utily of \n here :
    > print TO_PARENT "$$\n";


    bgy
    bgy, Oct 20, 2008
    #3
  4. bgy

    Guest

    bgy <> wrote:
    > > You close the pipe the first time through the loop, so it is no longer
    > > open the 2nd time through.
    > >
    > > If you want all children to talk on the same pipe, then don't close it
    > > in the loop. If you want different pipes for each child, then create
    > > the pipe inside the loop, not before it.
    > >
    > > ...
    > >> > print TO_PARENT $$;

    > >
    > > print TO_PARENT "$$\n";
    > >
    > > Xho
    > >

    >
    > Thanks for the help !
    > It works now.
    > I did what you told me, place the pipe inside the loop !
    >
    > But still one mystery, what is the utily of \n here :
    > > print TO_PARENT "$$\n";


    You read what is written to TO_PARENT using the construct <FROM_CHILD>,
    which is line-oriented. So you should write to it in lines. Perhaps
    you used -l or set $\ on order to do this, but it didn't look like it.

    If the child closes the pipe after writing to it, then you might not need
    the "\n", as the eof causes the <FROM_CHILD> to return. If you have all
    children write on one pipe, then there is no eof after the first child
    and <FROM_CHILD> never returns, and you have a deadlock.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Oct 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hoegje
    Replies:
    2
    Views:
    22,513
    Gianni Mariani
    Dec 5, 2003
  2. PC

    pipe() dup2() fork() execl() select()

    PC, Aug 9, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,239
  3. Rafael Giannetti Viotti

    Subprocess and pipe-fork-exec primitive

    Rafael Giannetti Viotti, Jul 30, 2007, in forum: Python
    Replies:
    2
    Views:
    2,997
    Rafael V.
    Aug 1, 2007
  4. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    560
    Eric Snow
    Jan 8, 2009
  5. Sebastian Noack

    os.pipe() + os.fork()

    Sebastian Noack, Feb 20, 2010, in forum: Python
    Replies:
    0
    Views:
    323
    Sebastian Noack
    Feb 20, 2010
Loading...

Share This Page