2 Processes and Interprocess Communication
Learn how operating systems create, schedule, and terminate processes, and how processes exchange information through message passing or shared memory.
Programs in execution
A is a program in execution, rather than passive code stored on a device. The operating system tracks information such as its identifier (PID), current instruction and CPU context, memory, and open resources. Managing processes lets the operating system handle and isolate multiple executing programs.
states and scheduling
A moves among states as it runs. In a common five-state model, New means it is being created; Ready means it can run but is waiting for CPU time; Running means it is executing on a CPU; Blocked or waiting means it cannot continue until an event occurs; and Terminated means it has finished or been stopped.
The usual progression is from New to Ready, then Running, and eventually Terminated. A running may be preempted and return to Ready when its time slice ends. If it must wait for input or another event, it becomes Blocked; when the event occurs, it becomes Ready again. The scheduler selects a ready to run. This is a general model: operating systems may use additional states or different names.
Creating and running processes
Operating systems provide mechanisms to create processes, though their exact interfaces vary. In a common Unix-like pattern, fork() creates a . The parent and child continue from the point of the call, and the return value tells each which it is. The child has a distinct PID; on Linux, its memory is initially implemented using copy-on-write rather than immediately copying every memory page.
The child may then call an exec function to replace its current program image with a different program. The itself continues to exist, but executes the new program. Some operating systems instead provide a single spawn-style operation that combines creation and program execution.
For example, a command-line shell can create a for a command and have that child execute the command. The shell can keep running to accept the next command.
Termination and cleanup
A terminates when it finishes normally, encounters an error, or is stopped by an external event such as a termination . On Unix-like systems, a can report an exit status, which its parent can collect with wait() or waitpid(). Waiting also lets the operating system release the remaining bookkeeping for a terminated child.
If a child has terminated but its parent has not collected its status, it may temporarily remain as a . It is no longer executing, but a small amount of information is retained for the parent.
Why processes communicate
Processes are normally isolated: one cannot simply read or change another ’s private memory. provides controlled ways for processes to exchange data or coordinate activity. Examples include connecting one program’s output to another program, and exchanging requests and responses between a client and server.
Two broad IPC approaches are and . A design using either approach must consider who may access the communication channel, how messages or data are organized, and how participants know when data is ready.
Message-passing channels
With , processes send and receive data through an operating-system-managed channel. The channel may carry a stream of bytes, distinct messages, or communication between processes on the same machine or across a network.
A is a byte stream with a read end and a write end, often used between related processes. In a shell pipeline such as
producer | consumer, the output of one is connected to the input of another.A named , also called a FIFO, can be found and opened through a filesystem name. The communicating processes therefore need not have been created as parent and child.
A message queue carries discrete messages; some queue systems support message priorities.
A socket supports communication between local processes or across a network. A Unix-domain socket is designed for communication between processes on the same machine.
Some IPC mechanisms primarily provide notification rather than transferring large amounts of data. A can notify a that an event occurred; processes generally need another mechanism to exchange substantial data.
and synchronization
With , processes map the same memory region into their address spaces and exchange data by reading and writing that region. This approach can be efficient for large or frequent exchanges because data do not need to pass through a message channel each time.
Processes must coordinate access to to avoid conflicting updates and inconsistent data. For example, they can use semaphores or other synchronization tools.
Choosing an IPC approach
provides a clear communication channel and can be convenient when processes exchange distinct messages. can be efficient for large data transfers, but requires careful synchronization. In either case, the design must address access to the channel, the organization of messages or data, and how participants know when data is ready.