mario::konrad
programming / C++ / sailing / nerd stuff
Multiprocessing: Tutorial 01
© 2002 / Mario Konrad

What is a Process?

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

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.

Walkthrough

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.

#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>

This very first program is so simple, we just need one function: main:

int main(int argc, char ** argv)
{

One local variable is needed. It’s the one, which holds the process ID of the new created one, the child process.

    pid_t pid;

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.

    pid = fork();

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.

    if (pid < 0)
    {
            fprintf(stderr, "%s: error during fork\n", argv[0]);
            return -1;
    }
    else if (pid == 0)
    {
            printf("CHILD : hello\n");
    }
    else
    {
            printf("PARENT: HELLO\n");
            waitpid(pid, 0, 0);
    }

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).

    return 0;
}

Compile and Execute

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:

$ ./process1

Excercises

Try to experiment with process creation. Here are some suggestions:

Download

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.