How to check if child process is still alive?

A

Andreas S

how do you check if your child process from fork still alive or not? In perl
I can send kill(0, $cid), I think. But Process.kill(0, child_id) always
returns 1.

To be honest, I couldn't get the perl code to work either. It always returns
1 too.

I can do `ps -p #{child_id}` but I wonder if Process.kill should work too.

-andre

_________________________________________________________________
Win a Zune™—make MSN® your homepage for your chance to win!
http://homepage.msn.com/zune?icid=hmetagline
 
A

ara.t.howard

how do you check if your child process from fork still alive or not? In p= erl=20
I can send kill(0, $cid), I think. But Process.kill(0, child_id) always= =20
returns 1.

To be honest, I couldn't get the perl code to work either. It always retu= rns=20
1 too.

I can do `ps -p #{child_id}` but I wonder if Process.kill should work too= =2E

-andre

_________________________________________________________________
Win a Zune=99=97make MSN=AE your homepage for your chance to win!=20
http://homepage.msn.com/zune?icid=3Dhmetagline

#
# returns true if pid is running, false otherwise
#
def alive pid
#--{{{
pid =3D Integer("#{ pid }")
begin
Process::kill 0, pid
true
rescue Errno::ESRCH
false
end
#--}}}
end
alias alive? alive


it should always return 1. it raises if the pid is dead or you do not have
permission to signal the process.

-a
--=20
be kind whenever possible... it is always possible.
- the dalai lama
 
G

Gary Wright

how do you check if your child process from fork still alive or
not? In perl I can send kill(0, $cid), I think. But Process.kill(0,
child_id) always returns 1.

To be honest, I couldn't get the perl code to work either. It
always returns 1 too.

I can do `ps -p #{child_id}` but I wonder if Process.kill should
work too.

Using Process.kill(0, pid) will return 1 as long as the process
exists. In
your situation, I'm guessing that the child process has terminated
but continues to exist as a zombie process. You have to call
Process.wait
to reap the child process in order for the child process to disappear
entirely.

pid = fork { sleep(10) }

puts Process.kill(0, pid) # 1
sleep 15
puts Process.kill(0, pid) # 1, process is a zombie at this point
Process.wait
puts Process.kill(0, pid) # exception, process doesn't exist

Gary Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top