A process is a program, or a part of a program, that runs independent from other processes, in a separate memory space. A process is also able to communicate with other processes using IPC, inter process communication that you will learn about in further tutorials.
The independance of processes is a very important feature. The termination of one process has (usually) no affect to other processes, they stay up and running. This is a very robust and powerful concept.
Because of the separation of the memory spaces, the communication between processes seem to get complicated on the first look. On a second, deeper look the IPC is a very powerful tool, but it highly depends on the underlying operation system. There are several communication channels which can be used, however this is not the topic of this tutorial. If you are interested in more information about IPC, please proceed to further tutorials.
This tutorial deals with the very basics of multi processing:
The program we're going to have a look at, shows how to create a new process, a so called child process. The program simply creates the process and each process prints out a simple message. So far nothing spectacular. The really interesting part is, that you don't have to do much to reach this goal. See for yourself.
First, the inclusion of some header files. stdio.h does not need any further explanation.
sys/wait.h is needed for synchronization of the two processes.
1 2 3
This very first program is so simple, we just need one function: main:
One local variable is needed. It's the one, which holds the process ID of the new created one, the child process.
Now, here it comes: the well known, sometimes feared, sometime confusion generating,
fork statement. fork copies the entire context of the process and then lets the child process run. Whether the parent process is next or the newly created child process depends on the process scheduler of the operating system. For further information, please read the
fork man pages.
fork returns a value, which you can use to determine what happend. If the value is below 0 an error has been occurred and no new process has been created. If the value is exactly 0, the code is executed as child process. If the value is above 0, the code is executed as parent process.
9 10 11 12 13 14 15 16 17 18 19 20 21 22
The only thing, what's left and worth an explanation is the statement
waitpid. This statement lets the parent process wait for the child process to terminate. The termination of the child process is either normally (normal termination because of an end of the child program) or abnormally (termination caused by a segmentation fault or any other error).
The last part is just quitting the program (either the parent or the child process).
Compilation and execution is very easy since you don't need any special library.
To compile use the following command:
$ gcc -o process1 process1.c
To execute is just as easy as to compile:
Try to experiment with process creation. Here are some suggestions:
All source code files provided by this page is free to copy, modify, redistribute and use for any purpose. The tutorial is copyrighted by Mario Konrad.