fork() and parent pid

fork() and parent pid

Post by Bob Nels » Mon, 05 Feb 1996 04:00:00



/*
Although my question below concerns the Linux platform in
particular, I pose it here because it may elicit a response
from experts who are interested in any *nix flavor or clone.

Consider the following code based on Stevens "UNIX Network
Programming", p. 55. I'm trying to see why the child doesn't
inherent the parent process ID...and why the program just hangs.
The ppid in the child is 1 (from init) rather than the expected
pid of the parent. If it makes any difference, my kernel is a
stock, stable 1.2.13.

It works as expected on the Sun:

SunOS netcom17 4.1.3_U1 9 sun4m
parent: childpid = 29540, pid = 29539, ppid = 28179, pgrp = 29539
child: pid = 29540, ppid = 29539, pgrp = 29539

Here's what I get on the PeeCee:

Linux dell450 1.2.13 #2 i486
parent: childpid = 23793, pid = 23792, ppid = 23791, pgrp = 23792
child: pid = 23793, ppid = 1, pgrp = 23792
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t childpid;

    if((childpid = fork()) == 1) {
        perror("fork failure");
        exit(1);
    }
    else if(childpid == 0) {
        printf("child: pid = %d, ppid = %d, pgrp = %d\n",
               getpid(), getppid(), getpgrp());
        exit(0);
    }
    else {
        printf("parent: childpid = %d, pid = %d, ppid = %d, pgrp = %d\n",
               childpid, getpid(), getppid(), getpgrp());
        exit(0);
    }

    return 0;  

Quote:}

--
=============================================================================

      Linux for fun, M$ for $$$...and the NFL for what really counts!
=============================================================================
 
 
 

fork() and parent pid

Post by Andrew Gier » Mon, 05 Feb 1996 04:00:00



Quote:>/*
>Although my question below concerns the Linux platform in
>particular, I pose it here because it may elicit a response
>from experts who are interested in any *nix flavor or clone.

>Consider the following code based on Stevens "UNIX Network
>Programming", p. 55. I'm trying to see why the child doesn't
>inherent the parent process ID...and why the program just hangs.
>The ppid in the child is 1 (from init) rather than the expected
>pid of the parent. If it makes any difference, my kernel is a
>stock, stable 1.2.13.

>It works as expected on the Sun:

>SunOS netcom17 4.1.3_U1 9 sun4m
>parent: childpid = 29540, pid = 29539, ppid = 28179, pgrp = 29539
>child: pid = 29540, ppid = 29539, pgrp = 29539

>Here's what I get on the PeeCee:

>Linux dell450 1.2.13 #2 i486
>parent: childpid = 23793, pid = 23792, ppid = 23791, pgrp = 23792
>child: pid = 23793, ppid = 1, pgrp = 23792
>*/

>#include <stdio.h>
>#include <stdlib.h>
>#include <unistd.h>

>int main(void)
>{
>    pid_t childpid;

>    if((childpid = fork()) == 1) {

Shouldn't that be -1?

- Show quoted text -

Quote:>        perror("fork failure");
>        exit(1);
>    }
>    else if(childpid == 0) {
>        printf("child: pid = %d, ppid = %d, pgrp = %d\n",
>               getpid(), getppid(), getpgrp());
>        exit(0);
>    }
>    else {
>        printf("parent: childpid = %d, pid = %d, ppid = %d, pgrp = %d\n",
>               childpid, getpid(), getppid(), getpgrp());
>        exit(0);
>    }

>    return 0;  
>}

This is behaving strictly as per spec on both platforms; the difference is
a timing dependency.

Your parent process is not doing a wait() call to wait for the child process
to exit. Therefore, it is possible for the parent to get to its exit() call
*before* the child has executed the call to getppid(). When the parent
exits, the child is orphaned, so its PPID magically becomes 1. Stick a call
to wait() just before the exit() in the parent, and it will behave
consistently.

Don't know what you mean about the program* - it looks OK to me...

>--
>=============================================================================

>      Linux for fun, M$ for $$$...and the NFL for what really counts!
>=============================================================================


[.sig under construction]

 
 
 

fork() and parent pid

Post by Bob Nels » Mon, 05 Feb 1996 04:00:00


On 4 Feb 1996 14:39:58 GMT, Andrew Gierth wrote in response to this code:

Quote:>> > [Headers elided]
>> >int main(void)
>> >{
>> >    pid_t childpid;

>> >    if((childpid = fork()) == 1) {
>> Shouldn't that be -1?

Absolutely -- just a typo in the version posted here. It *is* -1 in
the "real" thing.

Quote:>> >        perror("fork failure");
>> >        exit(1);
>> >    }
>> >    else if(childpid == 0) {
>> >        printf("child: pid = %d, ppid = %d, pgrp = %d\n",
>> >               getpid(), getppid(), getpgrp());
>> >        exit(0);
>> >    }
>> >    else {
>> >        printf("parent: childpid = %d, pid = %d, ppid = %d, pgrp = %d\n",
>> >               childpid, getpid(), getppid(), getpgrp());
>> >        exit(0);
>> >    }

>> >    return 0;  
>> >}
>> Don't know what you mean about the program*-it looks OK to me...

On the PeeCee (Linux), the child process never returns (apparently) and
I have to ^C to get out of the program. In other words, the shell prompt
doesn't appear until I manually intervene. FWIW -- if I either pipe or
redirect the output of the above program, the shell prompt returns and
the "hanging" I referred to does not occur.

