LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2012, 02:55 PM   #1
elico
Member
 
Registered: Dec 2011
Posts: 145

Rep: Reputation: Disabled
FORK question


hi all

What is the point to create a child process ?
why not create a new process ?

Elico
 
Old 05-06-2012, 03:09 PM   #2
zer0signal
Member
 
Registered: Oct 2010
Location: Cleveland
Distribution: Slackware, Fedora, RHEL (4,5), LFS 6.7, CentOS
Posts: 258

Rep: Reputation: 29
Lots of reasons, having the Parent PID as the controlling process.. IE: Daemon.. Server/Client model, PPID manages incoming connections, and creates a child process to manage that new connections, to free up the parent to allow new connections.
 
Old 05-06-2012, 03:29 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
But the main reason is that fork() is the only mechanism for spawning a new process.
 
1 members found this post helpful.
Old 05-06-2012, 03:33 PM   #4
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
But isnt the child code a copy of father code ?
So they are meant to do the same task , am I right ?

Elico
 
Old 05-06-2012, 09:08 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
That's right, but the most common sequence is for the child process to follow a different branch in the code, set up a few things (mainly closing and opening files, etc.), and then call exec(), which completely replaces the parent's image with something new. Aside from process number 1 (init), that's how every process in the system begins life.
 
Old 05-07-2012, 02:22 AM   #6
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
Thanks

Can any one show a code example for generating two or more independent processes w/o children
and another code sample for a father process and a child process .

By the way can a father process have two or more children ?

Thanks
Elico
 
Old 05-07-2012, 06:59 AM   #7
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
What is the point to create a child process ?
checkout this discussion, http://www.linuxforums.org/forum/pro...d-process.html

Quote:
can a father process have two or more children ?
Yes, father process can create two or more child processes.
 
Old 05-07-2012, 07:06 AM   #8
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
code sample for a father process and a child process
You can checkout on this link - http://stackoverflow.com/questions/7...rent-processes.
 
Old 05-07-2012, 08:33 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by elico View Post
Can any one show a code example for generating two or more independent processes w/o children
and another code sample for a father process and a child process .
Sure. Here is very rudimentary code to create an independent process by breaking the inheritance chain:
Code:
pid1 = fork();
if(pid1)  waitpid(pid1, NULL, 0);    /* Parent waits for 1st child */
else {        /* Child process continues here */
        if(fork())  exit(0);         /* 1st child forks again and exits */
        /* The orphaned 2nd child process continues here */
             .
             .
             .
}
That orphaned child process gets adopted by the init process (PID 1). Note that the waitpid() call in the original parent is important to keep that short-lived 1st child from hanging around as a zombie. Note: Besides error checking, this code also omits a lot of other things you would want to do to create a truly independent process (setpgrp(), setting up the standard file descriptors, breaking the association with the controlling tty, ...).

The usual parent<->child arrangement is simpler. Just don't do the second fork(), and the parent has the option of (a) waiting for the child immediately, (b) continuing processing and doing the wait() later, or (c) continuing processing and exiting without doing a wait(). For case c, the child process then gets adopted by init, just as in the first example.
Code:
pid1 = fork();
if(pid1 == 0) {      /* Child process continues here */
     .
     .
     .
}

Last edited by rknichols; 05-07-2012 at 08:40 AM. Reason: added note
 
Old 05-07-2012, 01:30 PM   #10
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
Thanks

Elico
 
  


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
[SOLVED] Question : Developing/Making FreeBSD fork/derivative. lcxpics *BSD 2 04-12-2011 05:28 PM
Question about fork htmin Programming 5 01-21-2011 05:09 AM
fork question djgerbavore Programming 2 04-20-2006 02:27 AM
question about fork() hubabuba Programming 2 03-31-2006 10:00 AM
fork() question elmafiacs Programming 3 01-08-2005 12:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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