Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

tango.stdc.signal

Moderators: larsivi kris

Posted: 05/07/09 17:57:32

Could I get a simple code example of how I can utilize the signal function in my code? I looked online and at the actual signal.d file and could not figure out how to compile and get it working. I am stuck. Please help.

Author Message

Posted: 05/07/09 21:28:56

It isn't very recommended to use signals in D, and in particular are USR1 and 2 for use by the D runtime only.

Thus the main reason for using signals in D should be to catch those signalled by the system. But maybe you could tell why you want to use signals?

Note that the signals you reference are quite different from the signals from "Signals & Slots" as used in Qt, and partially in tango.core.Signal.

Posted: 05/07/09 22:08:59

I understand we cannot use SIGUSR1 and SIGUSR2 as it is explained in the signal.d file. Nevertheless, I want to run services and get a clean shut down with use of signals as in C/C++. It would be really helpful to use this feature.

I have included a simple snippet below. I am not sure if I should be using extern(C)? Could someone explain what I am doing wrong?

module etc.hello;

import tango.io.Stdout; import tango.stdc.signal;

class Hello {

this() {

signal(SIGINT, exit_program);

while(true) {

//Do nothing

}

}

void exit_program(int signum) {

Stdout.formatln("Signum {}", signum);

}

}

void main(char[][] args) {

new Hello();

}

Posted: 05/08/09 05:55:59

This worked (Tango 0.99.8 Linux):

import tango.io.Stdout;
import tango.stdc.signal;

void exit_program(int signum) {
    Stdout.formatln("Signum {}", signum);
}

class Hello {
    this() {
        signal(SIGINT, cast(sigfn_t) &exit_program);
        while (true) {
            // Do nothing
        }
    }
}

void main(char[][] args) {
    new Hello;
}

Perhaps a method cannot be passed to stdc, but a free function can?

Posted: 05/08/09 15:35:55

Yes, a function pointer (taken from a free function) is compatible with C function pointers, the other sort (delegates) are not.

Posted: 05/08/09 16:48:04

Thanks very much to both of you. I just learned something new. Very cool.