Last Minute Notes – Operating Systems (original) (raw)

Last Updated : 9 Feb, 2026

An Operating System (OS) is a system software that manages computer hardware, software resources, and provides common services for computer programs. It acts as an interface between the user and the computer hardware.

**Types of Operating System (OS):

**Batch OS

**Multiprogramming OS

**Time-Sharing / Multitasking OS

**Multiprocessing OS

**Multi-user OS

**Distributed OS

**Network OS

**Real-Time OS (RTOS)

Threads

A lightweight process and the basic unit of CPU execution.

**Features:

****Creation :**Using fork() system call.

**Types of Threads:

**Based on number:

**Based on level:

**Examples: Java Threads, POSIX Threads, Windows Threads, Solaris Threads

Process

Process is a program in execution.

**Key Points:

state

Process States Diagram

Schedulers

The OS uses schedulers to manage which process gets CPU time.

**Long-term Scheduler

**Medium-term Scheduler

**Short-term Scheduler

Dispatchers

Responsible for loading the selected process onto the CPU.

Performs context switching:

**CPU Scheduling Algorithms

**Need of Scheduling

**Process Timing Terms

**Arrival Time (AT)

**Completion Time (CT)

**Burst Time (BT)

**Turnaround Time (TAT)

**Waiting Time (WT)

Objectives of Process Scheduling Algorithm:

**Different CPU Scheduling Algorithms:

**1. First Come First Serve (FCFS): First Come, First Serve (FCFS) is one of the simplest types of CPU scheduling algorithms. It is exactly what it sounds like: processes are attended to in the order in which they arrive in the ready queue, much like customers lining up at a grocery store.

**2. Shortest Job First (SJF): Shortest Job First (SJF) or Shortest Job Next (SJN) is a scheduling process that selects the waiting process with the smallest execution time to execute next.

**3. Shortest Remaining Time First (SRTF): It is preemptive mode of SJF algorithm in which jobs are scheduled according to the shortest remaining time.

**4. Round Robin (RR) Scheduling: It is a method used by operating systems to manage the execution time of multiple processes that are competing for CPU attention.

**5. Priority Based scheduling: In this scheduling, processes are scheduled according to their priorities, i.e., highest priority process is schedule first. If priorities of two processes match, then scheduling is according to the arrival time.

**6. Highest Response Ratio Next (HRRN): In this scheduling, processes with highest response ratio is scheduled. This algorithm avoids starvation.

\text{Response Ratio} = \frac{\text{WT} + \text{BT}}{\text{BT}}

**Some useful facts about Scheduling Algorithms:

  1. FCFS can cause long waiting times, especially when the first job takes too much CPU time.
  2. Both SJF and Shortest Remaining time first algorithms may cause starvation. Consider a situation when a long process is there in the ready queue and shorter processes keep coming.
  3. If time quantum for Round Robin scheduling is very large, then it behaves same as FCFS scheduling.
  4. SJF is optimal in terms of average waiting time for a given set of processes. SJF gives minimum average waiting time, but problems with SJF is how to know/predict the time of next job.

**Critical Section Problem

  1. **Critical Section - The portion of the code in the program where shared variables are accessed and/or updated.
  2. **Remainder Section - The remaining portion of the program excluding the Critical Section.
  3. **Race around Condition - The final output of the code depends on the order in which the variables are accessed. This is termed as the race around condition.

Requirements for a Correct Solution

A solution for the critical section problem must satisfy the following three conditions:

**Mutual Exclusion

**Progress

**Bounded Waiting

**Synchronization Tools

**Semaphore

A semaphore is a synchronization tool used by the OS to control access to shared resources in a concurrent system.

**Atomic Operations

Operations that execute in one indivisible CPU step:

**Two Atomic Operations:

**Types of Semaphores:

**Counting Semaphore

**Mutex

**Misconception: There is an ambiguity between binary semaphore and mutex. We might have come across that a mutex is binary semaphore. But they are not! The purpose of mutex and semaphore are different. May be, due to similarity in their implementation a mutex would be referred as binary semaphore.

**Deadlock

Deadlock is a situation where a set of processes are permanently blocked because:

Necessary Conditions for Deadlock

**Deadlock handling

There are three ways to handle deadlock

**Deadlock prevention

To ensure the system never enters a deadlock state, at least one of the conditions for deadlock must be prevented:

