On Friday, 8 April 2016 at 13:25:37 UTC, Adam D. Ruppe wrote:
On Friday, 8 April 2016 at 13:23:10 UTC, Adam D. Ruppe wrote:
Odds are it is that there's terminal output for the background process

NOT a character btw, just any output, then the OS puts you on hold so it can do its thing.

To catch a signal, it is just like in C http://stackoverflow.com/a/35687472/1457000

simplest case:

import core.stdc.signal;
signal(SIGTTOU, SIG_IGN); // ignore the output thing


and see what happens next

Found another problem: I can't even catch SIGINT in a simple program:

import std.stdio;
import std.process;
import core.stdc.signal;
import core.thread;
import std.datetime;


extern(C) void sig_hand(int signal) nothrow @nogc @system {
    import core.stdc.stdio: printf;
    printf("signal %d catched!\n", signal);
}

void main() {
    if (signal(SIGINT, &sig_hand) == SIG_ERR) {
        writeln("can't catch SIGINT");
    }

    string line;
    write("> ");
    while(1) {
        Thread.sleep(1.seconds);
    }
    /*
    while ((line = readln()) != null) {
        writeln("you said:", line);
        write("> ");
    }
    */
}

When using while with Thread.sleep, Ctrl+C will show

signal 2 catched!

then the program is gone, but its process is still found with `ps -ef`, which I have to kill manually.

In the c version as shown here <http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/>, the loop will go on and you can press multiple Ctrl-C.

The D version behavior is strange.

When Using while with readln, after hitting Ctrl-C, the next readln will throw exception:

std.stdio.StdioException@std/stdio.d(3977): Input/output error
----------------
??:? void std.stdio.StdioException.opCall() [0x498531]
??:? ulong std.stdio.readlnImpl(shared(core.stdc.stdio._IO_FILE)*, ref char[], dchar, std.stdio.File.Orientation) [0x498676] /usr/include/dmd/phobos/std/stdio.d:1565 ulong std.stdio.File.readln!(char).readln(ref char[], dchar) [0x486fac] /usr/include/dmd/phobos/std/stdio.d:1426 immutable(char)[] std.stdio.File.readln!(immutable(char)[]).readln(dchar) [0x486eb1] /usr/include/dmd/phobos/std/stdio.d:3385 immutable(char)[] std.stdio.readln!(immutable(char)[]).readln(dchar) [0x486e3b]
source/app.d:25 _Dmain [0x48677e]
??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x48ad2a] ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x48ac74] ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x48ace6] ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x48ac74]
??:? _d_run_main [0x48abe5]
??:? main [0x487b0b]
??:? __libc_start_main [0xc7628ec4]

Reply via email to