Operating Systems: Processes, Memory, Scheduling and Deadlocks
An operating system (OS) is system software that manages hardware resources, provides controlled services to programs, and helps users and applications interact with a computer safely and efficiently.
Operating systems are used in desktops, servers, mobile devices, embedded systems, cloud infrastructure, and real-time control systems.
1. What Is an Operating System?
An operating system coordinates the use of hardware such as the processor, memory, storage, displays, keyboards, and network devices. It also provides services that applications use, including file access, memory allocation, process creation, networking, and security checks.
The OS acts as both a resource manager and an abstraction layer. For example, an application works with a file name instead of directly controlling storage blocks on a disk.
| Function | What the OS does |
|---|---|
| Process and thread management | Creates, schedules, pauses, resumes, and terminates executing programs. |
| Memory management | Allocates memory, protects address spaces, and supports virtual memory. |
| File and storage management | Organizes files, directories, permissions, storage blocks, and caching. |
| Device and I/O management | Coordinates hardware through device drivers, buffering, interrupts, and I/O services. |
| Security and protection | Controls authentication, permissions, isolation, and access to protected resources. |
| Networking | Provides network communication services and manages network interfaces. |
| User interface | May provide a command-line interface, graphical interface, or both. |
2. Types of Operating Systems
Operating-system categories can overlap. For example, one OS may be multitasking, multi-user, networked, and capable of real-time tasks in different environments.
| Type | Main characteristic | Typical context |
|---|---|---|
| Batch OS | Runs groups of jobs with little or no direct interaction during execution. | Large recurring workloads and historical mainframe processing |
| Time-sharing OS | Shares processor time among users or programs to provide responsive interaction. | Multi-user systems and interactive terminals |
| Multitasking OS | Runs multiple processes by switching the CPU among them. | Desktop and server systems |
| Network OS | Provides services and resource sharing across connected but separately managed computers. | Servers, client-server networks, and enterprise environments |
| Distributed OS | Attempts to coordinate multiple computers so that they appear more unified to users or applications. | Specialized distributed-computing environments |
| Real-time OS | Provides predictable response timing for tasks with deadlines. | Industrial control, medical devices, automotive systems, and avionics |
| Mobile or embedded OS | Designed for constrained devices, sensors, mobile hardware, or dedicated appliances. | Phones, tablets, routers, smart devices, and embedded controllers |
Hard and soft real-time systems
- Hard real-time: missing a deadline may cause system failure or serious harm.
- Soft real-time: deadlines are important, but occasional lateness reduces quality rather than causing total failure.
3. Processes, Threads, and Process States
| Term | Meaning |
|---|---|
| Program | A passive set of instructions stored on disk or another storage medium. |
| Process | A program in execution, with its own address space, resources, state, and OS-managed information. |
| Thread | An execution unit within a process. Threads in the same process commonly share memory and resources, while each thread has its own execution state and stack. |
Common process states
| State | Meaning |
|---|---|
| New | The process is being created. |
| Ready | The process is prepared to run and waits for CPU time. |
| Running | The process is currently executing on a CPU. |
| Waiting / Blocked | The process waits for an event, such as I/O completion or a lock becoming available. |
| Terminated | The process has completed or has been stopped. |
A running process may move to the waiting state when it requests I/O. When the required event occurs, it returns to the ready state. A scheduler can also preempt a running process and return it to the ready state.
Process Control Block (PCB)
The OS keeps process-related information in a data structure commonly called a Process Control Block. Exact contents vary by operating system, but a PCB commonly stores:
- Process identifier and current state
- Program counter and CPU-register information
- Scheduling priority and queue information
- Memory-management information
- Open files, I/O status, and accounting information
- Security or credential information
Switching the CPU from one process or thread to another is called a context switch. It enables multitasking but has overhead because the OS must save and restore execution state.
4. CPU Scheduling
CPU scheduling selects a ready process or thread to execute. Scheduling policies attempt to balance responsiveness, fairness, throughput, and efficient processor use.
Common scheduling measures
- CPU utilization: percentage of time the CPU is busy.
- Throughput: number of completed jobs in a time interval.
- Turnaround time: completion time minus arrival time.
- Waiting time: total time spent waiting in the ready queue.
- Response time: time from arrival until the process first receives CPU service.
| Algorithm | Description | Strength | Limitation |
|---|---|---|---|
| FCFS | First-Come, First-Served runs jobs in arrival order. | Simple and predictable. | Can cause the convoy effect when a long job delays many short jobs. |
| SJF | Shortest Job First chooses the shortest expected CPU burst. | Minimizes average waiting time when burst lengths are known accurately. | Actual future burst length is difficult to know; long jobs may wait. |
| SRTF | Shortest Remaining Time First is the preemptive form of SJF. | Responsive to newly arriving short jobs. | Requires burst-time estimates and may starve long jobs. |
| Priority scheduling | Selects the highest-priority ready process. | Important tasks can receive prompt service. | Low-priority tasks may starve; aging can reduce this problem. |
| Round Robin | Each ready process receives a time quantum in rotation. | Fair and suitable for time-sharing systems. | A very small quantum increases context-switch overhead; a large quantum behaves more like FCFS. |
| Multilevel Feedback Queue | Uses multiple queues and may move jobs between them based on behavior. | Can favor interactive and short jobs while still serving others. | Complex to tune and implement fairly. |
5. Memory Management and Virtual Memory
The OS manages physical memory and gives processes protected virtual address spaces. The familiar register-cache-RAM-storage hierarchy is primarily a hardware-storage hierarchy; the OS mainly manages RAM, virtual memory, and storage-backed data.
Memory-allocation approaches
| Technique | Main idea | Important point |
|---|---|---|
| Contiguous allocation | A process occupies one continuous block of physical memory. | Can suffer from external fragmentation. |
| Paging | Virtual memory is divided into fixed-size pages; physical memory is divided into fixed-size frames. | Avoids external fragmentation but may cause internal fragmentation. |
| Segmentation | Memory is divided into logical variable-sized segments, such as code or data. | Matches program structure but can suffer from external fragmentation. |
| Virtual memory | Provides a larger logical address space than immediately available physical RAM. | Only actively needed pages must remain in RAM at a given time. |
Paging and page faults
During paging, the OS and memory-management hardware translate virtual addresses to physical addresses. A cache of recent address translations is commonly called a Translation Lookaside Buffer (TLB).
A page fault occurs when a process accesses a page that is not currently in physical memory. The OS may load the required page from storage, update mappings, and resume the process. A page fault is a normal virtual-memory event, not automatically a program error.
| Algorithm | Rule | Note |
|---|---|---|
| FIFO | Replace the page that has been in memory the longest. | Simple, but can perform poorly and may show Belady's anomaly. |
| LRU | Replace the least recently used page. | Uses locality well, but exact implementation can be expensive. |
| Optimal | Replace the page whose next use is farthest in the future. | Provides a theoretical benchmark because future references are not known in real systems. |
Excessive paging activity can lead to thrashing, where the system spends much of its time moving pages between storage and memory instead of performing useful work.
6. Synchronization and Deadlocks
Critical sections and synchronization
When concurrent processes or threads share data, they can create race conditions unless access is coordinated. A critical section is code that accesses shared data and must not be executed unsafely by multiple tasks at the same time.
- Mutex: a lock that allows one task at a time to enter a protected section.
- Semaphore: a synchronization counter used to coordinate access to limited resources or events.
- Monitor: a higher-level synchronization construct that combines shared data with controlled operations.
Deadlock
A deadlock occurs when a group of processes or threads waits indefinitely because each one holds a resource and waits for a resource held by another member of the group.
Four necessary deadlock conditions
- Mutual exclusion: at least one resource is non-shareable.
- Hold and wait: a process holds resources while waiting for additional ones.
- No preemption: resources cannot be forcibly removed in the normal case.
- Circular wait: a circular chain exists in which each process waits for a resource held by the next process.
| Strategy | Approach |
|---|---|
| Prevention | Design the system so that at least one necessary deadlock condition cannot hold. |
| Avoidance | Allocate resources only when the system remains in a safe state; Banker's Algorithm is a classic example. |
| Detection and recovery | Allow deadlocks, detect them, then recover by terminating work, rolling back, or reclaiming resources where possible. |
| Practical engineering controls | Use consistent lock ordering, timeouts, limited lock scope, and careful resource design to reduce risk. |
7. File Systems and Storage Management
File-system concepts
- File: a named collection of related data.
- Directory: a structure that maps names to files or other directories.
- Metadata: information about a file, such as size, ownership, permissions, timestamps, and location details.
- File permissions: rules that control who can read, write, execute, or modify a file.
File-allocation methods
| Method | Description | Main trade-off |
|---|---|---|
| Contiguous allocation | A file occupies consecutive storage blocks. | Fast sequential and direct access, but growth and fragmentation can be difficult. |
| Linked allocation | Each file block points to the next block. | Easy growth, but poor random access and pointer overhead. |
| Indexed allocation | An index block stores pointers to a file's data blocks. | Supports direct access, but index information needs storage. |
| Extent-based allocation | A file is described by ranges of contiguous blocks. | Used by many modern file systems to combine efficient access with flexible growth. |
Disk scheduling
Disk-scheduling algorithms are most relevant to rotating hard disks because they have mechanical seek time and rotational delay. Solid-state drives do not have moving heads, so their performance concerns and scheduling behavior differ.
- FCFS: serves requests in arrival order.
- SSTF: serves the request with the shortest seek distance; some requests may wait too long.
- SCAN: moves the disk arm in one direction while serving requests, then reverses.
- C-SCAN: serves requests in one direction and returns to the beginning without serving requests on the return trip.
- LOOK and C-LOOK: reverse or return at the last pending request rather than moving to the physical end of the disk.
8. Kernel, System Calls, and Interrupts
| Concept | Meaning |
|---|---|
| Kernel | The privileged core of an OS that manages hardware access, memory, processes, and many low-level services. |
| User mode | A restricted execution mode used by ordinary applications to protect the system from unsafe operations. |
| Kernel mode | A privileged mode in which the kernel can perform protected operations and access hardware directly. |
| System call | A controlled interface through which a program requests an OS service, such as opening a file or creating a process. |
| Interrupt | A hardware or software signal that causes the CPU to pause normal execution and handle an event. |
| Device driver | Software that helps the OS communicate with a particular hardware device. |
Kernel design styles
- Monolithic kernel: many core services run in kernel space.
- Microkernel: keeps a smaller core in kernel space and moves more services into user space.
- Hybrid design: combines ideas from more than one approach.
9. Examples of Operating-System Families
The following are examples of operating-system families and platforms, not a ranking. Features vary across editions, versions, device manufacturers, and system configurations.
| Platform or family | General character |
|---|---|
| Microsoft Windows | Desktop, laptop, workstation, and server operating-system family. |
| macOS | Desktop operating system designed for Apple Macintosh computers. |
| Linux distributions | Complete systems built around the Linux kernel and additional software; widely used on servers, desktops, and embedded devices. |
| Android | Mobile operating-system platform based on the Linux kernel and the Android Open Source Project. |
| iOS | Mobile operating system for Apple mobile devices. |
| Unix and Unix-like systems | A broad family and heritage of multi-user, multitasking systems rather than one single modern operating system. |
10. Quick Revision and Practice Questions
| Topic | Key point |
|---|---|
| Operating system | Manages resources and provides controlled services to programs. |
| Process | A program in execution. |
| Thread | An execution unit within a process. |
| PCB | OS data structure containing process-related information. |
| Round Robin | Time-sharing scheduling based on a time quantum. |
| Virtual memory | Uses virtual addresses and storage-backed pages to support larger logical address spaces. |
| Page fault | Occurs when a needed page is not currently in physical memory. |
| Deadlock | Indefinite waiting caused by a circular resource dependency. |
| System call | Controlled request from a program for an OS service. |
| Kernel | Privileged OS core that manages low-level resources. |
Practice questions
-
What is the difference between a program and a process?
Answer: A program is passive code stored on disk; a process is that program while it is executing with allocated resources and state. -
What is the main difference between a process and a thread?
Answer: A process has its own address space and resources, while threads in the same process usually share that address space and resources. -
Why can Round Robin be inefficient when the time quantum is too small?
Answer: Too many context switches occur, increasing overhead. -
Name the four necessary conditions for deadlock.
Answer: Mutual exclusion, hold and wait, no preemption, and circular wait. -
Why is the Optimal page-replacement algorithm mainly theoretical?
Answer: It requires knowledge of future memory references, which a real operating system does not have.