Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dlang install some signal handlers by default

Tags:

linux

signals

d

I have infinite loop program written in D:

$ cat dprog.d 
import std.stdio;
import core.thread;

void main()
{
  while(1){
    Thread.sleep(dur!("seconds")(1));
  };
}

When I build and run this program on Linux (Ubuntu), kill -10 $PID has no effect on it. And indeed, it does catch a bunch of signals by default:

$ cat /proc/$PID/status | grep SigCgt
SigCgt: 0000000180000a00

Signal 10 is a SIGUSR1 signal, for which the default action is process termination (consult man 7 signal).

Equivalent program, written in C terminates on kill -10 $PID and its cat /proc/$PID/status | grep SigCgt is SigCgt: 0000000000000000.

Equivalent program, written in Rust also terminates on kill -10 $PID, however its cat /proc/$PID/status | grep SigCgt is SigCgt: 0000000180000440.

My question is: does D (Rust) compiler register default signal handlers? If yes, why? Also, is this documented somewhere?

like image 299
ikostia Avatar asked Jul 11 '18 23:07

ikostia


People also ask

How do I get out of signal handler?

Signal handlers can be specified for all but two signals (SIGKILL and SIGSTOP cannot be caught, blocked or ignored). If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling abort() , exit() , or longjmp() .

How do I create a signal handler in Unix?

*/ . . /* set a signal handler for ALRM signals */ signal(SIGALRM, catch_alarm); /* prompt the user for input */ printf("Username: "); fflush(stdout); /* start a 30 seconds alarm */ alarm(30); /* wait for user input */ gets(user); /* remove the timer, now that we've got the user's input */ alarm(0); . . /* do something ...

What does a signal handler return?

It returns back to where it was in your code when the signal was triggered. Many libraries and applications exploit the same mechanisms to implement threadless multitasking (for instance libmill).


1 Answers

The docs for core.thread.thread_setGCSignals tell us that, on Posix systems, SIGUSR1 and SIGUSR2 are used for the runtime to control suspending and resuming threads for GC purposes.

This function allows you to change which signals are used, in case there are different signals you don't need.

like image 81
dhasenan Avatar answered Nov 09 '22 06:11

dhasenan