LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2012, 11:24 AM   #1
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Rep: Reputation: 20
Perl


hi everyone,
consider this code:
Quote:
die 'Fork Failed' unless defined(my $child=fork());
exit 0 if $child;
what does this one mean?
exit 0 if $child;
thanks in advance
 
Old 05-15-2012, 11:42 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
It means "exit with an exit code of 0 if $child is defined."

The previous line attempts to fork, and $child will be set in the parent process if the fork succeeds.
 
Old 05-15-2012, 11:53 AM   #3
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
thanks for the answer,
Quote:
$child will be set in the parent process
it means $child is equal to parent?

Last edited by z99; 05-15-2012 at 12:04 PM.
 
Old 05-15-2012, 12:08 PM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
Quote:
Originally Posted by z99 View Post
thanks for the answer,

it means $child is equal to parent?
No. It means that the program forks a child process, and the $child variable will only be defined in the parent process, containing the PID of the child.
 
1 members found this post helpful.
Old 05-15-2012, 12:22 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
To be more precise fork has a really strange "magic": from that point there will be two processes (or threads) instead of only one - yes, the second process is the forked one.
In the main process the fork will return with the pid of child and in the child process the fork will return with 0. See the man page here

About the line: die 'Fork Failed' unless defined(my $child=fork());
if fork was not successful (that is $child is not defined) we will die and print a message: Fork Failed
About the line: exit 0 if $child
if $child will be true only if fork was successful and we are now in the parent process. The line means the parent process will be terminated immediately after creating the child.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
install vim via yum thinks perl is required - I build perl 5.14.2 from source rubanek Linux - General 4 05-02-2012 06:42 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 06:41 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration