LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   FORK() hard to understand (https://www.linuxquestions.org/questions/linux-newbie-8/fork-hard-to-understand-4175431535/)

elico 10-10-2012 02:42 PM

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

rknichols 10-10-2012 05:31 PM

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.

elico 10-11-2012 04:01 AM

Thanks
Can you show me a simple example please ?

Elico

pan64 10-11-2012 04:23 AM

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

Celyr 10-11-2012 04:58 AM

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.

elico 10-11-2012 05:14 AM

Thanks

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

Elico

chrism01 10-11-2012 05:25 AM

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.

elico 10-11-2012 05:48 AM

Thanks
Elico

rknichols 10-11-2012 08:13 AM

Quote:

Originally Posted by elico (Post 4802843)
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.

elico 10-11-2012 12:10 PM

Thanks
Looks like a long way to grasp and master linux ..

Elico


All times are GMT -5. The time now is 06:29 PM.