Processes often need to work together: one produces data and another consumes it, or several share a database. They must be able to exchange information safely.

Cooperating Processes

Processes are independent if they cannot affect each other, and cooperating if they can. Cooperation allows information sharing, computation speed-up by dividing work, modularity, and convenience. Cooperating processes need inter-process communication (IPC).

Definition: Inter-process communication. The mechanisms provided by the operating system that allow processes to exchange data and coordinate their actions, chiefly shared memory and message passing.

Models of IPC

In shared memory, a region of memory is shared by the processes, which read and write it directly. It is fast, since the kernel is involved only to set it up, but the processes themselves must synchronise their access. In message passing, processes exchange messages through the kernel with send() and receive(). It is simpler and suits distributed systems, but is slower for large data. Messages may be sent directly to a named process or indirectly through a mailbox (port), and the send and receive may be blocking (synchronous) or non-blocking (asynchronous). UNIX provides pipes, which carry a stream of bytes from one process to another, named pipes (FIFOs), which can link unrelated processes, message queues, shared memory segments and sockets.

Race Conditions

A race condition occurs when several processes access and change shared data at the same time, and the final result depends on the order in which their instructions happen to run. Consider count++ executed by two processes. In machine terms, it is three steps: load count into a register, add 1, store it back. If both load the value 5 before either stores, both store 6, and one increment is lost. Race conditions are hard to find, since they appear only for certain timings.

The Critical Section

The part of a program in which a process accesses shared data is its critical section. To prevent race conditions, the system must ensure that no two processes are in their critical sections at the same time. This is called mutual exclusion.

Table 3.1: IPC models

Feature Shared memory Message passing
Speed Fast Slower
Kernel role Setup only Every message
Synchronisation By the processes Built in
Suits Large data on one machine Small data, networks

Example 3.1: A lost update

Problem: count = 5. Processes P and Q each execute count++ as load, add, store. Show an order that gives the wrong result.

Solution:

  1. P loads 5 into its register.
  2. Q loads 5 into its register.
  3. P adds 1 and stores 6.
  4. Q adds 1 and stores 6.

Result: count is 6 instead of 7. One increment was lost because of a race condition.

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(void) {
    int fd[2];
    char buf[40];
    pipe(fd);
    if (fork() == 0) {
        close(fd[0]);
        write(fd[1], "hello from child", 17);
        return 0;
    }
    close(fd[1]);
    read(fd[0], buf, sizeof buf);
    wait(NULL);
    printf("parent received: %s\n", buf);
    return 0;
}

Output

parent received: hello from child

Common mistakes

  • Thinking that a single statement such as count++ is always atomic.
  • Using shared memory without any synchronisation.
  • Assuming that a program without visible errors in tests has no race conditions.

Key takeaways

  • Cooperating processes use shared memory or message passing.
  • Pipes, FIFOs, message queues and sockets are UNIX IPC methods.
  • A race condition makes the result depend on timing.
  • The critical section must be executed with mutual exclusion.

Practice questions

  1. What is a race condition?
  2. Name two IPC mechanisms in UNIX.
  3. What is a critical section?

Answers to the practice questions

  1. A situation in which the result of concurrent access to shared data depends on the order of execution.
  2. Pipes and shared memory (also FIFOs, message queues, sockets).
  3. The part of a program in which a process accesses shared data.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit III