2

I was actually creating a script in which fork() creates a child process that runs in the background, and checks the time period of the main script (parent process), running in the foreground, using its process ID. If the main script (parent process) exceeds the threshold time, then action/s would be taken.

In Linux, it got implemented because of the INIT process that becomes the parent of the active child process (orphaned process) after the main script(parent) is killed or finished.

But, I am not able to implement it in Windows, as parent-child architecture is different from Linux.

Short code for the same (in Linux) under Perl language is:

sub run_sleep { 
    my $pid = fork();  ## Here, $pid var. will have child process PID for parent, and value 0 for child process
    return if $pid;     # returns to the parent process( out of function)   
    print "Running child process\n";   # Proceeds for the child process
    select undef, undef, undef, $initial_time_wait ;
    if ( kill 0, $Parent_ID ) {    ##Here, $Parent_ID is main script id (parent process id)
      print "Parent process $Parent_ID_or_script_id still exists\n";
    }
    else {
      print "Parent process $Parent_ID_or_script_id must have completed";
      exit 0;
    }
    print "Done with child process\n";
    exit 0;  # end child process
}

How to implement this for Windows?

Dada
  • 6,313
  • 7
  • 24
  • 43
AskQuestionsP
  • 153
  • 1
  • 9
  • Using a new thread within the same process may be an easier approach to implement some sort of watchdog. – Robert Jul 05 '21 at 11:39
  • 2
    Apparently in perl ["Windows implementations of Perl emulate forking using threads"](https://stackoverflow.com/questions/8727001/multiprocessing-via-perl-on-windows) which would be why killing the parent process kills the child, technically they are both the same process. You might need to find a way to spawn full fledged *processes* on Windows rather than threads. https://stackoverflow.com/questions/45792/how-can-i-fork-a-background-processes-from-a-perl-cgi-script-on-windows – Mokubai Jul 05 '21 at 11:48

2 Answers2

2

Windows doesn't provide a fork mechanism. Perl provides a limited emulation using threads. Since no child process is created, there is no child process to keep alive.

You might be able to use something like the following (but you would need to communicate any relevant state to the child since it's not a copy of the parent):

if (@ARGV && $ARGV[0] eq "!fork!") {
   shift(@ARGV);
   child();
   exit;
}

...

my $pid;
if ($^O eq 'Win32')  {
   $pid = system(-1, $^X, '--', $0, "!fork!", ...args...);
} else {
   ...
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
1
  1. In Windows, when you kill the parent process, its children are also killed. (Not like Linux, where after the parent is killed, children get their new parent -INIT with PID 1).
  2. In windows, the parent will also not exit automatically (parent PID will be present) if the child is still running. (For your question).

So, the solution would be that you add a step where before ending of the main script, child PID is killed; and so as when child will be killed, the parent would also be able to exit successfully.

So, now if your main script finishes before the threshold time, it means no action is required by the child (And also, child will be killed before the main script completes). And if the main script crosses the threshold, the child will send a mail. And when the main script will be about to end, child will be killed first, and the main script can get exited successfully.

AskQuestionsP
  • 153
  • 1
  • 9