LinuxQuestions.org
Review your favorite Linux distribution.
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 10-10-2012, 02:42 PM   #1
elico
Member
 
Registered: Dec 2011
Posts: 145

Rep: Reputation: Disabled
FORK() hard to understand


The fork() function is used to create a new process by duplicating the existing process from which it is called.

for example

int main(void)
{
pid_t childPID;
int var_lcl = 0;

statement1 ;
statement2 ;
statement3 ;


childPID = fork();

if(childPID >= 0) // fork was successful
{
if(childPID == 0) // child process
{

do child statements

}
else //Parent process
{
do parent statements
}
}
else // fork failed
{


}

My question is will statements 1 to 3
be executed in child without mentioning this
just because they apear in the calling process ?

Elico
 
Old 10-10-2012, 05:31 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,515

Rep: Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545
Execution in the child continues with the statement following the fork() call, just as does the parent. The child process does not restart from the beginning.
 
Old 10-11-2012, 04:01 AM   #3
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
Thanks
Can you show me a simple example please ?

Elico
 
Old 10-11-2012, 04:23 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,570

Rep: Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811
what kind of example do you need?
Code:
childPID = fork();

if(childPID >= 0) // fork was successful
{
     if(childPID == 0) // child process
    {
# this is the code executed only in the child process

    do child statements

    }
    else //Parent process
    {
# this is the code executed only in the parent process

    do parent statements
    }

}
else // fork failed
{

# something went wrong
}
you can also look for tutorials http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
 
Old 10-11-2012, 04:58 AM   #5
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 321

Rep: Reputation: 80
Initially there is one process, that executes statement1, statement2, statement3. When it comes to fork() the kernel make a copy of the process, and you have the original continuing the execution with fork returning the pid of the child, the child instead get a 0 from fork.

Last edited by Celyr; 10-11-2012 at 05:00 AM.
 
1 members found this post helpful.
Old 10-11-2012, 05:14 AM   #6
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
Thanks

Now in what cases the FORK() is used ?
Also can the child send a message to the father ?

Elico
 
Old 10-11-2012, 05:25 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
1. fork() (note lowercase) is used for multi-processing, as you are creating multiple processes from one program.
Typically it's used to parallel process a problem eg Apache has several different methods of working, one of which is fork()'ing.

2. Various methods can be used to send a msg back to the parent from the child, eg shared memory, temp files, sockets of various types (tcp, udp, Unix)
However, these days, if you need to do a lot of data sharing back and forth, most people would use threads instead.
This is easier, as you can declare a variable to be thread-global.
 
1 members found this post helpful.
Old 10-11-2012, 05:48 AM   #8
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
Thanks
Elico
 
Old 10-11-2012, 08:13 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,515

Rep: Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545Reputation: 1545
Quote:
Originally Posted by elico View Post
Now in what cases the FORK() is used ?
The fork() system call is the only** means of creating a new process. For example, when you tell your shell to run some program, your shell first fork()s itself, then the child shell process sets up the environment and does an exec() of the target program. The exec() system call replaces the current process image with a new process image, preserving only the argument list, environment list, and open file descriptors. Yes, that sounds like an overly complex way to execute a program, but it is quite powerful and a lot of work by some very clever people has gone into making it efficient.

** The one exception is the init process (PID 1), which is the ultimate ancestor of every other process on the system.
 
Old 10-11-2012, 12:10 PM   #10
elico
Member
 
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
Thanks
Looks like a long way to grasp and master linux ..

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
Is SELinux hard to understand? jokar.mohsen Linux - Security 14 08-23-2012 12:16 PM
[SOLVED] Understand hard drive failure log gacanepa Linux - Hardware 4 08-01-2012 02:20 PM
[SOLVED] Very high SLAB usage, hard to understand nwrk Linux - Server 17 10-01-2011 10:04 PM
LXer: Understand: Fork Bombing Attack LXer Syndicated Linux News 0 09-01-2007 09:10 AM
Hard to understand how to install gui(mplayer) akihandyman Linux - Newbie 3 12-30-2003 02:11 PM

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

All times are GMT -5. The time now is 03:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration