user6734995
user6734995

Reputation:

Non-blocking child process blocks file

Consider this scenario:

We have three scripts:

script.pl

use strict;
use warnings;


print "\nStarting a blocking process";
print "\nRedirect the output of the blocking process to execution.log";
my $cmd = "perl d:\\blocking_script.pl >d:\\execution.log";
my $exitCode = system ($cmd);
print "\nAfter the execution of the blocking process";

print "\nNow I try to rename the log";
rename "d:\\execution.log", "d:\\execution.err" or print "\nCouldn't rename because : $!";

blocking_script.pl

use strict;
use warnings;

print "\nFrom the blocking_process I run a non-blocking process";
my $cmd = "start perl d:\\non_blocking_script.pl";
my $exitCode = system ($cmd);
print "\nAfter I started the non-blocking process";

non_blocking_script.pl

use strict;
use warnings;

print "\nI am an independent non-blocking process";
sleep 5;
print "\nStill here";
sleep 2;
print "\nYou can't rename the log because you didn't wait for me";
sleep 3;
print "\n.";
sleep 1;

What will result from this?

Couldn't rename because : Permission denied

While another command promopt will be hanging ironically :

I am an independent non-blocking process
Still here
You can't rename the log because you didn't wait for me
.

In my situation from perl I run an external application in a blocking way, but that application was starting some non-blocking process which were holding my log.

How can I overcome this situation?

Upvotes: 0

Views: 70

Answers (1)

Sinan Ünür
Sinan Ünür

Reputation: 118118

Here is the documentation for start (which you should also be able to read by using start /? on the command line. I do not have access to a Windows system right now, so I can't verify.

/b

Starts an application without opening a new Command Prompt window. CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.

blocking_script.pl is waiting for the cmd window which start opened to run non_blocking_script.pl.

In the short run, using start /b might help.

Or, you could try

my @cmd = start => qw(perl d:\\non_blocking_script.pl);
my $exitCode = system @cmd;

However, you should change your design.

Upvotes: 1

Related Questions