www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Shutdown signals

reply Tim <tim.oliver tutanota.com> writes:
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
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent reply Tim <tim.oliver tutanota.com> writes:
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
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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: ```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 10 2021
parent reply Tim <tim.oliver tutanota.com> writes:
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: ```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 10 2021
parent reply Patrick Schluter <Patrick.Schluter bbox.fr> writes:
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 [1]: https://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal
May 10 2021
parent Tim <tim.oliver tutanota.com> writes:
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 [1]: https://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal
Thanks a lot!
May 11 2021