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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.