Thread: Zombi process

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    21
    Rep Power
    0

    Zombi process


    Arg! Help! My process table is full of undead zombi threads!
    Seems my script doesn't take care of his serious child problem!

    How can you clean a child thread to prevent it from becoming a zombi thread ?
    (how can you use the os.kill command, if it is the solution...)

    Thanks,
    (BTW when i said full of zombies it looks more like 1 to 2 thousand zombies... so i'd prefer taking care of the problem)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    14
    Maybe import the os module, run loop through every process you want to kill and run a command os.system('kill -9', x)? I'm new at all this so don't kill me if that is totally irrelevant.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Are these threads created through the Python thread/threading modules, or are they child processes created with os.fork?

    Either way the thread or process should terminate naturally unless they are stuck in an infinite loop or blocked waiting for some resource to be released.

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    21
    Rep Power
    0
    Well the thing is that the kill -9 command does not work, I tried it... once the processes are written with the "defunct" mention next to them they are unkillable because they don't really exist any more i think....

    Theses processes were created with the thread module, but they should end, there is no infinit loops in them, and they don't seem to take computing time either, so it looks as if they are not running.

    Well in fact it is a little more complicated, I start a thread and in the thread i use an os.spawnv (this does an automatic fork I think?) to start another script, and it is that script that zombifies. (Should there be a return None at the end of all functions? because i tried to put a sys.exit() to be sure it exited but it still stays there as a zombi, so i'm thinking that the parent process -the thread- has to clean up by its self...)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    My understanding of sys.exit is that if called in the main thread it will kill all the other threads, but if called in a child thread it will only kill off that thread. I may be wrong though. I don't know if that is related to the problem you are having.

    Some more questions:

    * are the spawned programs also written in Python?

    * is there any way to debug them, e.g. by putting in trace statements so that you can see what they are blocking on?

    Dave - The Developers' Coach
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    I did a few experiments and I think you have two options:
    1. Only spawn the child processes in the main thread (why do you need to spawn them in a child thread, perhaps you only need threads for monitoring if at all?). This will allow the child processes to respond to the os.kill.

    2. If you must use threads then you need to signal the child process program to terminate somehow and then os.wait() for the child process to finish. But os.kill is ignored! Perhaps you can signal the child app using sockets or pipes.

    This code starts two processes and then kills them:
    Code:
    import os
    import signal
    import time
    
    procs = []
    
    for n in range(2): 
        procs.append(os.spawnvp(os.P_NOWAIT, "ping", ["ping", "127.0.0.1"]))
    
    print procs
    time.sleep(5)
    for n in procs: 
        print n
        os.kill(n, signal.SIGINT)
    This code tries to do the same thing but fails.
    The kill signal is ignored.
    Code:
    import threading
    import os
    import signal
    import time
    
    procs = []
    class Worker(threading.Thread): 
        def run(self): 
    	procs.append(os.spawnvp(os.P_NOWAIT, "ping", ["ping", "127.0.0.1"]))
    
    for n in range(2): 
        a = Worker()
        a.start()
    
    print procs
    time.sleep(5)
    for n in procs: 
        print n
        os.kill(n, signal.SIGINT)
    When the shell process from which the python program was run is killed the child processes are stopped but you are left with two do nothing zombies.

    grim
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Actually, the processes I generarted are reported as Sleeping and not Zombified.

    Not sure if this will help.

    These processes can be killed with kill -9 PID.
    Last edited by Grim Archon; June 24th, 2004 at 07:01 AM. Reason: update on kill
  14. #8
  15. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Minor changes...
    Code:
    	procs.append(os.spawnvp(os.P_NOWAIT,"ping",["ping","127.0.0.1"]))
    	os.waitpid(procs[-1],0)
    Code:
    for n in procs:
        print n
        os.system("kill -9 %s"%n)
    print "waiting for the threads to finish"
    for n in range(2):
        wt[n].join(5.0)
    Worked.

IMN logo majestic logo threadwatch logo seochat tools logo