tags:

views:

66

answers:

2

I have some perl code which establishes a signal handler:

$SIG{'KILL'} = sub {
....
};

I'm tasked with porting this to windows and I was wondering how I could generate this signal from a C# class.

I saw the Process.Kill method in the System.Diagnostics.Process class which seems to allow me to create (via another method) and kill my process, but I can't figure out how get it to send the signal to the perl script.

+3  A: 

You're probably thinking of the TERM signal handler, since KILL can't be caught. Anyway, Windows doesn't use signals and while it lets you use them, it's within a single thread only, no sending signals to other processes.

You have a variety of other IPC mechanisms though, the usual one for requesting that another process exit gracefully is PostMessage(WM_QUIT), but that's really only applicable to graphical applications which Perl scripts usually aren't.

A well supported approach on Windows would be for the parent process to create an event (with the inheritable flag) and pass the handle to the child process, putting it into the environment would be very convenient for Perl. Then the child can perform an action in response to the event.

In addition to window message and kernel events you could use any of sockets, pipes, files, mailslots, process exit codes, shared memory, registry entries, and DDE to communicate between processes on Windows.

Ben Voigt
PostMessage(..) variations may be aimed at some miniperl.c variant (GUI-based perl interpreter just to catch a message?), some embedded XS code, or, (guess it has kinda similar under the hood) GTK window.
mhambra
Yes Perl scripts can create windows and receive messages, it's just unusual. `WM_QUIT` isn't actually sent to a window, but to a thread. As long as the thread is running a message loop it doesn't actually need a window.
Ben Voigt
@Ben Do you think you could elaborate a little on what you mean by "create an event?"
tzenes
In the caller, it's a call to the Windows API CreateEvent. The .NET way to call it is creating an instance of `EventWaitHandle`. The perl way is `Win32::Event` I think you'll have to use named events instead of inheritance because neither .NET nor Perl seem to provide a way to use events by numeric handle. Well, perl does mention `get_Win32_IPC_HANDLE`, but it'd probably be easier to generate a unique event name and pass that in an environment variable instead of the handle. http://search.cpan.org/~cjm/Win32-IPC/lib/Win32/Event.pm
Ben Voigt
A: 

As OS, Windows does not support signals. Perl on Windows has emulation of signals for

  • Ctrl-C

  • You can send a signal to other thread of the same Perl program.

Alexandr Ciornii