www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Catching signals with D

reply Matej Nanut <matejnanut gmail.com> writes:
--0015174c123e760fa004b4b62513
Content-Type: text/plain; charset=UTF-8

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. :)

--0015174c123e760fa004b4b62513
Content-Type: text/html; charset=UTF-8

Hello everyone, I&#39;ve been fascinated by D lately and have been using it for
all<br>my school assignments (like simple ray casting and simulated
annealing).<br><br>What I can&#39;t find anywhere is how to do something
like<br>


&quot;signal(SIGINT, myhandler)&quot; (I&#39;m in a Linux
environment).<br><br>I need this to stop the annealing process early but still
keep the current best<br>result. Is there a better way to interrupt my
program?<br>

<br>
Thanks!<span class="HOEnZb"><font
color="#888888"><br>Matej<br></font></span><br>P.s. I hope I sent this to the
appropriate address. :)

--0015174c123e760fa004b4b62513--
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 =?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
prev sibling next sibling parent Matej Nanut <matejnanut gmail.com> writes:
--0016e6dd98ac25ebe404b4d6a651
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

 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

--0016e6dd98ac25ebe404b4d6a651
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

 Heywood Floyd: that works, but what exactly am I permitted to use inside t=
he handler, as I assume it&#39;s a C function? This might be a useless ques=
tion as non-atomic operations touching global data aren&#39;t supposed to b=
e in signal handlers, but I&#39;m still interested to know.<br>

<br> <span class=3D"gD">Alex R=C3=B8nne Petersen: what exactly is </span><a=
 href=3D"https://github.com/D-Programming-Language/druntime/blob/master/src=
/core/sys/posix/signal.d" target=3D"_blank">https://github.com/D-Programmin=
g-Language/druntime/blob/master/src/core/sys/posix/signal.d</a>? I don&#39;=
t see it mentioned anywhere on <a href=3D"http://dlang.org">dlang.org</a>? =
:/ I&#39;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. :)<br>

<br>Thanks,<br>Matej<br>

--0016e6dd98ac25ebe404b4d6a651--
Dec 24 2011
prev sibling next 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=

 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=

 I don't see it mentioned anywhere on dlang.org? :/ I'm still new to all t=

 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=

  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=


 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/=


 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 =


 ever needed was in the default repositories of my Linux distributio=


 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; =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