(Anno Siegel) wrote in message news:<co1nur$5ea$>...
> <> wrote in comp.lang.perl.misc:
> > hi all,
> >
> > i was doing the following program in perl
> >
> > #!/usr/bin/perl
> > 2
> > 3 main();
> > 4
> > 5 sub main
> > 6 {
> > 7 my $id;
> > 8 if(($id=fork())==0)
> > 9 {
> > 10 print("in child process, id=$$!\n");
> > 11 sleep 5;
> > 12 $i=getppid;
> > 13
> > 14
> > 15 if(kill 0,$i)
> > 16 {
> > 17 print("signal sent\n");
> > 18 }
> > 19 else
> > 20 {
> > 21 print("parent died pid=$i\n");
> > 22 }
> > 23
> > 24 exit;
> > 25 }
> > 26 else{
> > 27 print("parent terminated\n");
> > 28 exit(0);
> > 29 }
> >
> > note that i am taking the parent pid after the
> > parent has exited.
>
> You are taking it after sleeping for five seconds. Presumably the parent
> process had time to terminate, but we don't *know* this.
>
> > according to std unix idiom,
>
> Idioms are part of languages (including computer languages). What
> you are describing is the behavior of an OS, not an idiom.
>
> > the init process (with
> > pid =1) should be the parent of the orphaned child process in this
> > example. but when i executed the code, this was the output:
> >
> > [madhav@madhav perl]$ perl child.pl
> > in child process, id=13235!
> > parent terminated
> > [madhav@madhav perl]$ parent died pid=13234
> >
> > this indicates that the parent of the orphaned child process is not
> > the init process as it should be. please tell me what is means. i did
> > the same program in C and got the parent id as 1. cant it be the same
> > with perl?
>
> It is, for me. When I run your program, I see
>
> [anno4000@lublin ~/clpm]$ perl ttt
> parent terminated
> in child process, id=13690!
> [anno4000@lublin ~/clpm]$ parent died pid=1
>
>
*but it doesn't look like Perl is responsible.*
i have checked the parent of the process by running the ps -ax command
in another console while the child process was sleeping, and it is
reporting me that the parent of this process is indeed the init
process as it should be.
this was the output when i checked it again:
[madhav@madhav perl]$ perl child.pl
in child process, id=4256!
parent terminated
[madhav@madhav perl]$ parent died pid=4255
[root@madhav bin]# ps -lax | grep perl
1 1104 4256 1 21 0 5308 1376 schedu S pts/0 0:00
perl child.pl
0 0 4258 2341 15 0 3572 624 pipe_w S pts/2 0:00
grep perl
this clearly shows that the init has clearly became the parent of the
process.