Hermesh Gupta
Hermesh Gupta

Reputation: 181

fork implementation in perl

I want to execute a child process in perl. I also want my code to be platform independent (same for windows as well as unix, except some glitches like filepath etc.).

The problem is perl implementation of fork in windows, is a pseudo-process, actually a thread. Please refer to the perl fork emulation for windows here.

It also mentions about problems in executing kill and exec commands on pseudo-processes. Would it be safe to use two different version of fork for different platforms or should I go with OS specific APIs?

Upvotes: 8

Views: 4672

Answers (2)

mob
mob

Reputation: 118605

Forks::Super already worries about and addresses a lot of these portability concerns, letting you portably run code like

use Forks::Super;

$pid = fork();
if ($pid != 0) {
   ...
   if (kill 'ZERO',$pid) { print "Job is running.\n"; }
   kill 'STOP', $pid; # or $pid->suspend
   kill 'CONT', $pid; # or $pid->resume
   kill 'TERM', $pid; # or $pid->terminate
   waitpid $pid, 0;   # or $pid->wait or $pid->waitpid(0)
}

Upvotes: 5

cdarke
cdarke

Reputation: 44354

Assuming you require asynchronous processes, go for the platform specific APIs (if you can wait for the child then you can use system or qx).

That's fork/exec on UNIX, Win32::Process::Create on Windows. The fork emulation on Windows was a brave try, but the platforms are so different in this area I think you are on a looser trying to produce a portable solution that ticks all the boxes.

Example: python tried to unify the interfaces with their subprocess module. It works for simple stuff, but there are 4 UNIX specific and 2 Windows specific parameters (to Popen).

Upvotes: 4

Related Questions