LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-12-2011, 06:43 AM   #1
wch705
LQ Newbie
 
Registered: Jul 2010
Location: Australia
Distribution: Debian
Posts: 23

Rep: Reputation: 0
problem with fork()ing multing child process in C


i got basic knowledge about creating a single child from a parent using fork(). But when it comes into creating multiple children, i am simply stuck.
I am trying to create two processes from a parent and it would wait for both two processes to finish.
my attempt is as below
Code:
main()
{
	int pid,i;
	printf("let's start the process,and i am pid%d\n",getpid());
	for (i=0;i<=1;i++)
	{
		pid=fork();
		if ((pid>0))
		{
			wait(NULL);
			printf("i am the parent pid %d\n",getpid());
		}
		else if (pid==0)
		printf("i am the child pid %d\n",getpid());
	}
	return 0;
}
for some reasons, it came up with four processes.
the sample output:
Quote:
let's start the process,and i am pid2859
i am the child pid 2860
i am the child pid 2861
i am the parent pid 2860
i am the parent pid 2859
i am the child pid 2862
i am the parent pid 2859
could anyone point out the logic error?
thanks in advance.
 
Old 05-12-2011, 06:59 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Think about it... your child process continues within the for-loop. Since it appears that this is not your intent, then perhaps you should explicitly tell the child to exit.
 
Old 05-12-2011, 11:26 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your child processes are doing exactly the same thing as the parent processes... which is to unconditionally fork() a child. You need to add logic to the top level, to determine whether or not to fork().
--- rod.
 
Old 05-15-2011, 05:28 AM   #4
wch705
LQ Newbie
 
Registered: Jul 2010
Location: Australia
Distribution: Debian
Posts: 23

Original Poster
Rep: Reputation: 0
after some days, i finally understood, thanks all for help
 
  


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
fork(), variable values between child and parent process???? dearvivekkumar Programming 6 02-24-2010 05:18 AM
Need solution to "failed to fork PTY" and "error creating the child process" problems Gnusboy Linux - Newbie 4 01-02-2010 07:54 AM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 08:16 AM
Problem with child process karabaja4 Programming 4 07-29-2008 03:05 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM

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

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