Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processes 1

Status
Not open for further replies.

Corwinsw

Programmer
Sep 28, 2005
117
BG
I RTFMed. I think I udnerstood some things, but definitely couldn't catch the whole picture. So I continue trying to find good faq/manual/book, but by this time, could somebody try to explain me some things.

To be more clear:
1. What exactly fork do (it's mechanism, how it works)?
2.
open PIPE, "grep commit logfile | ";
while(<PIPE>) {
# Process records selected by "grep" here
}
As I understand that the grep and the while process works simultaniously. Am i right? How is this possible? (mean is only that needed?)
That's for now, i promise there will be more questions. :)

Corwin
 
Hello Corwin,

Let's split these questions up a bit.

Fork(), as I understand it, works in this way.

The fork() system call is implemented differently depending upon which operating system you're working on. That doesn't matter in the Perl script as it is used in the same way no matter where you're using it.

It looks as if you're on a Unixish system. On Unix and Unix-like systems it is implemented as a call to the operating system version of fork() and this works by making an almost exact copy of the current process (script) and launching that as well - both processes run from exactly the same point and look (almost) exactly the same. Here's a short example that demonstrates how to run a long job without the user having to wait for it to finish:
Code:
# Launch the batch delete job
if(fork()){
[tab]print "Batch Delete Job Launched\n";
} else {
[tab]BatchDeleteJob();
[tab]exit(0);
}
I said that the two processes were almost identical if you remember. You can think of the script (or process) running along in a straight line until it hits the fork() system call - at that point it splits into two. Like a fork in the road.

What your script needs to understand is which of the two processes it is - is it the the process that's been there all along (the parent process) or is the process that's just been created? (the child process)

The answer lies in the return value of the fork() system call.

In the parent process this returns the process number of the *child* process, so you can see what you've just launched.

In the child process it returns zero.

With me so far?

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Actually no.
So as I understand with if(fork()) you check if this is the the child or parent process and do different things in the different cases. Do both scripts go through that? And do we need to check everywhere which process is that to tell the parent and child process what to do?
Is this the way to control the flow of the child process?


Corwin
 
Yes - and the way you check if this is the parent or child is by checking the return code of fork()

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Isn't it too long, can't you start the child process in another file?
I read somewhere that the only difference between threads and processes is that processes don't share the data.
Does it mean that if:
if(fork()){
$var = 2;
} else {
$var = 1;
}
there will be 2 copies of $var?
Also if you call fork more than one time, what will be the id of the new process? And if you call into the flow of the child process what will happen?

Corwin
 
Yes it makes copy of var,but with new fork...
Pfu it becomes crazy, it makes child process for both father and child, and 2^^3 = 8 copies of the $var.
Why would somebody use sth like that?

Corwin
 
:) how short do you want the example to be?

I wasn't talking about threads in that example, just processes - I suggest that if you're first comfortable with manipulating processes it will be easier to understand threads later on.

In your example, assuming that fork is creating a copy of the entire process, changing the value of $var in the child process would *not* change the value of the variable with the same name in the parent process.

If you call fork() more than once from your parent process you will create a child process each time you call it - each child process will have a different process id (PID)

It's not possible to "call into the flow of the child process" from the parent process. If you find yourself wanting to do that you might want to re-think your script design.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I meant to call fork.

I don't understand that:

if (fork()) {
print "father: $w\n";
}
else {
print "child: $w\n";
}

I haven't called fork before, but it prints both child and father. Why?


Corwin
 
it *is* splitting into two processes - both of them are printing to the screen - this will prove it to you.


if (fork()) {
sleep 5;
print "father: $w\n";
}
else {
print "child: $w\n";
}

that one will print "child" before "father"

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
OK when I want to check if this is the child process without creating one more porcess what should I do?
I read somewhere that I should use $$ but this gives only the number of ther process and I think that it becomes too complicated.

Corwin
 
The process splits into two when it executes this line:
Code:
if(fork()){
You can tell if you're the child process because fork() returns zero (or false, if you like) to the child process.

Another way of writing the same code, which might make it more obvious is:

my $child_pid = fork();

if($child_pid != 0){
# I'm a daddy!
} else {
# I'm a child process
}


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Ahm, but they are actually different, because:
Code:
my $pid = fork();
if($pid){
   print "I'm a daddy!\n";
   } else {
   print "I'm a child \n";
}


if($pid){
   print "I'm a second daddy!\n";
   } else {
   print "I'm a second child \n";
}
produces only two processes when initalizing $pid.
So it prints once every possible time - 4 prints.

Code:
if(fork()){
   print "I'm a daddy!\n";
   } else {
   print "I'm a child \n";
}


if(fork()){
   print "I'm a second daddy!\n";
   } else {
   print "I'm a second child \n";
}

The second call of fork in the second if makes child process es for both the initial child and parent processes. So the output is:
I am child
I am daddy
I am second child
I am second child
I am second daddy
I am second daddy


Corwin
 
That's correct yes, the process splits in two at the first fork() and then both parent and child process execute the second fork().

When that happens both processes split into two, making four processes.

You seem to understand fork() - what's your question?

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Actually I am understanding that now, while talking wth u and trying.
So my next question:
Code:
#!/usr/bin/perl -w
use strict;

my $pid = fork();
if($pid){
   print "I'm a daddy!\n";
   } else {
   print "I'm a child \n";
}
my $pid2 = fork();
if($pid){
   print "I'm a second daddy!\n";
   } else {
   print "I'm a second child \n";
}

Now there is no difference if I write $pid or $pid2 in my second if. How could I understand if it is the first child process ot the second child process?

Corwin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top