Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 968
Hermesh Gupta Avatar asked Jan 07 '13 11:01

Hermesh Gupta


People also ask

What is fork () explain it with an example?

fork() in CIt takes no parameters and returns an integer value. Below are different values returned by fork(). Negative Value: creation of a child process was unsuccessful. Zero: Returned to the newly created child process. Positive value: Returned to parent or caller.

What is fork () used for?

System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.

How do you create 4 processes using fork?

Multiple calculations in 4 processes using fork()1st child sort the array. 2nd child find total even number(s) in a given array. 3rd child calculate the sum of even numbers in an array.

Does fork () create a new process?

fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process. The child process and the parent process run in separate memory spaces.


2 Answers

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)
}
like image 178
mob Avatar answered Oct 05 '22 13:10

mob


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

like image 29
cdarke Avatar answered Oct 05 '22 13:10

cdarke