Processes, threads and communication
A process combines an executing program with its current state and resources. This chapter separates process creation, execution and communication so that program traces can be counted correctly. Thread and system-call questions usually test what is shared and what changes.
1.1 System calls and privileged execution
Past papers · CS 2014–2026 (23 papers): 2 questions, 2 marks
A system call requests a kernel service through a controlled entry point. A library function can compute entirely in user mode, invoke one system call, or invoke several. The name of a C function alone does not determine the number of kernel entries. For example, strlen can run entirely in user space; malloc may reuse an existing arena or request more memory from the kernel. Normal process termination through exit ultimately enters the kernel. Sleeping for a positive interval requires kernel timer/scheduling support in the usual UNIX model.
| Event or operation | Essential distinction |
|---|---|
| System call | Synchronous request caused by the executing program |
| Exception, such as a page fault | Synchronous event associated with an instruction |
| Device interrupt | Asynchronous notification from hardware |
| Mode switch | Changes execution privilege; may continue the same process |
| Context switch | Saves one execution context and restores another |
| Timer interrupt | Lets the OS regain control for preemptive scheduling |
The kernel validates arguments and access rights before performing a service. Updating memory-protection registers, configuring a device directly, and disabling interrupts require privilege. A user program can request I/O through a system call without gaining unrestricted device access.
1.2 Process state and saved context
Past papers · CS 2014–2026 (23 papers): 5 questions, 5 marks
A ready process can execute once it receives the CPU. A blocked process must first wait for an event. In the basic five-state model, ready cannot move directly to blocked: it must run before issuing a blocking operation. Event completion changes blocked to ready; it does not bypass scheduling and force immediate execution.
The ready and blocked paths distinguish CPU waiting from event waiting.
The process control block records identity, state, saved registers and program counter, scheduling data, memory-management references and I/O information. The program's code is passive; several processes may execute the same code with different data and saved contexts.
| Scheduler | Decision |
|---|---|
| Long-term | Which jobs enter the active population |
| Short-term | Which ready execution context gets the CPU |
| Medium-term | Which processes are suspended or restored |
An interrupt may return to the same process; neither a timer tick nor another process's I/O completion necessarily selects a different process. An actual blocking call or disk-backed page-in takes the caller out of running state.
Suspended-ready and suspended-blocked states add memory residency to this model. Completion of an event can make a suspended-blocked process suspended-ready without bringing it into memory.
1.3 Fork, exec and wait
Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks
After a successful fork, both parent and child continue after the call. The return value is zero in the child and the child's positive process ID in the parent; failure returns −1 to the caller and creates no child. Ordinary private variables have separate logical copies. Copy-on-write may initially share physical pages.
For conditional forks, draw one branch per return path and evaluate short-circuit expressions separately. Count each print at the program point where it is reached: a print after every fork round contributes the population at each round, whereas a print after the whole loop counts only processes reaching the loop exit. A wait orders parent progress; it does not undo a child's later forks. && evaluates its right operand only if the left operand is nonzero; || evaluates it only if the left operand is zero.
A successful exec replaces the program image while retaining process identity; it does not return to the old instruction stream. wait/waitpid can collect a child's termination status. A zombie has terminated but still has status awaiting collection. An orphan has lost its parent and may still be executing; it is reparented.
1.4 Threads and what they share
Past papers · CS 2014–2026 (23 papers): 6 questions, 7 marks
Threads within a process share its address space and process resources. Each thread needs its own execution state. A thread's stack is logically private, but another thread can access it through a valid shared pointer in the same address space.
| Item | Threads of one process |
|---|---|
| Code, global data, heap | Shared address space |
| Open file descriptors | Shared process descriptor table |
| Program counter, registers | Separate execution state |
| Stack, thread ID | Separate per thread |
| Process page table | Normally shared |
| User-to-kernel mapping | Consequence |
|---|---|
| Many-to-one | User threads use one kernel thread; no multicore parallelism among them; a blocking kernel call can block that kernel thread |
| One-to-one | Kernel schedules each thread; threads can run on separate cores |
| Many-to-many | User threads are multiplexed over multiple kernel threads |
User-thread creation or switching can avoid a kernel entry. Switching between threads of one address space need not change the page-table root. Switching processes may require that change; tagged TLB entries can reduce flushing.
1.5 IPC and ordering
Shared memory lets processes access a common region after setup; the processes must coordinate updates. Message passing transfers data through send/receive operations. A blocking receive waits for an appropriate message; a nonblocking receive can return without one. With zero buffering, a rendezvous requires sender and receiver to meet.
| Mechanism | Revision cue |
|---|---|
| Anonymous pipe | Byte stream; close unused ends after fork |
| Named pipe | A named communication endpoint for independently started processes |
| Shared memory | Data sharing does not supply mutual exclusion |
| Message queue/mailbox | Messages and naming/selection rules are part of the interface |
| Signal | Event notification; not a general bulk-data channel |
| Socket | IPC endpoint, local or networked; protocol details belong to Networks |
For a pipe, EOF occurs only after all write-end references are closed and buffered data has been consumed. Keeping an unused write end open can prevent a reader from observing EOF. A full pipe may block a writer; an empty pipe with a live writer may block a reader.
Last-minute recall
- Kernel entry and process switching are different events.
- A blocked process cannot be selected merely because the CPU is idle.
- Fork resumes both paths after the call; exec replaces one path's program.
- Count only forks actually reached by each process.
- Private variables and inherited file offsets have different sharing rules.
- Threads share an address space, not a single program counter.
- Close unused pipe endpoints before reasoning about termination.