Signals in the plug

What is the behavior of the signal in the plug. If all signals are inherited in fork. If not, which and why?

+4
source share
3 answers

At least in Linux, signal handlers themselves are inherited, but not waiting for a signal.

Quoting Linux fork(2) man page :

fork () creates a child process that differs from the parent process only in its PID and PPID, and that the resource value is set to 0. File locks and pending signals are not inherited.

This makes sense because the signals are related to the (parent) process. A newly created process (basically) is a copy of the current process, so the signal handlers are saved.

Although this is not directly related, a call to the exec() , which often follows fork() , will destroy all signal handlers as a new new executable is loaded into the process (overwriting the functions that currently handle the signals).

+13
source

But to be absolutely sure that your code is portable and consistent on all platforms, it is better to check the behavior of signals that may affect the execution of your program. While linux ensures that implementations are free to choose the way they want sigaction to come in handy for the same.

0
source

I will go with paxdiablo's answer that pending signals are reinitialized, although the signal handler is copied. Here, the kernel source code in do_fork is debugged, which actually performs forking on behalf of the process.

 /*do_fork(...)*/ spin_lock_init(&p->alloc_lock); init_sigpending(&p->pending);// reinitializing the pending signals 
0
source

Source: https://habr.com/ru/post/1315385/


All Articles