www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Catching signals with D

reply Matej Nanut <matejnanut gmail.com> writes:
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. :)
Dec 22 2011
next sibling parent Heywood Floyd <soul8o8 gmail.com> writes:
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
Dec 22 2011
prev sibling next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
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/cor /sys/posix/signal.d ? - Alex
Dec 23 2011
next sibling parent Matej Nanut <matejnanut gmail.com> writes:
 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=C3=B8nne 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
Dec 24 2011
prev sibling parent Andrew Wiley <wiley.andrew.j gmail.com> writes:
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 quest=
ion
 as non-atomic operations touching global data aren't supposed to be in
 signal handlers, but I'm still interested to know.

  Alex R=F8nne Petersen: what exactly is
 https://github.com/D-Programming-Language/druntime/blob/master/src/core/s=
ys/posix/signal.d?
 I don't see it mentioned anywhere on dlang.org? :/ I'm still new to all t=
his
 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).
Dec 24 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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> w=
rote:
  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 suppo=
sed
 to be in signal handlers, but I'm still interested to know.
=20
  Alex R=C3=B8nne 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 distributio=
n,
 so I neved needed to worry about anything. :)
=20 That module is part of druntime, and you can import it with import core.sys.posix.signal; =20 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=20 some work to make it happen. It's more a case of the fact that druntime= =20 doesn't document C stuff in general. It's been argued that it should, a= nd it's=20 been argued that you should just look at the C docs if you want to see = what=20 they do. The reality is that it should probably document which C declar= ations=20 that it has but not actually say what they do (leaving that up to the p= roper C=20 documentation), but even assuming that that were agreed upon, an effort= would=20 have to be made to make that happen, and that hasn't happened. - Jonathan M Davis
Dec 24 2011