Part 3 of 7 · 2 min read
Parent and child
In plain words: A private mapping is copied on write after fork (page 6 of Level 6), and a shared mapping is not copied. So a parent can create a shared region, fork, and talk to the child through it. The test program below keeps three counters in the region. Parent and child each add 1 to every counter 1,000,000 times:
struct shared { long plain; atomic_long atom; long locked; pthread_mutex_t m; };
struct shared *s = mmap(NULL, sizeof *s, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
pthread_mutexattr_t at; pthread_mutexattr_init(&at);
pthread_mutexattr_setpshared(&at, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&s->m, &at);
pid_t pid = fork();
for (int i = 0; i < N; i++) {
s->plain++; /* no protection */
atomic_fetch_add(&s->atom, 1); /* atomic */
pthread_mutex_lock(&s->m); s->locked++; pthread_mutex_unlock(&s->m);
}
The expected total is 2,000,000 for each counter. Three runs gave:
| Counter | Run 1 | Run 2 | Run 3 |
|---|---|---|---|
plain ++ |
1,961,569 | 1,960,371 | 1,955,475 |
| atomic add | 2,000,000 | 2,000,000 | 2,000,000 |
| mutex | 2,000,000 | 2,000,000 | 2,000,000 |
The plain counter lost between 38,000 and 45,000 updates in each run, which is the lost-update race of Level 4, now between processes and not between threads. The cause is the same: ++ is a read, an add and a write, and the two processes interleave them. The fix is also the same, with one new detail. An ordinary mutex lives in the memory of one process and means nothing to another. A mutex in shared memory must be marked process-shared with pthread_mutexattr_setpshared(..., PTHREAD_PROCESS_SHARED), as in the code, and the mutex object itself must be inside the shared region. An atomic operation needs no such setting, since the hardware makes it atomic on the shared frame.
Process-shared synchronisation
Process-shared synchronisation means a lock, semaphore or condition variable that is stored in shared memory and marked so that processes, and not only threads of one process, can use it to coordinate their access to the shared data.
Example: the mutex in the test is set to PTHREAD_PROCESS_SHARED and placed in the shared region, so parent and child can lock the same mutex.
Common mistake
Putting a normal mutex in shared memory, or a mutex in ordinary memory next to shared data, does not protect anything across processes. The lock must be in the shared region and be process-shared.
Check yourself:
1. Why does a shared mapping made before fork stay shared in the child?
2. Why did the plain counter lose updates?
3. What two things must be true of a mutex that two processes use?