For 2023 - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
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)
Some other Articles
Well House Manor - the next six yearsAudio equipmentException handling in Perl - using evalChildren, zombies, and reaping processesExpect in Perl - a short explanation and a practical example Dulwich College Preparatory, and Sevenoaks, SchoolsSetting up a matrix of data (2D array) for processing in your programSanta announcement, 5th December 2010, MelkshamLots 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).
|
|