Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can somebody explain fork clearly to me?

Reply
Thread Tools

Can somebody explain fork clearly to me?

 
 
Steve
Guest
Posts: n/a
 
      02-24-2010
As it clearly states on oreilly's site:
"The fork command actually creates a child process, and returns the
PID of the process to the parent, and a value of zero to the child"

My brain is having an implosion trying to understand code such as
this:
1. unless(fork)
2. if ($pid = fork)

that's like saying "unless 6124" where 6124 is a PID. or if 6124, do
this, else to this.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-24-2010
Steve <(E-Mail Removed)> wrote:
>As it clearly states on oreilly's site:
>"The fork command actually creates a child process, and returns the
>PID of the process to the parent, and a value of zero to the child"
>
>My brain is having an implosion trying to understand code such as
>this:
>1. unless(fork)
>2. if ($pid = fork)
>
>that's like saying "unless 6124" where 6124 is a PID. or if 6124, do
>this, else to this.


Has nothing to do with fork() but with boolean values in Perl or
actually with scalars. In Perl any scalar can be used in many different
capacities and has simultaneously at least a string, a numerical, and a
boolean value.
The boolean value of a number is false if and only if that number is 0.
Therefore ""unless 6124" is the same as "unless <true>". (*)

It is a convenient short-hand. If you prefer you can replace the "fork"
command with "fork != 0" and you will get exactly the same behaviour.

jue

*<true> indicating the true value, not the string '<true>', which on the
other hand is a true value itself.
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      02-24-2010
Steve wrote:
) As it clearly states on oreilly's site:
) "The fork command actually creates a child process, and returns the
) PID of the process to the parent, and a value of zero to the child"
)
) My brain is having an implosion trying to understand code such as
) this:
) 1. unless(fork)
) 2. if ($pid = fork)
)
) that's like saying "unless 6124" where 6124 is a PID. or if 6124, do
) this, else to this.

It's actually very simple.
As soon as fork() is executed, the following code is executed *twice*.
Once in the parent process and once in the child process.

At that point, the two processes are completely indistinguishable, except
for one thing: the return value of fork().

So, the if() that checks the value of the fork() call is actually making
sure that the parent process and the child process are then going in
different directions.

if (my $pid = fork) {
say "Hello, I am the parent process! The child process is $pid";
} else {
say "Hello, I am the child process!";
}

In this piece of code (barring strange exceptions or typos on my part),
*both* 'say' statements will be executed. One in the parent process,
one in the child process.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      02-24-2010
On Feb 24, 11:20*am, Steve <(E-Mail Removed)> wrote:
> As it clearly states on oreilly's site:
> "The fork command actually creates a child process, and returns the
> PID of the process to the parent, and a value of zero to the child"
>
> My brain is having an implosion trying to understand code such as
> this:
> 1. unless(fork)
> 2. if ($pid = fork)
>
> that's like saying "unless 6124" where 6124 is a PID. *or if 6124, do
> this, else to this.


There will be 2 processes as soon as the fork below occurs
at runtime with fork() returning the child process id (PID,
eg 6124, to the parent and 0 to the child itself.

unless ( fork ) { # or 'unless ($pid = fork)'
.....
} else { # child: fork returns zero
....
}

But, Perl's backend compiles the above to a separate fork
call followed by the branching logic. So, after the fork,
both parent and child exist and each can evaluate fork's
return and branch correctly.

--
Charles DeRykus
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can somebody explain to me the output of the following code? asdfghjk C Programming 17 08-19-2010 08:02 PM
can somebody explain? Syd MCDST 3 06-20-2006 07:35 PM
please explain clearly what actually happens after i click on COMPILE srikanth C Programming 5 02-28-2006 01:29 PM
Can explain the MAP function more clearly? Thanks yezi Perl Misc 19 09-30-2005 06:59 AM
Can somebody explain .... Mike Digital Photography 7 10-21-2004 07:13 AM



Advertisments