Open System Services Programmer's Guide

/* we are grandchild ... exec server process with stdin
* and stdout set to connection and stderr set to
* launcher's stderr
*/
execv("./dserver",argv); /* no return from exec */
}
exit(0); /* child goes away */
}
else
{
/* we are parent */
fprintf(stderr, "dserver exec'ed in cpu %d\n", cpu);
wait(&newpid); /* wait for child to avoid zombie
* child process */
close(STDIN_FILENO);
close(STDOUT_FILENO);
}
} /* end while (1) */
}
The original dlaunch1 process closes the file descriptors associated with the requester2
connection. The grandchild process, dserver, which has inherited file descriptors for the
connection, handles all work associated with the connection. When dserver terminates, it no
longer has a parent process (because its parent, the child process, already terminated) and cannot
become a zombie process.
Note that dserver is a dynamic server, launched only when needed and terminating when its
function is completed. Such servers conserve processor resources if run infrequently. If servers need
to be launched frequently, a static iterative server, such as that shown in Example 40 (page 147)
or in Example 44 (page 182), uses processor resources more efficiently.
Using two terminal sessions, sample dialog for the terminal launching dlaunch1 might be:
/home/software/chrisbl/cabpubs: ./dlaunch1
dlaunch1 starting in cpu 8
dserver exec'ed in cpu 8
dserver [cpu 8] - starting
dserver [cpu 8] - message received on fd 0 = hello from first requester
dserver [cpu 8] - stopping [input closed]
dserver exec'ed in cpu 8
dserver [cpu 8] - starting
dserver [cpu 8] - message received on fd 0 = hello from another requester
dserver [cpu 8] - stopping [input closed]
dlaunch1 stopping - timeout
/home/software/chrisbl/cabpubs:
while the terminal running the copy of requester2 might show:
/home/software/chrisbl/cabpubs: ./requester2
Requester starting
?hello from first requester
Read 27 bytes back from server
?
/home/software/chrisbl/cabpubs: ./requester2
Requester starting
?hello from another requester
Read 29 bytes back from server
?
/home/software/chrisbl/cabpubs:
Common and Unique Characteristics 109