问题描述:

I trying to learn fork and this is a simple program:

#!/usr/bin/perl

# PRGNAME.pl by Whom

#

#

use strict;

use warnings;

main(@ARGV);

sub main

{

my @array = qw(1 2 3 4 5 6);

while ( (my $t = pop @array) ) {

if (! (my $pid = fork) ) {

exit if ( $t == 2 );

for(;;){}

}

}

waitpid(-1, 0);

message("This is the $PRGNAME exercise file.");

}

sub message

{

my $m = shift or return;

print("$m\n");

}

sub error

{

my $e = shift || 'unkown error';

print("$0: $e\n");

exit 0;

}

But on the shell when I am trying: ps -fu $LOGNAME | grep [f]ork, I found:

 i59tib 28361 1 3 16:20:07 pts/34 6:07 /usr/bin/perl ./fork.pl

i59tib 28363 1 3 16:20:07 pts/34 6:07 /usr/bin/perl ./fork.pl

i59tib 28366 1 3 16:20:07 pts/34 6:07 /usr/bin/perl ./fork.pl

i59tib 28364 1 3 16:20:07 pts/34 6:07 /usr/bin/perl ./fork.pl

i59tib 28362 1 3 16:20:07 pts/34 6:08 /usr/bin/perl ./fork.pl

Here one of the processes exited and also its parent while I comment this line exit if ( $t == 2 );

and run ps -fu $LOGNAME | grep [f]ork again, I found:

 i59tib 624 623 1 16:29:11 pts/34 0:04 /usr/bin/perl ./fork.pl

i59tib 629 623 1 16:29:11 pts/34 0:04 /usr/bin/perl ./fork.pl

i59tib 628 623 1 16:29:11 pts/34 0:04 /usr/bin/perl ./fork.pl

i59tib 625 623 1 16:29:11 pts/34 0:04 /usr/bin/perl ./fork.pl

i59tib 627 623 1 16:29:11 pts/34 0:04 /usr/bin/perl ./fork.pl

i59tib 626 623 1 16:29:11 pts/34 0:04 /usr/bin/perl ./fork.pl

i59tib 623 4766 0 16:29:11 pts/34 0:00 /usr/bin/perl ./fork.pl

How can I exit the child process without exiting the parent?

网友答案:

How can I exit the child process without exiting the parent?

The parent exited because the parent reached the end of the program, so tell the parent to do something, anything! For example,

1 while waitpid(-1, 0) > 0;
网友答案:

How can I exit the child process without exiting the parent?

try below program here Child dies before parent.

#!/usr/bin/perl
print "i am about to fork\n";
my $pid = fork();
if($pid > 0) {
    my $i = 0;
    while($i<5) {
         print "PARENT--I will be keep on running\n";
         sleep(5);
         $i++;
    }
    print "PARENT-- I m about to dead\n";
} else {
    $i = 0;
    while($i <3) {
        print "CHILD-- I will be dead before than parent\n";
        #exit(0);   you can put exit like this also.
        sleep(2);
        $i++;
    }
    print "CHILD-- I m about to dead\n";
}

Below is the sample output.

i am about to fork
PARENT--I will be keep on running
CHILD-- I will be dead before than parent
CHILD-- I will be dead before than parent
CHILD-- I will be dead before than parent
PARENT--I will be keep on running
CHILD-- I m about to dead
PARENT--I will be keep on running
PARENT--I will be keep on running
PARENT--I will be keep on running
PARENT-- I m about to dead

You can exist with any supported signal also.You can get different signal list here. You can use the same sample code at loop too but make sure here that there will be one super parent and other will be their child.

please run this program to understand more about this. Always super-parent will get exited at the end no matter you use exit or not.

#!/usr/bin/perl
print "i $$ am about to fork\n";
my @children_pids;
my $n = 3;
for my $i ( 0..($n-1) ) {
    my $pid = fork();
    if($pid > 0) {
        print "i m $$ child of".getppid()."\n";
        push @children_pids, $pid;
    } else {
        print "I $$ is about to dead. I am child of".getppid()."\n";
        exit();
    }
}
waitpid $_, 0 for @children_pids;
print "$$ is finished\n";

typical output at my system of above program is as below.

i 3919 am about to fork
i m 3919 child of3092
I 3920 is about to dead. I am child of3919
i m 3919 child of3092
i m 3919 child of3092
I 3921 is about to dead. I am child of3919
I 3922 is about to dead. I am child of3919
3919 is finished

Parent will wait all its child to get exit;then only it will exit.

相关阅读:
Top