LinuxQuestions.org
Visit Jeremy's Blog.
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 12-07-2009, 02:18 PM   #1
mitsulas
LQ Newbie
 
Registered: May 2006
Distribution: Debian
Posts: 21

Rep: Reputation: 0
Under which circumstances a child process creates another child process using fork?


Hello,

Consider the following code:
Code:
int main()
{
	int i=0;
	pid_t pid;
	for(i=0;i<2;i++)
	{
		pid=fork();
		if(pid==0){
			printf("This is child %i i=%i\n",getpid(),i);
		}
		else{
			printf("Parent: chid_pid=%i i=%i parent's pid=%i\n",pid,i,getpid());

		}

	}
	
	exit(0);
}
I get the following output:

Parent: chid_pid=4356 i=0 parent's pid=4355
This is child 4356 i=0
This is child 4357 i=1
This is child 4358 i=1
Parent: chid_pid=4357 i=1 parent's pid=4355
Parent: chid_pid=4358 i=1 parent's pid=4356



As I can observe instead of two children(as I expect) processes there are three. This is because child process 4356 creates its own child. Under which circumstances this happens? Why all the messages of the type
"This is child X i=Y" are concentrated one under another? How exactly fork works? Is affected by the fact that I have a dual-core processor?

Thanks in advance
 
Old 12-07-2009, 02:41 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by mitsulas View Post
As I can observe instead of two children(as I expect) processes there are three.
Why would you expect two?

Quote:
This is because child process 4356 creates its own child.
You even understand why there are three.

Quote:
Under which circumstances this happens?
Everything except the return value of fork() is duplicated between the parent and child, so after the fork() the child will do exactly the same things the parent does except where those things depend on the return value of fork().

You did not make continuing through the loop until i is 2 depend on that return value. (see alternate code below)

Quote:
Why all the messages of the type
"This is child X i=Y" are concentrated one under another?
The processes happened to execute in that sequence.

Quote:
How exactly fork works?
Too general a question to answer.

Quote:
Is affected by the fact that I have a dual-core processor?
The sequence in which the processes execute is probably affected by the fact that you have a dual-core processor.

If you wanted just the two child processes you say you expected, notice where I added break to your code in the version below. That means the child does not stay in the loop so it does not create its own child.

Code:
int main()
{
	int i=0;
	pid_t pid;
	for(i=0;i<2;i++)
	{
		pid=fork();
		if(pid==0){
			printf("This is child %i i=%i\n",getpid(),i);
                        break;
		}
		else{
			printf("Parent: chid_pid=%i i=%i parent's pid=%i\n",pid,i,getpid());

		}

	}
	
	exit(0);
}

Last edited by johnsfine; 12-07-2009 at 03:26 PM.
 
Old 12-08-2009, 02:37 AM   #3
mitsulas
LQ Newbie
 
Registered: May 2006
Distribution: Debian
Posts: 21

Original Poster
Rep: Reputation: 0
Thank you for you answer.

Quote:
Originally Posted by johnsfine View Post
Everything except the return value of fork() is duplicated between the parent and child, so after the fork() the child will do exactly the same things the parent does except where those things depend on the return value of fork().
Why the other child process doesn't make its own child?
 
Old 12-08-2009, 08:16 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by mitsulas View Post
Why the other child process doesn't make its own child?

Everything except the return value of fork() is duplicated


Are you ignoring the fact that the variable i in your program is part of that "everything"? Or are you ignoring the way the value of i affects the subsequent behavior?

As you coded the program, when you create a child in which i is 0, each of the child and the parent will create another child (and in both those children, i will be 1). When you create a child in which i is 1, neither the parent nor the child will create another child.

As you coded it, whether a process goes on to create a child does not depend on whether that process is itself the original or a child. It depends only on the value of i.
 
  


Reply

Tags
fork



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
Difference between Child THREAD and Child PROCESS whho Linux - Newbie 12 02-16-2015 12:22 AM
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM

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

All times are GMT -5. The time now is 12:22 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