Can somebody explain fork clearly to me?

S

Steve

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.
 
J

Jürgen Exner

Steve said:
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.
 
W

Willem

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
 
C

C.DeRykus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top