Hmmm...I've got to get that *other* Stevens book! :)

Finally...has there even been (or will there ever be) a FAQ for this
newsgroup in particular? I've got to think that my question has
been posed and answered ULONG_MAX times.

--
=============================================================================

      Linux for fun, M$ for $$$...and the NFL for what really counts!
=============================================================================

 
 
 

fork() and parent pid

Post by John J. Rushford J » Tue, 06 Feb 1996 04:00:00


: /*
: Although my question below concerns the Linux platform in
: particular, I pose it here because it may elicit a response
: from experts who are interested in any *nix flavor or clone.

: Consider the following code based on Stevens "UNIX Network
: Programming", p. 55. I'm trying to see why the child doesn't
: inherent the parent process ID...and why the program just hangs.
: The ppid in the child is 1 (from init) rather than the expected
: pid of the parent. If it makes any difference, my kernel is a
: stock, stable 1.2.13.

: It works as expected on the Sun:

: SunOS netcom17 4.1.3_U1 9 sun4m
: parent: childpid = 29540, pid = 29539, ppid = 28179, pgrp = 29539
: child: pid = 29540, ppid = 29539, pgrp = 29539

: Here's what I get on the PeeCee:

: Linux dell450 1.2.13 #2 i486
: parent: childpid = 23793, pid = 23792, ppid = 23791, pgrp = 23792
: child: pid = 23793, ppid = 1, pgrp = 23792
: */

: #include <stdio.h>
: #include <stdlib.h>
: #include <unistd.h>

: int main(void)
: {
:     pid_t childpid;

:     if((childpid = fork()) == 1) {
:         perror("fork failure");
:         exit(1);
:     }
:     else if(childpid == 0) {
:         printf("child: pid = %d, ppid = %d, pgrp = %d\n",
:                getpid(), getppid(), getpgrp());
:         exit(0);
:     }
:     else {
:         printf("parent: childpid = %d, pid = %d, ppid = %d, pgrp = %d\n",
:                childpid, getpid(), getppid(), getpgrp());
:         exit(0);
:     }

:     return 0;  
: }

Perhaps you need to wait() in the parent before you exit as you don't know
which process will finish first.  If the parent is exiting before the child
prints the ppid, then you should see ppid = 1.

John Rushford

 
 
 

fork() and parent pid

Post by Joe Foster of Bo » Tue, 06 Feb 1996 04:00:00


[chomp]

Quote:> It works as expected on the Sun:
> SunOS netcom17 4.1.3_U1 9 sun4m
> parent: childpid = 29540, pid = 29539, ppid = 28179, pgrp = 29539
> child: pid = 29540, ppid = 29539, pgrp = 29539
> Here's what I get on the PeeCee:
> Linux dell450 1.2.13 #2 i486
> parent: childpid = 23793, pid = 23792, ppid = 23791, pgrp = 23792
> child: pid = 23793, ppid = 1, pgrp = 23792
> */
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> int main(void)
> {
>     pid_t childpid;
>     if((childpid = fork()) == 1) {

     if((childpid = fork()) == -1) {
...but this was corrected in another post.

Quote:>         perror("fork failure");
>         exit(1);
>     }
>     else if(childpid == 0) {
>         printf("child: pid = %d, ppid = %d, pgrp = %d\n",
>                getpid(), getppid(), getpgrp());
>         exit(0);
>     }
>     else {
>         printf("parent: childpid = %d, pid = %d, ppid = %d, pgrp = %d\n",
>                childpid, getpid(), getppid(), getpgrp());

I got the child to stop reporting its parent's process ID as 1 if
I inserted a sleep(1) here. What's happening is that the parent
fork()'s, prints it line of output, and then exits, all before
the child can execute its call to getppid(). Since init's duties
include acting as the process orphanage as well as the getty
manager, the ppid is changed to 1 as soon as the parent process
exits. You might be seeing this on the PC but not on the Suns
because Intel CPU's take a long time to save their current state
and load a new state, so non-brain-damaged OS designers tend to
design multitaskers to try to give each process a long timeslice
to minimize the context-switch overhead. Linux is simply running
the parent to completion before running the child.

Quote:>         exit(0);
>     }
>     return 0;  
> }

[chomp]

--

WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!

 
 
 

1. fork() and parent pid

Ok, someone has already pointed out the fact that by having the parent
exit before waiting for the child, the child becomes a zombie and is
inherited by the init function.  No problem.  But there is one other
thing that needs to be corrected as well.

This is absolutely incorrect.  First of all, the childpid will *never*
equal one...  Second of all, when fork fails, it returns -1, not 1.

Since you are reading Stevens' UNIX Networking book, I suggest another
excellent book (probably better than his Networking book) by Stevens:
"Advanced Programming in the UNIX Environment".  It goes over alot
of these types of issues.

Michael Douglass
Texas Networking, Inc.

"I didn't change a thing and from the moment I didn't change it,
it doesn't work anymore." -- Anonymous

2. How do I put a return in my prompt???? (csh)

3. How to get PID of the initiator (parent) of a forked process.

4. ieeed.h librairie

5. How to find children PID's given parent PID?

6. HPUX: problem with shared library

7. Getting children's PID with parent's pid?

8. Error: can't read "curvendor": no such variable

9. How the waiting parent get the pid from the child?

10. Zombie processes with parent INIT (pid 1)

11. child's parent PID is 1 ???

12. parent pid

13. NEWBIE: how to find and kill by Parent PID