Unix processes run in protected memory spaces, this means that no process is able to access another process' memory. If you like two or more processes to share some data, you have to advice the operting system to create a memory space which all processes have access to. This "feature" is called shared memory (shared between the processes).
As mentioned before, the shared memory is handles by the operating system. This means you cannot allocate memory going the usual way using malloc and free, you have to use some system calls instead. Consider the shared memory as a resource like any of the other resouced controlled by the operating system. You have to reserve the resource in the beginning and you have to release the resource after you used it.
Do not worry, there are not many system calls you have to know. But there's another thing you might have to know. Multiple processes are running concurrently. Shared memory is not protected with a mutual exclusion. You have to make sure which process is accessing when the shared memory. In many cases there's no need for such a synchronisation, it depends on your application.
This program has two processes (parent and child), that access properties in a shared memory. To demonstrate that the memory is really shared, the child process will alter the data of the properties. Both processes display the content of the properties.
The number of header files to include is very minimalistic:
1 2 3
A few symbols we need later.
5 6 7
The following structure is not really needed for the handling of the shared memory. This is application specific and is used to show a possible use of shared memory.
The structure represents a property, a unique key and a value. In this case both are strings.
9 10 11 12 13
Some global variables. The variable
magic_key is to identify the shared memory. This is an arbitrary but unique number.
15 16 17
Both, parent and child process, are created within the function main. This is just to simplify the code. As you may see, we will use very few variables.
20 21 22 23 24
First we need to allocate a memory space, that is shared between the processes. This is done using the system call
shmget(2). Please read the man page to get more specific information.
Note: do not use
malloc and free to allocate shared memory, it won't work!
26 27 28 29
If the allocation of the shared memory failed, shmget will return
-1. In this case the program should stop.
30 31 32 33 34 35
Next, we'll need to fold the shared memory space into this process. Please read the man page of
shmat(2) for further information.
37 38 39 40 41 42 43 44
From now on, we're able to access the shared memory space as we do as normal memory space allocated using
malloc. For example: we could fill the shared memory with some data.
46 47 48
To prove that two different processes, running in different protected memory spaces, are able to access the shared memory we need to create a child process.
50 51 52 53 54
If the creation of the child process failed, we must clean up the shared memory, since it is handled by the operating system. This is done with the system call
55 56 57 58 59 60 61 62 63
The code of the child process contains nothing spectacular. It is a simple loop, in which the shared memory gets written every two seconds.
64 65 66 67 68 69 70 71 72 73 74
The code of the parent process is nothing special as well. Just read the shared memory every two seconds and print the content.
75 76 77 78 79 80 81
The parent process waits for the child process to terminate and then releases the allocated, shared memory.
83 84 85 86 87 88 89 90
That's it, let's get out of the program.
Compilation and execution is very easy since you don't need any special library.
To compile use the following command:
$ gcc -o process3 process3.c
To execute is just as easy as to compile:
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.