Operating System Fundamentals
Processes
A process is a program in execution and a unit of resource allocation. It includes instructions, data, and a process control block.
An orphan is a child whose parent exits first. It is adopted by init or an appropriate subreaper. A zombie is an exited child whose status has not been collected through a call such as wait() or waitpid(). It retains a process table entry and a PID. Enough zombies can exhaust the PID space. The parent should reap its children, or an adopter can reap them after the parent exits.
A daemon is a background process independent of a controlling terminal. It can perform periodic work.
Threads
Threads are execution paths within a process and units of scheduling. Multiple threads can share process resources while handling work such as network requests, events, and rendering concurrently.
Processes and threads
- Resources are allocated to a process and shared by its threads.
- Threads are independently schedulable.
- Creating, terminating, and switching processes generally has more overhead. A process switch may also require switching address spaces, whereas a switch between threads of one process can retain the address space.
- Threads can communicate through shared data. Separate processes use interprocess communication.
User-level threading handles management in user space, avoiding many kernel transitions. Kernel-level scheduling can run threads on different processors in parallel. Some systems map multiple user-level threads onto a pool of kernel threads.
Concurrency and parallelism
Concurrent tasks make progress during the same interval. A single core can interleave them. Parallel tasks run simultaneously and require multiple execution resources.
Time-sharing and real-time systems
Time-sharing divides CPU time into slices and rotates among jobs, improving interactive response and utilization. A real-time system must respond to external inputs within prescribed deadlines. Predictable timing is central to reliability and safety.
Process states
Common states are new, ready, running, terminated, and blocked. A ready process can run when given CPU time. A blocked process waits for a resource or event such as I/O. A running process can block on an external device or return to ready when its slice expires or a higher-priority task preempts it.
Scheduling algorithms
- First come, first served. Simple arrival ordering can make short or I/O-heavy jobs wait behind long ones.
- Shortest job first. Schedule by estimated execution time. A stream of short jobs can starve a long job.
- Shortest remaining time first. The preemptive form of shortest job first.
- Round robin. Run the first ready task for one slice, then move it to the back if it remains runnable. Short slices increase switching overhead. Long slices increase response time.
- Priority scheduling. Select based on priority. Aging can raise the priority of waiting tasks to mitigate starvation.
A preemptive scheduler can suspend a running process. Under nonpreemptive scheduling, the chosen task keeps the CPU until it blocks or finishes. A context switch saves the outgoing execution state and restores the incoming state.
Deadlock
Deadlock occurs when each member of a group waits for an event that only another member can cause. The four necessary conditions are mutual exclusion, holding resources while waiting for more, no forced preemption of held resources, and circular wait.
- Ignoring deadlock may be a deliberate trade-off where prevention is expensive and the event is rare.
- Prevention breaks at least one necessary condition.
- Avoidance tests requests or admissions to avoid unsafe allocations.
- Detection and recovery use information such as a resource-allocation graph to identify and resolve deadlocks.
Synchronization and IPC
Common concepts include critical sections, mutexes, semaphores, and monitors. A pipe is a kernel buffer that provides a one-way stream. A named pipe associates it with a pathname, allowing unrelated processes to communicate. Message queues hold discrete messages. Shared memory lets processes access the same region. Semaphores control access to shared resources, and sockets support communication across hosts.
Disk scheduling
First come, first served is simple and fair but can have long seeks. Shortest seek time first favors the nearest request, potentially starving distant ones. SCAN, the elevator algorithm, serves requests as the head moves in one direction before reversing.
Virtual memory and page replacement
Virtual memory provides a larger logical address space and can load only portions of a process at a time. Backing storage can help hold data that does not currently fit in RAM.
- Optimal. Evict the page whose next access is farthest in the future. This is a theoretical baseline.
- FIFO. Evict the oldest insertion. Frequently accessed pages can still be removed.
- LRU. Evict the least recently used page. An exact linked-list implementation pays for an update on each access.
- Clock. A circular scan clears set reference bits and gives the corresponding pages a second chance. A page with a clear bit can be selected for eviction.