Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
}
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.