Thread overview
Shutdown signals
May 10, 2021
Tim
May 10, 2021
Adam D. Ruppe
May 10, 2021
Tim
May 10, 2021
Adam D. Ruppe
May 11, 2021
Tim
May 11, 2021
Patrick Schluter
May 11, 2021
Tim
May 10, 2021

Hi all,

How can I get a D program to detect something a keyboard interrupt so I shut things down in a specific way?

May 10, 2021

On Monday, 10 May 2021 at 23:20:47 UTC, Tim wrote:

>

Hi all,

How can I get a D program to detect something a keyboard interrupt so I shut things down in a specific way?

import core.sys.posix.signal; then you can use the same functions as C to set signal handlers.

May 10, 2021

On Monday, 10 May 2021 at 23:31:11 UTC, Adam D. Ruppe wrote:

>

On Monday, 10 May 2021 at 23:20:47 UTC, Tim wrote:

>

Hi all,

How can I get a D program to detect something a keyboard interrupt so I shut things down in a specific way?

import core.sys.posix.signal; then you can use the same functions as C to set signal handlers.

I can't find that in the docs, nor in dpldocs. Can you help out with this?

May 10, 2021

On Monday, 10 May 2021 at 23:35:06 UTC, Tim wrote:

>

I can't find that in the docs, nor in dpldocs. Can you help out with this?

dpldocs.info/signal it comes up as the second result.

The C function you call from there (on linux anyway) is sigaction. A little copy/paste out of my terminal.d:

      // I check this flag in my loop to see if an interruption happened
      // then i can cleanly exit from there.
       __gshared bool interrupted;

      // the interrupt handler just sets the flag
       extern(C)
       void interruptSignalHandler(int sigNumber) nothrow {
               interrupted = true;
       }

       // then this code registers the handler with the system
       import core.sys.posix.signal;
       sigaction_t n;
       n.sa_handler = &interruptSignalHandler;
       sigaction(SIGINT, &n, &oldSigIntr); // third arg can also be null if you don't care about the old one
May 11, 2021

On Monday, 10 May 2021 at 23:55:18 UTC, Adam D. Ruppe wrote:

>

On Monday, 10 May 2021 at 23:35:06 UTC, Tim wrote:

>

[...]

dpldocs.info/signal it comes up as the second result.

The C function you call from there (on linux anyway) is sigaction. A little copy/paste out of my terminal.d:

      // I check this flag in my loop to see if an interruption happened
      // then i can cleanly exit from there.
       __gshared bool interrupted;

      // the interrupt handler just sets the flag
       extern(C)
       void interruptSignalHandler(int sigNumber) nothrow {
               interrupted = true;
       }

       // then this code registers the handler with the system
       import core.sys.posix.signal;
       sigaction_t n;
       n.sa_handler = &interruptSignalHandler;
       sigaction(SIGINT, &n, &oldSigIntr); // third arg can also be null if you don't care about the old one

I don't know why I didn't find that. I was searching for the full name, maybe too specific? Thanks anyways, this is super helpful. I wish it was documented better though :(

So why use sigaction and not signal? From what I can tell signal is the C way of doing things

May 11, 2021

On Tuesday, 11 May 2021 at 06:44:57 UTC, Tim wrote:

>

On Monday, 10 May 2021 at 23:55:18 UTC, Adam D. Ruppe wrote:

>

[...]

I don't know why I didn't find that. I was searching for the full name, maybe too specific? Thanks anyways, this is super helpful. I wish it was documented better though :(

So why use sigaction and not signal? From what I can tell signal is the C way of doing things

Use sigaction(), signal() has problems. See this stackoverflow 1 question explains the details

May 11, 2021

On Tuesday, 11 May 2021 at 06:59:10 UTC, Patrick Schluter wrote:

>

On Tuesday, 11 May 2021 at 06:44:57 UTC, Tim wrote:

>

On Monday, 10 May 2021 at 23:55:18 UTC, Adam D. Ruppe wrote:

>

[...]

I don't know why I didn't find that. I was searching for the full name, maybe too specific? Thanks anyways, this is super helpful. I wish it was documented better though :(

So why use sigaction and not signal? From what I can tell signal is the C way of doing things

Use sigaction(), signal() has problems. See this stackoverflow 1 question explains the details

Thanks a lot!