Catching signals with D

Heywood Floyd soul8o8 at gmail.com
Thu Dec 22 18:31:32 PST 2011


On 12/22/11 23:51 , Matej Nanut wrote:
> Hello everyone, I've been fascinated by D lately and have been using it
> for all
> my school assignments (like simple ray casting and simulated annealing).
>
> What I can't find anywhere is how to do something like
> "signal(SIGINT, myhandler)" (I'm in a Linux environment).
>
> I need this to stop the annealing process early but still keep the
> current best
> result. Is there a better way to interrupt my program?
>
> Thanks!
> Matej
>
> P.s. I hope I sent this to the appropriate address. :)

Hi!

I don't know of any official way, but since D links against the c 
runtime you can just hook up functions from there, I believe. This works 
on osx at least:


import	std.stdio;

extern(C) void signal(int sig, void function(int) );

// Our handler, callable by C
extern(C) void handle(int sig){
	writeln("Signal:",sig);
}

void main()
{
	enum SIGINT = 2; // OS-specific

	signal(SIGINT,&handle);
	writeln("Hello!");
	readln();
	writeln("End!");
}



$ rdmd sig.d
Hello!
^CSignal:2
^CSignal:2

End!
$ _



/HF




More information about the Digitalmars-d-learn mailing list