/*
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!
===========================================================================