Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to portably stop processes?

by haj (Vicar)
on Mar 31, 2023 at 14:24 UTC ( [id://11151373]=perlquestion: print w/replies, xml ) Need Help??

haj has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

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

Replies are listed 'Best First'.
Re: How to portably stop processes?
by NERDVANA (Deacon) on Mar 31, 2023 at 17:13 UTC

    Actually fork and exec don't "work fine" on Windows :-( They're cheap approximations that have all sorts of bad consequences for programs written in a Unix mindset. You can read about it on Proc::Background::Win32

    That module is also my recommendation for how to handle your problem. (disclaimer: I'm the maintainer) (and bah, there's a POD bug on that page. I'll get that fixed...)

    Oh, but if you use cygwin perl, fork and exec work fine and do create new processes.

      Actually fork and exec don't "work fine" on Windows :-(

      Fair enough: I should have been more specific. They were good enough for my particular problem ... until I wanted to have "infinite" processes.

      And indeed, Proc::Background is pretty much exactly what I was hoping for. Thanks a lot!

Re: How to portably stop processes?
by eyepopslikeamosquito (Archbishop) on Apr 01, 2023 at 03:19 UTC
Re: How to portably stop processes?
by LanX (Saint) on Mar 31, 2023 at 17:26 UTC
    I'm no expert, but fork and perlfork talk at length about not to use kill on children when fork was emulated.

    See https://perldoc.perl.org/perlfork#kill()

    There are various alternatives listed in perlipc though.

    An SSCCE with tests might help provide alternatives.

    > If I just use Win32::Process, then this would be a dependency also on Linux

    I'm quite sure that you can have install dependencies based on the actual OS. Never tried this tho.

    HTH. :)

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11151373]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found