Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

kill 0 always true

by Xxaxx (Monk)
on Mar 05, 2002 at 23:51 UTC ( [id://149558]=perlquestion: print w/replies, xml ) Need Help??

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

I've been working with fork(), and kill in an attempt to resolve a problem with a `lynx -source $page` hanging periodically. I thought from various docs that my $result = kill 0, $pid; would tell me if the child process was running or done. But alas the result is always 1. How do I get around this? Claude

Replies are listed 'Best First'.
(crazyinsomniac: pod ) Re: kill 0 always true
by crazyinsomniac (Prior) on Mar 06, 2002 at 00:11 UTC
    You ask
    I've been working with fork(), and kill in an attempt to resolve a problem with a `lynx -source $page` hanging periodically. I thought from various docs that my $result = kill 0, $pid; would tell me if the child process was running or done. But alas the result is always 1. How do I get around this? Claude
    perldoc -f kill says:

    kill SIGNAL, LIST Sends a signal to a list of processes. Returns the number of processes successfully signaled (which is not necessarily the same as the number actually killed).
    $cnt = kill 1, $child1, $child2; kill 9, @goners;
    If SIGNAL is zero, no signal is sent to the process. This is a useful way to check that the process is alive and hasn't changed its UID. See the perlport manpage for notes on the portability of this construct.

    Unlike in the shell, if SIGNAL is negative, it kills process groups instead of processes. (On System V, a negative *PROCESS* number will also kill process groups, but that's not portable.) That means you usually want to use positive not negative signals. You may also use a signal name in quotes. See the section on "Signals" in the perlipc manpage for details.

    so then I perldoc perlport, and it says:

    kill SIGNAL, LIST

    Not implemented, hence not useful for taint checking. (Mac OS, RISC OS)

    kill() doesn't have the semantics of raise(), i.e. it doesn't send a signal to the identified process like it does on Unix platforms. Instead kill($sig, $pid) terminates the process identified by $pid, and makes it exit immediately with exit status $sig. As in Unix, if $sig is 0 and the specified process exists, it returns true without actually terminating it. (Win32)

    So now you have to ask yourself what OS you are on?

    It would appear your process is not done (i realize you know).

    As far as I can see, you can't get around it (it's not quite a zombie). If you're doing any kind of forking, i'd really reccomend Parallel::ForkManager.

    Also, check out perlipc and perlfork (if you have it).

    This has been an excursion into the world of pod, click here for the friendly guide (If it's got no pod, throw it out -- podmaster).

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: kill 0 always true
by Zaxo (Archbishop) on Mar 06, 2002 at 00:28 UTC

    It may be true because you're making zombies.

    To know when a child exits, install a SIGCHLD handler. If you set $SIG{CHLD} = 'IGNORE'; the kill trick will work. kill 0, $pid; is more useful for confirming the child has started, right after the fork. The wait or waitpid calls are often used, but since you're debugging a stuck process they are not suitable for you in the body of your program.

    If you still are running when the child exits, the child's process hangs around until you read it or say you don't want it. Thus are born zombies if you dont handle them.

    After Compline,
    Zaxo

Re: kill 0 always true
by rjray (Chaplain) on Mar 06, 2002 at 00:11 UTC

    This is telling you. Read the entry for kill in perlfunc. The return value is the number of processes that would have received the signal. Since you are only test-killing one, the answer would only ever be 0 or 1. Getting back 1 means that the process ID in $pid is still alive (albeit perhaps frozen), and hasn't changed its UID (so you can still send it signals).

    --rjray

Re: kill 0 always true
by VSarkiss (Monsignor) on Mar 06, 2002 at 00:11 UTC

    It really depends on your operating system. In most Unix variants, this will only tell you if a process with the given pid exists in the process table, not whether it's doing anything. In other words, it will return true unless and until the process in question exits.

    I'm not sure if it's the same on Win32, but I suspect it is.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://149558]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-28 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found