|XP is just a number|
How to portably stop processes?by haj (Vicar)
|on Mar 31, 2023 at 14:24 UTC||Need Help??|
haj has asked for the wisdom of the Perl Monks concerning the following question:
I am looking for a "clean" way to terminate processes I've created on both Linux and Windows.
The processes exec an external program which in some situations intentionally runs "forever", or until stopped by someone else.
On Linux, this is straightforward: $child_pid = fork, then in the child process exec. Later, kill 1, $child_pid from the parent process.
On Windows (Windows 11 in my case), fork and exec are emulated but work quite fine... only that I don't know how to kill the subprocess. The PID returned from fork is apparently useless. When I kill 1 $child_pid, the main process complains Terminating on signal SIGHUP(1) and terminates immediately, leaving the external process running like a zombie. I can get rid of the zombie by entering Ctrl-C on the command line.
On Windows - and only on Windows - I can replace fork, exec and kill by the corresponding functions from Win32::Process. If I just use Win32::Process, then this would be a dependency also on Linux, which is unwanted, so I'd go for a solution with if, and write two separate helper modules.
Has this exercise already been done? I didn't find anything under Process...