Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Children, zombies, and reaping processes

What is "Reaping Processes"?

Most programs are single processes - you write a program that runs sequentially from start to finish, interrupted only by the controls of conditionals like if, loops like while and subroutines / functions / methods, and you can always look at a program and say "it's HERE in running". But that's most programs, not all programs.

Let's say that you've got a server process (a.k.a. a daemon) that's going to provide a service when called up - it could be a web server, a print server, or something more specialised. You're going to want that process to head off in two different directions when a user calls up the service - you're going to want the original process to carry on listening for more requests (rather than hang up a "no vacancies" sign), and you're going to want a new process to be branched off from that original to provide the service. This is done by the fork function - in C, also burst through to Perl and many other languages.

So far, so good ... but what happens when the child process completes its work? In a program that only runs for a short while, with just a few children, it can be allowed to simply exit, but in program that's got a persistent parent, such as a server, an exit isn't quite enough - it leave debris in the process table in the form of zombies - which clutter up an operating system release until they're cleaned up by the parent "reaping" them, or by the parent exiting.

When a child exits, it sends a signal - SIGCHLD - to its parent, and that's an indicator to the parent to run the cleanup code if it needs to - to release the zombie. In Perl, there are two ways to do this:

  $SIG{CHLD} = 'IGNORE';
will cause the zombie to be reaped cleanly with no further action; IGNORE is standard / supplied value / subroutine in Perl's signal handler which silently accepts signals and causes them to have no effect on the receiving process.

  use POSIX ":sys_wait_h";
  while (($kidval = waitpid(-1, &WNOHANG)) > 0) {} ;

will cleanly reap the zombie and will allow you to take further action yourself on its receipt.

There's an example [here] showing the use of this in a complete demonstration program.

Note - signals are operating system dependent. Example tested / used on Unix (Mac OSX) and Linux systems.
(written 2010-10-23, updated 2010-10-29)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P223 - Perl - Interprocess Communication
  [604] Perl - multiprocess applications - (2006-02-13)
  [1918] Perl Socket Programming Examples - (2008-12-02)
  [2694] Multiple processes (forking) in Python - (2010-03-25)
  [2970] Perl - doing several things at the same time - (2010-09-25)
  [3011] What are .pid files? - (2010-10-23)
  [3412] Handling binary data in Perl is easy! - (2011-08-30)
  [3940] Run other processes from within your Perl program - (2012-12-03)


Back to
Expect in Perl - a short explanation and a practical example
Previous and next
or
Horse's mouth home
Forward to
What are .pid files?
Some other Articles
Well House Manor - the next six years
Audio equipment
Exception handling in Perl - using eval
Children, zombies, and reaping processes
Expect in Perl - a short explanation and a practical example
Dulwich College Preparatory, and Sevenoaks, Schools
Setting up a matrix of data (2D array) for processing in your program
Santa announcement, 5th December 2010, Melksham
Lots of ways of doing it in Perl - printing out answers
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3010_Chi ... esses.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb