Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Child thread not receiving SIGALRM

horobicahorobica Posts: 22Member
Hello people.

A thread+signal problem: the parent process sets the handler for SIGALRM and then starts a child thread. The child thread schedules and alarm call. This should make the child thread receive a SIGALRM signal, handled by the handler set by the parent process, right? Well... practice proved me wrong and I don't know why.

Actually, here is the code I've tested this whole thing with:

--- code ---
from signal import *
from thread import *
from time import *

def handle_sig(nSigNo,oFrame):
print 'Alarm raised, exiting'
exit()

def child():
print 'Child started'
alarm(2)
print 'Child: alarm set, will exit on signal'
while True:
print 'Child: tick'
sleep(1)

def sig_test():
print 'Parent started'
signal(SIGALRM,handle_sig)
start_new_thread(child,())
print 'Parent: spawned child, sleeping'
sleep(5)


sig_test()
--- end code ---


This child should of course exit after 2 ticks but, surprisingly, it keeps going until the parent exits because the child never receives the SIGALRM signal.

Any ideas what is happening here, anyone?

Thanks,
Dan

Comments

  • infidelinfidel Posts: 2,900Member
    : Hello people.
    :
    : A thread+signal problem: the parent process sets the handler for SIGALRM and then starts a child thread. The child thread schedules and alarm call. This should make the child thread receive a SIGALRM signal, handled by the handler set by the parent process, right? Well... practice proved me wrong and I don't know why.
    :
    : Actually, here is the code I've tested this whole thing with:
    :
    : --- code ---
    : from signal import *
    : from thread import *
    : from time import *
    :
    : def handle_sig(nSigNo,oFrame):
    : print 'Alarm raised, exiting'
    : exit()
    :
    : def child():
    : print 'Child started'
    : alarm(2)
    : print 'Child: alarm set, will exit on signal'
    : while True:
    : print 'Child: tick'
    : sleep(1)
    :
    : def sig_test():
    : print 'Parent started'
    : signal(SIGALRM,handle_sig)
    : start_new_thread(child,())
    : print 'Parent: spawned child, sleeping'
    : sleep(5)
    :
    :
    : sig_test()
    : --- end code ---
    :
    :
    : This child should of course exit after 2 ticks but, surprisingly, it keeps going until the parent exits because the child never receives the SIGALRM signal.
    :
    : Any ideas what is happening here, anyone?

    This is just a wild guess because I've never done threading in python, but perhaps the problem is that the parent thread is setting the signal trap while the child thread is the one receiving the signal? Are signals managed per-thread? I have no idea.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.