Thread overview
Catching signals with D
Dec 23, 2011
Matej Nanut
Dec 23, 2011
Heywood Floyd
Dec 24, 2011
Matej Nanut
Dec 24, 2011
Andrew Wiley
Dec 24, 2011
Jonathan M Davis
December 23, 2011
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. :)


December 23, 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


December 23, 2011
On 22-12-2011 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,

Have you seen: https://github.com/D-Programming-Language/druntime/blob/master/src/core/sys/posix/signal.d ?

- Alex
December 24, 2011
@Heywood Floyd: that works, but what exactly am I permitted to use inside the handler, as I assume it's a C function? This might be a useless question as non-atomic operations touching global data aren't supposed to be in signal handlers, but I'm still interested to know.

@Alex Rønne Petersen: what exactly is
https://github.com/D-Programming-Language/druntime/blob/master/src/core/sys/posix/signal.d?
I don't see it mentioned anywhere on dlang.org? :/ I'm still new to all
this stuff. When programming in C, everything I ever needed was in the
default repositories of my Linux distribution, so I neved needed to worry
about anything. :)

Thanks,
Matej


December 24, 2011
On Sat, Dec 24, 2011 at 7:37 AM, Matej Nanut <matejnanut@gmail.com> wrote:
> @Heywood Floyd: that works, but what exactly am I permitted to use inside the handler, as I assume it's a C function? This might be a useless question as non-atomic operations touching global data aren't supposed to be in signal handlers, but I'm still interested to know.
>
> @Alex Rønne Petersen: what exactly is
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/sys/posix/signal.d?
> I don't see it mentioned anywhere on dlang.org? :/ I'm still new to all this
> stuff. When programming in C, everything I ever needed was in the default
> repositories of my Linux distribution, so I neved needed to worry about
> anything. :)

That module is part of druntime, and you can import it with import core.sys.posix.signal;

The documentation isn't on dlang.org, probably because dlang.org doesn't contain the documentation for OS-specific modules (it's hard to generate the documentation for those when you're not on the same OS).
December 24, 2011
On Saturday, December 24, 2011 10:58:19 Andrew Wiley wrote:
> On Sat, Dec 24, 2011 at 7:37 AM, Matej Nanut <matejnanut@gmail.com> wrote:
> > @Heywood Floyd: that works, but what exactly am I permitted to use
> > inside
> > the handler, as I assume it's a C function? This might be a useless
> > question as non-atomic operations touching global data aren't supposed
> > to be in signal handlers, but I'm still interested to know.
> > 
> > @Alex Rønne Petersen: what exactly is https://github.com/D-Programming-Language/druntime/blob/master/src/core/ sys/posix/signal.d? I don't see it mentioned anywhere on dlang.org? :/ I'm still new to all this stuff. When programming in C, everything I ever needed was in the default repositories of my Linux distribution, so I neved needed to worry about anything. :)
> 
> That module is part of druntime, and you can import it with import core.sys.posix.signal;
> 
> The documentation isn't on dlang.org, probably because dlang.org doesn't contain the documentation for OS-specific modules (it's hard to generate the documentation for those when you're not on the same OS).

It's really not all that hard thanks to version blocks, but you do have to do some work to make it happen. It's more a case of the fact that druntime doesn't document C stuff in general. It's been argued that it should, and it's been argued that you should just look at the C docs if you want to see what they do. The reality is that it should probably document which C declarations that it has but not actually say what they do (leaving that up to the proper C documentation), but even assuming that that were agreed upon, an effort would have to be made to make that happen, and that hasn't happened.

- Jonathan M Davis