**Mutual Exclusion: This condition cannot be removed because some resources (non-shareable resources) must be exclusively allocated to one process at a time.

**Hold and Wait: This condition can be avoided using the following strategies:

**Pre-emption:If a process P1P1P1 requests a resource RRR that is held by another process P2P2P2:

**Circular Wait: This condition can be avoided using the following method:

**Deadlock Avoidance

The Deadlock Avoidance Algorithm prevents deadlocks by monitoring resource usage and resolving conflicts before they occur, like rolling back processes or reallocating resources. It minimizes deadlocks but doesn’t fully guarantee their prevention. The two key techniques are:

  1. **Process Initiation Denial: Blocks processes that might cause deadlock.
  2. **Resource Allocation Denial: Rejects resource requests that could lead to deadlock.

**Banker's Algorithm

The Banker’s Algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It ensures that the system remains in a safe state by simulating the allocation of resources to processes without running into a deadlock.

The following Data structures are used to implement the Banker’s Algorithm:

**1. Available: It is a 1-D array of size **‘m’ indicating the number of available resources of each type. Available[ j ] = k means there are **‘k’ instances of resource type **R **j.

**2. Max: It is a 2-d array of size ‘ **n*m’ that defines the maximum demand of each process in a system. Max[ i, j ] = k means process **P i may request at most **‘k’ instances of resource type **R j.

**3.Allocated: It is a 2-d array of size **‘n*m’ that defines the number of resources of each type currently allocated to each process. Allocation[ i, j ] = k means process **P i is currently allocated **‘k’ instances of resource type **R j.

**4. Need: It is a 2-d array of size **‘n*m’ that indicates the remaining resource need of each process. Need [ i, j ] = k means process **P i currently needs **‘k’ instances of resource type **R j Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ].

**Deadlock Detection and Recovery

**Detection: A cycle in the resource allocation graph represents deadlock only when resources are of single-instance type. If the resources are of multiple instance type, then the safety algorithm is used to detect deadlock.

**Recovery: A system can recover from deadlock through adoption of the following mechanisms:

**Deadlock Ignorance

In the Deadlock ignorance method the OS acts like the deadlock never occurs and completely ignores it even if the deadlock occurs. This method only applies if the deadlock occurs very rarely. The algorithm is very simple. It says, ” if the deadlock occurs, simply reboot the system and act like the deadlock never occurred.” That’s why the algorithm is called the **Ostrich Algorithm.

**Memory Management

In multiprogramming system, the task of subdividing the memory among the various processes is called memory management. The task of the memory management unit is the efficient utilization of memory and minimize the internal and external fragmentation.

Requirement of Memory Management

Logical and Physical Address Space

Static and Dynamic Loading

Loading a process into the main memory is done by a loader. There are two different types of loading :

**Memory Management Techniques

(a) Single Partition Allocation Schemes - The memory is divided into two parts. One part is kept to be used by the OS and the other is kept to be used by the users.
(b) Multiple Partition Schemes -

  1. **Fixed Partition - The memory is divided into fixed size partitions.
  2. **Variable Partition - The memory is divided into variable sized partitions.

Variable partition allocation schemes:

  1. **First Fit - The arriving process is allotted the first hole of memory in which it fits completely.
  2. **Best Fit - The arriving process is allotted the hole of memory in which it fits the best by leaving the minimum memory empty.
  3. **Worst Fit - The arriving process is allotted the hole of memory in which it leaves the maximum gap.

Note:

**Paging

In paging, physical memory is divided into fixed-size frames and virtual memory is divided into fixed-size pages. The size of a page is equal to the size of a frame.

paging

**Segmentation

Segmentation is implemented to give users view of memory. The logical address space is a collection of segments. Segmentation can be implemented with or without the use of paging.

logical_view_of_segmentation

**Page Fault: A page fault is a type of interrupt, raised by the hardware when a running program accesses a memory page that is mapped into the virtual address space, but not loaded in main/virtual memory.

**Page Replacement Algorithms

**1. First In First Out (FIFO)

This is the simplest page replacement algorithm. In this algorithm, operating system keeps track of all pages in the memory in a queue, oldest page is in the front of the queue. When a page needs to be replaced page in the front of the queue is selected for removal.

For example, consider page reference string 1, 3, 0, 3, 5, 6 and 3 page slots.

**Belady’s anomaly: Belady’s anomaly proves that it is possible to have more page faults when increasing the number of page frames while using the First in First Out (FIFO) page replacement algorithm.

For example, if we consider reference string 3 2 1 0 3 2 4 3 2 1 0 4 and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10 page faults.

**2. Optimal Page replacement

In this algorithm, pages are replaced which are not used for the longest duration of time in the future.

Let us consider page reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 and 4 page slots.

Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults.
0 is already there so —> 0 Page fault.
When 3 came it will take the place of 7 because it is not used for the longest duration of time in the future.—> 1 Page fault.
0 is already there so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available in the memory.

Optimal page replacement is perfect, but not possible in practice as an operating system cannot know future requests. The use of Optimal Page replacement is to set up a benchmark so that other replacement algorithms can be analyzed against it.

**3. Least Recently Used (LRU)

In this algorithm, the page will be replaced which is least recently used.

Let say the page reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 .

Initially, we have 4-page slots empty. Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults.
0 is already their so —> 0 Page fault.
When 3 came it will take the place of 7 because it is least recently used —> 1 Page fault.
0 is already in memory so —> 0 Page fault. 4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available in the memory.

**4. Most Recently Used (MRU)

In this algorithm, page will be replaced which has been used recently. Belady’s anomaly can occur in this algorithm.

Example: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frames. Find number of page faults using MRU Page Replacement Algorithm.

Virtual Memory

Virtual memory is a memory management technique used by operating systems to give the appearance of a large, continuous block of memory to applications, even if the physical memory (RAM) is limited. It allows larger applications to run on systems with less RAM.

**Key Points

virtual_memory

Demand Paging

Demand paging is a memory management technique used in operating systems where a page (a fixed-size block of memory) is only loaded into the computer's RAM when it is needed, or "demanded" by a process.

**How Demand Paging Works:

  1. **When a Program Starts: Initially, only a part of the program (some pages) is loaded into RAM. The rest of the program is stored on the hard drive (secondary memory).
  2. **When a Page is Needed: If the program needs a page that is not currently in RAM, a **page fault occurs. This means the operating system must fetch the required page from the hard drive and load it into RAM.
  3. **Efficient Memory Use: With demand paging, only the parts of the program that are actively used are in memory, which makes better use of RAM. This allows the system to run large programs without needing all of their data loaded into memory at once.

**Advantages

Thrashing

Thrashing is a situation in which the operating system spends more time swapping data between RAM (main memory) and the hard drive (secondary memory) than actually executing processes. This happens when there isn't enough physical memory (RAM) to handle the current workload, causing the system to constantly swap pages in and out of memory.

**Cause

**Effects

**Techniques to Handle Thrashing

File Systems

A file system is a method an operating system uses to store, organize, and manage files and directories on a storage device.

**File Directories

The collection of files is a file directory. The directory contains information about the files, including attributes, location, and ownership. Much of this information, especially that is concerned with storage, is managed by the operating system.

Below are information contained in a device directory.

**The operation performed on the directory are:

**File Allocation Methods

There are several types of file allocation methods. These are mentioned below.

**Continuous Allocation: A single continuous set of blocks is allocated to a file at the time of file creation. Thus, this is a pre-allocation strategy, using variable size portions. The file allocation table needs just a single entry for each file, showing the starting block and the length of the file.

Continuous-allocation

**Linked Allocation(Non-contiguous allocation): Allocation is on an individual block basis. Each block contains a pointer to the next block in the chain. Again the file table needs just a single entry for each file, showing the starting block and the length of the file. Although pre-allocation is possible, it is more common simply to allocate blocks as needed. Any free block can be added to the chain.

Linked-Allocation

**Indexed Allocation: Indexed Allocation allocates disk blocks non-contiguously. A separate index block is used to store the addresses of all the data blocks of a file. The file table contains the address of the index block. Data blocks do **not point to each other.

Indexed-Allocation

Disk Scheduling

Disk scheduling algorithms decide which disk request to service next to reduce seek time and improve performance.

**Disk Scheduling Algorithms

FCFS (First Come First Serve)

SSTF (Shortest Seek Time First)

SCAN (Elevator Algorithm)

**C-SCAN (Circular SCAN)

**LOOK

**C-LOOK

LIFO (Last In First Out)

Disk

Comparison Among the Disk Scheduling Algorithms