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.

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)).
.



Relevant Pages

  • 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: 2 questions about signals
    ... apply, after the fork(), to both parent and child processes. ... I had been thinking that if one wanted to have the signal handler ... return value of getpid(). ...
    (comp.unix.programmer)
  • Re: 2 questions about signals
    ... apply, after the fork(), to both parent and child processes. ... I had been thinking that if one wanted to have the signal handler ...
    (comp.unix.programmer)
  • Re: pthread and async-signal safety.
    ... I searched SUS specification and I found no pthread_* function ... in what they call async-signal-safe functions. ... deadlock in a signal handler. ... Since 'fork' is required to be async-signal safe, ...
    (comp.unix.programmer)
  • Re: 2 questions about signals
    ... apply, after the fork(), to both parent and child processes. ... I had been thinking that if one wanted to have the signal handler ... return value of getpid(). ... standards seem to be saying that there might be, ...
    (comp.unix.programmer)