user1065000
user1065000

Reputation: 85

Cannot Run Fork, unwanted number of processes are forked

I am having an issue with Fork in Perl. I want to execute 10 Fork Processes at a go from one single script all 10 Child (Forked) processes will do the same thing (Copy files from one place to another).

When I execute this code, my OS Hangs and when I actually check there are hell lot of processes which are forked at a time.

Here is my Code:

while ($callCount <= $totalCalls) {
 for (1..$TotalProcessToFork) {
         print "Call -> $callCount";
         if($pid = fork) {
             #in Parent Process
             print " :: PID -> $pid\n";
             push(@list_of_pid, $pid);
         } else {
             #in Child Process
             `touch $callCount`;
         }
         $callCount++;
     }
 }

Now when I execute this code, there are around 1000 child processed which are executed.

Can any one tell me what wrong I am doing here.

Upvotes: 0

Views: 209

Answers (4)

U. Windl
U. Windl

Reputation: 4325

Independent of Perl: Learn how fork works!

In your loop the first process being forked continues to execute the loop `` $TotalProcessToFork -1times, the second process being forked by the original parent will execute the loop$TotalProcessToFork -2` times, and so on...

In addition the first child forked by the first child will also execute the loop $TotalProcessToFork -3 times.

Maybe try this code:

#!/usr/bin/perl
use warnings;
use strict;

my $TotalProcessToFork = 3;

for (1..$TotalProcessToFork) {
    print "PID $$ processes $_\n";
    if ((my $pid = fork) > 0) {
        print "parent $$ processing $_ created PID $pid\n";
    } else {
        print "child $$ processes $_\n";
    }
}

When I ran it, I got this output:

PID 13415 processes 1
parent 13415 processing 1 created PID 13416
PID 13415 processes 2
child 13416 processes 1
parent 13415 processing 2 created PID 13417
PID 13415 processes 3
PID 13416 processes 2
parent 13415 processing 3 created PID 13418
parent 13416 processing 2 created PID 13419
PID 13416 processes 3
PID 13417 processes 3
parent 13416 processing 3 created PID 13420
parent 13417 processing 3 created PID 13421
child 13420 processes 3
child 13418 processes 3
child 13421 processes 3
child 13419 processes 2
PID 13419 processes 3
child 13422 processes 3
parent 13419 processing 3 created PID 13422

Upvotes: 0

snoofkin
snoofkin

Reputation: 8895

You may want to take a look at Parallel::ForkManager, which will probably make your life easier.

Also, don't use external Linux touch command; it's better to use File::Touch.

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30638

This happens because when you fork a process, it creates two processes. Lets call them a1 and a2. Now a1 is the parent and a2 is the child, so when a2 is executed, it creates b1 and b2. When these all are executed, they also create new processes recursively.

Upvotes: 1

tripleee
tripleee

Reputation: 189317

The children fork, too. You need to exit the loop one way or another in the child case. A common pattern is to fork and exec, or you could just say last.

Upvotes: 4

Related Questions