Code Comments
Programming Forum and web based access to our favorite programming groups.Hello group, I have a problem that i have to create a new process, when my process gets a SIGCHLD. So i want to use fork inside the signal handler for SIGCHLD. Can i use like that? I know that non-reentrant functions can not be used inside a signal handler. Is fork() a reentrant function or not? And in the signal handler i'm traversing a linked list which has the complexity of O(n2). Is that ok if a signal handler to be that heavy? Note: no global variables are being changed inside the handler. Please assist me ASAP. Thanks. -Kiran.
Post Follow-up to this messagekiran <edu.mvk@gmail.com> writes: > I have a problem that i have to create a new process, when my process > gets a SIGCHLD. So i want to use fork inside the signal handler for > SIGCHLD. > > Can i use like that? Assuming there are no 'other issues' (ie 'multiple threads'), yes. I would nevertheless rather do the fork outside the signal handler. > I know that non-reentrant functions can not be > used inside a signal handler. This is wrong. A signal handler must only call functions which are async-signal-safe (that's different from 'reentrant') if the signal could have interrupted a non async-signal-safe function. A list of those is given in the 'Signal Concepts' section of SUS. > And in the signal handler i'm traversing a linked list which has the > complexity of O(n2). Traversing a linked-list is an operation with a linear complexity (O(n)).
Post Follow-up to this messagekiran <edu.mvk@gmail.com> writes: >I have a problem that i have to create a new process, when my process >gets a SIGCHLD. >So i want to use fork inside the signal handler for SIGCHLD. >Can i use like that? I know that non-reentrant functions can not be >used inside a signal handler. Is fork() a reentrant function or not? >And in the signal handler i'm traversing a linked list which has the >complexity of O(n2). Is that ok if a signal handler to be that heavy? >Note: no global variables are being changed inside the handler. How do you guarantee that the linked list is in a stable state in the signal handler? fork() can legally be called from signal handlers (per POSIX). See http://www.unix.org/single_unix_specification/ Casper -- Expressed in this posting are my opinions. They are in no way related to opinions held by my employer, Sun Microsystems. Statements on Sun products included here are not gospel and may be fiction rather than truth.
Post Follow-up to this messagekiran <edu.mvk@gmail.com> writes: > I have a problem that i have to create a new process, when my process > gets a SIGCHLD. > So i want to use fork inside the signal handler for SIGCHLD. Have you read the documentation for the operating system you're using? You didn't say what you're using, but on Solaris I see this for fork(2): | MT-Level | Async-Signal-Safe. | and the attributes(5) page says: Async-Signal-Safe Async-Signal-Safe refers to particular library functions that can be safely called from a signal handler. A thread that is executing an Async-Signal-Safe function will not deadlock with itself if interrupted by a sig- nal. Signals are only a problem for MT-Safe functions that acquire locks. Async-Signal-Safe functions are also MT-Safe. Signals are disabled when locks are acquired in Async-Signal- Safe functions. These signals prevent a signal handler that might acquire the same lock from being called. So, you can do it there at least. > And in the signal handler i'm traversing a linked list which has the > complexity of O(n2). Is that ok if a signal handler to be that heavy? > Note: no global variables are being changed inside the handler. That's something you'll have to evaluate on your own. Nobody here knows the underlying requirements or detailed design of your program. -- James Carlson, Solaris Networking <james.d.carlson@sun.com> Sun Microsystems / 35 Network Drive 71.232W Vox +1 781 442 2084 MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.