avanta has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

Suppose, I have a program which forks a parent and child processes and they will work seperately and different processing will be done in each. Also, i want no intevention of the two processes with each other. i.e. we will not wait for both the process to end. Now, my question is that in case, our child process hangs due to some reason how can we monitor it. Also, here our parent may have been completed.

Please help!!

Replies are listed 'Best First'.
Re: monitor a process's state.
by moritz (Cardinal) on May 06, 2010 at 13:14 UTC
    How do you determine if a child "hangs"? I mean which criteria would you use?

    See perlipc for general ideas of communicating with processes.

      I was wondering the same thing... Especially if you're not waiting for it to finish, how can you ever know if it is hung or not?

      I've used Parallel::ForkManager in the past to spawn processes, but it also has a 'wait_all_children' function that will cause it to wait until all of the children processes are finished. In that case, you can have the child processes return a variable denoted that it completed successfully, errored, returned invalid results, etc.

      Of course it depends on what you're trying to accomplish, but if you don't wait for the children to finish, I can't imagine that you'd know whether or not they ever really did finish.
        Well, if one defines "hanging" as "using more CPU time than a certain, fixed amount", then one could use wait_all_children in the parent process, and set CPU time limits with BSD::Resource in each child process.

        Of course that requires re-organizing the previous task of the parent process into a child process, but as the others have mentioned, that's a pretty good idea anyway.

Re: monitor a process's state.
by roboticus (Chancellor) on May 06, 2010 at 13:13 UTC

    avanta

    I've not done it in perl before, but plenty of times in C/C++. In cases like this, I generally have a parent fork off children, one for each independent task. The parent (original) then monitors the status of the child process(es). Typically, I also have a few shared variables just for the children to advertise any interesting internal state that the parent may be interested in.

    As I said, I've not done it in perl before, as none of my code has needed it so far. But IIRC, there are several thread/process management packages on CPAN that may be of some help to you.

    ...roboticus

Re: monitor a process's state.
by almut (Canon) on May 06, 2010 at 13:29 UTC
    ...Also, here our parent may have been completed.

    You could have the children write various status info to log files in a directory, e.g. with the files containing the PID in their names.  At a minimum, you'd need a heartbeat message (say a timestamp, printed every minute or so, as long as everything is running ok), and a "finished" status message.

    Something else (e.g. a cronjob) could then periodically scan this directory, check the files for out-of-date heartbeat timestamps, not being followed by "finished" (which would indicate that the process hangs or somehow disappeared), etc., create a summary report, and do the cleanup of the files associated with finished jobs.

    Of course, this would only work if the children's code is under your control, so you can modify it to print heartbeats, etc.

Re: monitor a process's state.
by JavaFan (Canon) on May 06, 2010 at 14:11 UTC
    What I would do:
    1. Define what "hung" means and how you can detect this (hard part).
    2. Write a nagios (or whatever monitoring tool you're using) script that checks for this condition (easy part).