Practical mod_perl / HTML Book /



previous page: 10.2.2. Freeing the Parent Process
  
page up: HTML Version of the book
  
next page: 10.2.4. Avoiding Zombie Processes

10.2.3. Detaching the Forked Process


Now what happens if the forked process is running and we decide that we need to restart the web server? This forked process will be aborted, because when the parent process dies during the restart, it will kill its child processes as well. In order to avoid this, we need to detach the process from its parent session by opening a new session with help of a setsid( ) system call (provided by the POSIX module). This is demonstrated in Example 10-15.

Example 10-15. fork3.pl

use POSIX 'setsid';

defined (my $kid = fork) or die "Cannot fork: $!\n";
if ($kid) {
    # Parent runs this block
}
else {
    # Child runs this block
    setsid or die "Can't start a new session: $!";
    # ...
}

Now the spawned child process has a life of its own, and it doesn't depend on the parent any more.

 

Continue to:

  • prev: 10.2.2. Freeing the Parent Process
  • Table of Contents
  • next: 10.2.4. Avoiding Zombie Processes







TOP
previous page: 10.2.2. Freeing the Parent Process
  
page up: HTML Version of the book
  
next page: 10.2.4. Avoiding Zombie Processes


Menu

  • HTML Book
  • PDF Book
  • Download Code
  • Table of Contents
  • Errata
  • mod_perl2 User's Guide
  • Sitemap

Search


Add to Google




Creative Commons License


Written by
Eric Cholet (Logilune) and
Stas Bekman (StasoSphere & Free Books).


[ Privacy Policy | Terms of Use | About Us | Search ]

© 2007 StasoSphere.com