to execute one instruction), practically the only difference between the
ancestor and the descendant in the programmer's view is in the code
return fork () .
For descendant fork () returns zero and for ancestor the id ( pid ) of the
created descendant process. When for some reason the descendant was not
created, fork () will return –1. Therefore, the normal code for and fork ()
looks like this:
pid _ t pid ;
if (( pid = fork ()) == - 1)
{ / * error, crash * /) }
if (pid == 0)
{
// is a descendant
}
else
{
// this is the ancestor
printf (" Descendant started with code % d \ n ", pid );
}
Once the process is created, it can access the identifying information by a
system call to getpid () which returns the current process ID, and getppid (),
which returns the process ID of the ancestor .
#include
pid_t mypid, parent_pid;
mypid = getpid ();
parent_pid = getppid ();