Re: Can fork be used in a signal handler?



kiran <edu.mvk@xxxxxxxxx> 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@xxxxxxx>
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
.



Relevant Pages

  • Re: Signal Handlers - sort of mystified by this
    ... > Why can't printf() be used inside a signal handler? ... > be used inside a signal handler, then how come Dr. W Richard Stevens ... It is safe to call any async-signal-safe function in signal handler. ...
    (comp.unix.programmer)
  • Re: 2.6.x Fork Problem?
    ... >> or the parent set up a SIGCHLD handler before the fork. ... >> So, by the time the parent gets the CPU, the child is long gone. ... that we needed to change the signal handler for SIGCHLD. ...
    (Linux-Kernel)
  • Re: Pthreads and SIGCHLD
    ... > with fork() receiving (ie having its thread of execution asynchronously ... > interrupted by) the SIGBCHLD signal handler when the child terminates, ... SIGCHLD signal is "process directed", which means it will be delivered to ...
    (comp.unix.programmer)
  • Re: waitpid == -1 and errno == ECHILD
    ... supposing that the signal handler for SIGCHLD is NOT SIG_IGN? ... In what conditions we could have errno == ECHILD? ... I noticed that if I kill -9 the child process, ...
    (comp.unix.programmer)
  • Re: Pthreads and SIGCHLD
    ... > with SIGCHLD blocked. ... and the new thread unlocking the mutex once it has set up its signal mask. ... What the signal handler does isn't important so long as it doesn't ... fiddle with the critical variables concurrently with the main thread. ...
    (comp.unix.programmer)