LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 11-27-2006, 11:54 PM   #1
rdtgr
LQ Newbie
 
Registered: Nov 2006
Posts: 1

Rep: Reputation: 0
Question how do you create two children using fork()


Hi! I'm new here and I'm not sure if I've phrased the question well...you see, I'm new to linux and we were asked to create two child processes...I can already create one and a granchild, but two...I'm having trouble figuring out if I'm doing the righ thing...so help is very much needed...thanks...
 
Old 11-28-2006, 07:24 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,008

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Consider what happens when you enter a command in the shell; the shell forks and exec's (executes) the command and waits for it to complete before you get the shell prompt back. If you want to create a child process in the shell, you do it by running in the background with the ampersand appended to the command and you get the shell prompt back immediately (the shell does not wait for the command to complete).

If you're writing a program in, say, C to do this, you're going to use the fork and exec routines that are part of the Standard C Library (which are what the shell uses). You'll want to find a good intermediate to advanced C reference for the details of how to do this (in addition to the manual pages for fork, pipe, wait, execve and some other library functions. An oldie but goodie reference and how-to is Kochan and Wood Topics in C Programming. Bear in mind that you can fork, exec, and wait or fork, exec and not wait (so a parent can exec a child, the child can exec a grandchild and so on -- it can rapidly get complicated). One use of the wait function causes the parent to wait for the child to die. Bear in mind that fork and exec creates a new process that is independent of the parent (so the parent can fork and exec a child then die itself and the child will continue to run -- that's what daemon programs do).

Although you didn't say so explicitly, you may need to look into the IPC library functions if you're trying to have more than one child process communicating with each other -- that's shared memory, semaphores and messages (IPC is Inter Process Control) and is what is used for processes to communicate with each other. I don't know of a current reference that delves into IPC (the one I use is quite old and out of print years ago) but you'll need to find one that's full of examples before you jump in.

Best of luck.
 
Old 11-28-2006, 09:32 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,174
Blog Entries: 4

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-28-2006, 01:10 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

As you know, you create a child by calling "fork()".

As you've probably guessed, you can create TWO children simply by calling "fork()" twice. The tricky part is keeping track of who's the parent and who's the child:
Code:
  int pid = fork ();
  if (pid > 0)
  {
    printf ("I'm the parent!  I just created child pid (%d)!\n", pid);
  }
  else if (pid == 0)
  {
    printf ("I'm the child!  My pid is (%d)\n", getpid ());
  }
  else if (pid < 0)
  {
    perror ("Ouch!  Unable to fork() child process!\n");
    exit (1);
  }
'Hope that helps .. PSM
 
Old 11-29-2006, 02:29 PM   #5
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 443

Rep: Reputation: 30
to further iterate paulsm4 point, you can slap a while or for loop around the code snippet he supplied. NOTE: remember to reap all children process (man signal, my favorite way) and don't use an infinite loop or you will fork bomb your computer and have to restart.

Code:
int child = 0;
/*create two child using the system call fork() */
for(child = 0; child < 2; child++) {
  int pid = fork ();
  if (pid > 0)
  {
    printf ("I'm the parent!  I just created child pid (%d)!\n", pid);
  }
  else if (pid == 0)
  {
    printf ("I'm the child!  My pid is (%d)\n", getpid ());
  }
  else if (pid < 0)
  {
    perror ("Ouch!  Unable to fork() child process!\n");
    exit (1);
  }
}
Let me know if you have any questions

djgerbavor3
 
Old 12-03-2006, 07:21 AM   #6
acker
Member
 
Registered: Apr 2004
Location: Timisoara, Romania
Distribution: Debian
Posts: 90

Rep: Reputation: 15
Quote:
Originally Posted by djgerbavore

Code:
  else if (pid == 0)
  {
    printf ("I'm the child!  My pid is (%d)\n", getpid ());
  }

Code:
  else if (pid == 0)
  {
    printf ("I'm the child!  My pid is (%d)\n", getpid ());
    exit(0);
  }
If you don't exit from the child you'll go through the for loop in the child too.

--ack
 
Old 12-03-2006, 10:18 AM   #7
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 443

Rep: Reputation: 30
Quote:
If you don't exit from the child you'll go through the for loop in the child too.
Good point! I didn't realize that, I usually called an exec function on a child process or passing it to a dispatch function that deals with the children.

Thanks,


djgerbavor3
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Any software for children around? lfys Linux - Software 4 11-06-2006 11:05 AM
To run many children in the same level using fork() ugp Programming 1 02-07-2006 10:27 PM
what happens if i create a thread and then fork the process? Thinking Programming 7 10-31-2005 02:22 PM
how do I make the children gone with the parents jpan Programming 6 09-19-2004 11:40 PM
How many children have been created... maniac01 Programming 8 03-11-2004 11:27 AM


All times are GMT -5. The time now is 08:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration