What happend with parent process.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krieger
    New Member
    • Feb 2008
    • 3

    What happend with parent process.

    What happend with parent proces.
    Two scripts:

    perl4:
    [CODE=perl]#
    # loop operations
    #
    use strict;
    my @food;
    my $morsel;
    my $TableLength;
    my $i;
    @food = ("pear", "plum", "egg", "apple");
    #
    # Table printing
    #
    $TableLength = $#food;
    print $TableLength;
    foreach $i (@food){
    print $i."\n";
    }
    do
    {
    print "Password?" ;
    $a = <STDIN>;
    chop $a;
    }
    while ($a ne "fred");[/CODE]
    perl5:
    [CODE=perl]# Variable declaration
    my $FileName = "perl4.pl";
    my $Line = "";
    my @Table;
    my $pid;

    $pid = system $FileName;

    if(not defined $pid){
    #child
    print "no resources";
    }elsif($pid == 0)
    {
    print "I am a child";
    STDOUT->autoflush();
    sleep(5);
    print "I am a child2\n";
    }else{
    print "I am a parent";
    STDOUT->autoflush();
    }[/CODE]

    printout:
    prear
    plum
    egg
    apple
    Password?fred
    I am a childI am a child2

    And I have no output from parent process. Can anybody explain me why?
    Last edited by eWish; Mar 1 '08, 01:30 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    In your program, $pid is the return value of the system() function, which is the value of wait(), which is the value of the deceased forked process, which must be 0 (zero) judging by the behavior of your program. You are also using the autoflush() method without loading the IO::Handle module, which should cause your script to abort.

    See, your program is not checking if a process is a child or a parent, it is only checking the return value of the system() function.

    Comment

    Working...