Need explanation on Perl Fork


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need explanation on Perl Fork
# 1  
Old 11-02-2012
Need explanation on Perl Fork

Hi,

I am breaking my head to understand the below line of code in perl:
Code:
next if $pid = fork;    # Parent goes to next server.
    die "fork failed: $!" unless defined $pid;

Can anyone please explain me in detail as I am a slow learner?

Thanks,
Ali.

Last edited by Franklin52; 03-28-2013 at 04:20 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-02-2012
If fork succeeds, it returns the process ID of the child process in the variable $pid (of the parent process).
So, the parent will have some (non-zero) value in $pid and the child process will have a 0 value in $pid. And the next statements will be attempted to be executed by both the parent and the child processes.

If fork fails, $pid will be undef.

So, the first statement says "if forking succeeds, go to the next loop block iteration (applicable for the parent process only as only the parent has a non-zero value in $pid)".

So, the next statement (die "fork failed: $!" unless defined $pid;) is executed only by:

1) the parent process, if forking failed in the first statement, or
2) the child process, if forking is successful.

In 1), the parent process will die since $pid will be undef.
In 2), the child process will not die (that is, it will continue with the next set of statements) as for the child, $pid will be 0 (which is a defined value).

Last edited by elixir_sinari; 11-02-2012 at 12:18 PM..
# 3  
Old 03-27-2013
Elixir i have question too ... by wat u explained ..suppose if i want to run a date command on ten remote nodes i should place my command here ?


Code:
#!/usr/bin/perl
use strict;
use warnings;
print "Starting main program\n";
my @childs;
for ( my $count = 1; $count <= 10; $count++) {
        my $pid = fork();
        if ($pid) {
        # parent
        #print "pid is $pid, parent $$\n";
        push(@childs, $pid);
        } elsif ($pid == 0) 
            {
                 */ Does the remote command go here /*

                exit 0;
             } 
           else {
                die "couldnt fork: $!\n";
        }

}
 
foreach (@childs) {
        my $tmp = waitpid($_, 0);
         print "done with pid $tmp\n";
 }
 
print "End of main program\n";


Last edited by Corona688; 03-27-2013 at 12:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl scripts: requesting for logic explanation

Hi, balajesuri and durden_tyler, I have found your perl script for the thread https://www.unix.com/shell-programming-scripting/176370-perl-script-help-me-extracting-string.html, but find it difficult to understand the syntax. Could you or any forum members kindly shed some light on the logic... (3 Replies)
Discussion started by: royalibrahim
3 Replies

2. Shell Programming and Scripting

fork in perl

Hi, I have a file which has some 50 hosts, I want to login to each host using ssh and execute any command. I am using fork function in perl. I am able to login to each host and execute the command in the hosts paralelly, but it spawing/forking 50 processes at a time. It will consume the cpu... (5 Replies)
Discussion started by: Anjan1
5 Replies

3. Shell Programming and Scripting

fork in perl

Can someone tell me perl fork example please. I tried online but could not get proper documentation. (2 Replies)
Discussion started by: Anjan1
2 Replies

4. UNIX for Dummies Questions & Answers

In need of explanation

Its great someone provided this script that strips out a filename and extension but can someone explain how each line works? file1='Jane Mid Doe.txt' newfile='Jane.txt' 1) ext=${file1##*.} 2) filename=${file%%.???} 3) set -- $filename 4) newfile="1.$extension" (1 Reply)
Discussion started by: Lillyt
1 Replies

5. Homework & Coursework Questions

Please help with the following fork code..with complete explanation

I have the following piece of codes. Please explain it to me in great detail how are these codes working. 1. #include <stdio.h> int main(){ int x; x=0; while (x<2 && fork()){ if (!fork()) execlp("echo","x++","x",0); x++; system("echo x+x"); } } 2. #include <stdio.h> int i;... (1 Reply)
Discussion started by: prakashabii
1 Replies

6. Shell Programming and Scripting

Need explanation on Anonymous hash in PERL!!

Hi, I have doubt regarding the Anonymous hash. For creating a object we are generally using Anonymous hash. My Doubt is: Why we are only using Anonymous hash?? Instead of Anonymous hash can we use global hash variable and take its reference for creating an object using same bless function??... (0 Replies)
Discussion started by: jatanig
0 Replies

7. Shell Programming and Scripting

Perl fork

Hi, I want to exec three different functions in perl one per fork(); How can I determine that this it the third fork and I should use third function in it. if ($pid = 0) { first();} else ( #parent second(); ) How to run third function? (1 Reply)
Discussion started by: mirusnet
1 Replies

8. UNIX and Linux Applications

need explanation

Hi am having a c pgm. It has the include files (unistd.h,sys/types.h,win.h,scr.h,curses.h,stdarg.h and color.h). I don't know the purpose of these include files. will u plz explain me. (1 Reply)
Discussion started by: Mari.kb
1 Replies

9. Shell Programming and Scripting

tr explanation please

how does below tr command replace nonletters with newlines? I think I understand tr -cs '\n' part.. but what is A-Za-z\' <--- what is this?? tr -cs A-Za-z\' '\n' | -c --complement -s, --squeeze-repeats replace each input sequence of a repeated character that is... (0 Replies)
Discussion started by: convenientstore
0 Replies

10. Shell Programming and Scripting

new to perl.. how do I do this ? fork/ threads ?

I have never coded in perl before (just started today morning :). I need to write a perl program to automate a task. Here is how I do it manually: Start a program in my home dir. Now if I want to execute another program while this one is still running, what I would do is go to another... (6 Replies)
Discussion started by: the_learner
6 Replies
Login or Register to Ask a Question