Asynchronous plug safety signal ()

According to the Oracle Multithreaded Programming Guide , fork() should be safe to use inside signal handlers. But my process is stuck inside a signal handler, followed by a trace back:

  #0 __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95 #1 0x00007f86e6a9990d in _L_lock_48 () from /lib/x86_64-linux- gnu/libc.so.6 #2 0x00007f86e6a922ec in ptmalloc_lock_all () at arena.c:242 #3 0x00007f86e6ad5e82 in __libc_fork () at ./nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c:95 #4 0x00007f86e7d9f125 in __fork () at ./nptl/sysdeps/unix/sysv/linux/pt-fork.c:25 .... #7 signal handler called 

Since malloc unsafe to use in a signal handler, how can fork be?

Thanks in advance.

+5
source share
2 answers

Currently listed as a RedHat error :

Error 1422161 - glibc: fork is not safe for asynchronous signal

...

+++ This error was originally created as a clone of the error # 1422159 +++

POSIX requires fork to be safe for an asynchronous signal. Our current exercise is not.

+3
source

fork () will start a new process by copying some of the memory of the parents, but both of them are separate processes. Therefore, it is safe to use the built-in signal handler. The following is an example of starting a child ....

 #include <stdio.h> #include <unistd.h> #include <signal.h> #include <stdlib.h> int SHOULD_RUN = 1; void int_sig_handler(int signal){ printf("SIG_INT\n"); if(fork() == 0){ // child code printf("I am Child"); //signal(SIGINT, int_sig_handler); SHOULD_RUN = 1; while(SHOULD_RUN){ printf("I am Running Still...\n"); sleep(1); } exit(0); }else{ // parent code printf("parent"); } SHOULD_RUN = 0; } int main(int argc, const char *argv[]){ signal(SIGINT, int_sig_handler); while(SHOULD_RUN){ sleep(1); } return 0; } 
-2
source

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


All Articles