0

I wrote this code that should open several process , the problem is its work on linux well but when i execute it on windows its just create one process !!. is this possible to create multiprocess on windows with perl ?

$j = ARGV[0];
for($i=1; $i<=$j; $i++){
system("perl example.pl word.txt.$i &");
}

3 Answers3

6

& is a *nix thing. An explicit fork in Windows will do it.

Bear in mind that Windows implementations of Perl emulate forking using threads, so that may be another option.

my @pids;
for my $i (1 .. $j) {

    my $pid = fork;

    unless ( $pid ) {  # Child
        system("perl example.pl word.txt.$i");
        exit 0;
    }

    push @pids, $pid;
}

waitpid $_, 0 foreach @pids;
Zaid
  • 36,680
  • 16
  • 86
  • 155
3

Better fork from the enclosing Perl script and then call system in the child process without the trailing &. wait will be needed in the parent as well.

Because the argument of system is parsed by the system shell, you will encounter different behaviour from the Windows shell than from Bash, for example.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
0

It is a lot easier to use the START command (Windows Batch command) than to fork processes. The downside is that it will open multiple DOS windows.

system "start perl example.pl word.txt.$i";
mswanberg
  • 1,285
  • 8
  • 20