Operating System: Lectures
Operating System: Lectures
Lectures
DRY principal → do not repeat yourself
⚙️ How it works:
When one program is waiting (e.g., for I/O), CPU switches to another.
⚙️ How it works:
OS rapidly switches between tasks, giving the illusion of parallel execution.
⚙️ How it works:
Tasks are divided among processors.
thread ma cache preserve re bcz next tread ne use karvani jarur pade to ? elte
process to same j che so cache preserve rakhvno
User space
micro kernel ma performance ochu male bcz user space and kernel space
vache switch karta revu psde i.e overhead
interview questions:
user mode and kernel mode vache commn kai rite thay ?
Msg passing
Operating system 2
This can happen when:
Process ID
Registers
Program Counter
State
Memory limits
I/O information
Operating system 3
📌 7. Add to Scheduler
Process state is set to "Ready"
📌 8. Context Switching
When the process is chosen by the CPU scheduler, it begins execution
Standard che
Memory architecture
5 steps
Process termination
Operating system 4
Producer Consumer
OS ma badho khel most of CPU cycle ne ghatadva aaju baju fare che etle etlu
to boli j devanu k CPU utilization thay aa kam thi
Reader write and producer consumer problem sarkhi rite karvi plese code and
functionality of semaphores and mutex, conditional variables sarkhi rite karvi
Lec 23 - Baki rahkyo che leetcode questions kare che etle sarkhi rite samjava
💡
SELF STUDY
🔒 What is a Mutex?
Mutex stands for Mutual Exclusion.
It is a lock that allows only one thread to enter a critical section at a time.
🔧 Working:
lock() – A thread locks the mutex before entering critical section.
If another thread tries to lock() it while it's already locked — it will wait (block).
🚦 What is a Semaphore?
A semaphore is a signaling mechanism. It can be binary (like a mutex) or
counting (allowing more than one thread).
Operating system 5
✅ Example Use Case:
You have 5 identical printers, and multiple threads want to print. You want to
allow only 5 threads to access them simultaneously.
🔧 Working:
Has an integer counter.
wait() (also called P() or down or acquire() ) — Decrements the counter. If counter
is 0, it blocks.
signal() (also called V() or up or release ) — Increments the counter and wakes
waiting threads.
wait, P and acquire are used alternatively similarly signal, V and release.
🔁 Code Logic:
std::counting_semaphore<5> sem(5); // allows 5 threads
void print_job() {
sem.acquire(); // wait // access printer
sem.release(); // signal
}
wait (sleep/block) until another thread notifies them that something has
changed.
🔁 Real-Life Analogy
Imagine a classroom:
Operating system 6
Students (threads) are waiting for the teacher (condition) to say: “The exam is
over!”
When the teacher says “exam is over” (notify), they all leave (continue
execution).
✅ Benefits of ASID:
Benefit Description
✔️ Memory protection Prevents one process from using another's TLB entry
✔️ Efficient multi-process TLB can store entries from multiple processes safely
support
🔁 Without ASID:
OS would have to flush (clear) the entire TLB during every context switch →
slow and inefficient.
🧠 With ASID:
TLB can keep entries for multiple processes.
Operating system 7
✅ TLB (Translation Lookaside Buffer) in Operating System:
TLB (Translation Lookaside Buffer) is a small, fast, hardware cache in the MMU
(Memory Management Unit) that stores recent translations of virtual addresses
to physical addresses.
🧠 Why TLB?
Accessing the page table in RAM for every memory reference is slow.
So, TLB helps by storing recently used virtual-to-physical address mappings for
fast retrieval.
📌 How it works:
1. CPU generates a virtual address.
2. TLB is checked:
For example:
Code segment
Operating system 8
Data segment
Stack segment
Heap segment
Better organization
Protection
Flexibility
🧠 What is a Livelock?
A livelock is a situation in concurrent programming where two or more processes
(or threads) are not blocked, but they keep reacting to each other in a way that
prevents progress.
So:
But they keep interfering with each other, so no useful work is completed.
Processes are
State Processes are running
blocked
📘 Real-Life Analogy
Imagine two people trying to pass each other in a hallway:
Operating system 9
Both move to the left → block.
They keep moving, trying to be polite, but never pass each other.
That’s a livelock.
@2nd Time
Types of Threads
Operating system 10
📌 Example:
When you print a file, the data is not sent directly to the printer.
The printer then picks up jobs one by one and prints them.
✅ Benefits:
Allows multiple processes to submit print jobs at the same time.
🧺 What is Buffering?
Buffering is the technique of using a temporary memory area (buffer) to hold
data while it is being transferred between two devices/processes with different
speeds.
📌 Example:
While watching a video online, data is downloaded in the buffer before being
played.
✅ Benefits:
Compensates for speed mismatch between producer and consumer.
🧠 Key Differences:
Feature Spooling Buffering
Queues jobs for slow devices (like Temporarily holds data during
Purpose
printer) transfer
Operating system 11
Feature Spooling Buffering
📝 Summary:
Spooling is like a to-do list for devices: jobs are stored and processed one by
one.
Buffering is like a holding area to smooth out the flow between fast and slow
components.
Operating system 12
Feature Nano Kernel Exo Kernel
🧠 Summary Diagram:
Power On
↓
CPU starts & looks for BIOS/UEFI
↓
BIOS/UEFI runs POST (hardware test)
↓
BIOS/UEFI looks for bootloader in MBR (BIOS) or EFI partition (UEFI)
↓
Bootloader found and executed (GRUB, Bootmgr, boot.efi)
↓
Bootloader loads OS kernel into RAM
↓
OS takes over — system is ready
🧩 1. Process Concurrency
🔹 Definition:
Operating system 13
Concurrency refers to the execution of multiple processes or threads at the
same time (or seemingly at the same time, via interleaving on a single CPU).
Even if the system has only one CPU, it rapidly switches between processes,
giving the illusion that they are running in parallel.
🧠 Goal:
Improve CPU utilization
🛠️ Example:
A music app playing songs while you browse files — both processes are
concurrent.
🧩 2. Synchronization
🔹 Definition:
Synchronization ensures that multiple concurrent processes/threads don’t
interfere with each other, especially when accessing shared resources (e.g.,
variables, memory, files).
Without synchronization, you may face issues like:
Race conditions
Inconsistent data
🧠 Goal:
Ensure data consistency
🔐 Tools Used:
Operating system 14
Mutexes
Semaphores
Monitors
Condition variables
Can’t proceed.
It increases S to 1.
//Reader mate
Reader() {
wait(mutex); // Lock readCount
Operating system 16
readCount++;
if (readCount == 1) {
wait(writeLock); // First reader locks writer
}
signal(mutex); // Unlock readCount
// Writer mate
Writer() {
wait(writeLock); // Lock out readers and writers
Operating system 17
bypassing the CPU to speed up memory operations. The process is managed by a
chip known as a DMA controller.
GPT ans:
DMA (Direct Memory Access) is a technique that allows hardware devices (like
disk drives, network cards, sound cards, etc.) to transfer data directly to/from
memory without CPU involvement.
Normally, the CPU handles all data transfers between devices and memory. But
this can slow down the system. DMA solves this by offloading data transfer tasks
from the CPU.
Number of
Better multitasking and parallel execution
Cores
Heat
Sustained performance under load without thermal throttling
Management
What is multitasking?
Multitasking is the ability of an operating system to execute multiple tasks
(processes or programs) simultaneously. The CPU switches between tasks so
quickly that it gives the illusion that all tasks are running at the same time — even
on a single-core processor.
⚙️ How It Works:
Operating system 18
OS uses a scheduler to switch between tasks.
Pipes (Same Process): This allows a flow of data in one direction only.
Analogous to simplex systems (Keyboard). Data from the output is usually
buffered until the input process receives it which must have a common origin.
Named Pipes (Different Processes): This is a pipe with a specific name it can
be used in processes that don’t have a shared common process origin. E.g.
FIFO where the details written to a pipe are first named.
Shared Memory: This allows the interchange of data through a defined area of
memory. Semaphore values have to be obtained before data can get access to
shared memory.
Operating system 19
53. What is Cycle Stealing?
Cycle stealing is a method of accessing computer memory (RAM) or bus without
interfering with the CPU. It is similar to direct memory access (DMA) for allowing
I/O controllers to read or write RAM without CPU intervention.
✅ Advantages of Multithreading:
🔄 Concurrent Execution: Multiple threads run in parallel, improving
performance and responsiveness.
💾 Resource Sharing: Threads within the same process share code, data, and
resources, reducing overhead.
Operating system 20
📈 Scalability: Programs can scale with multi-core processors by running
threads on separate cores.
❌ Drawbacks of Semaphores:
🧠 Risk of Deadlock:
Incorrect use (e.g., forgetting to release a semaphore) can cause deadlocks,
where processes wait forever for each other.
❗ Priority Inversion:
A lower-priority thread holding a semaphore can block a higher-priority
thread, leading to unintended delays.
🔁 Starvation:
If not managed carefully, some threads might never get access to the critical
section (e.g., if others keep acquiring the semaphore).
Operating system 21
No Preemption: The operating system is not allowed to take a resource back from
a process until the process gives it back.
Circular Wait: A set of processes waiting for each other in circular form.
Deadlock: It occurs when two processes are blocked and hence neither can
proceed to execute
Precedence graphs?
→ shows the order of execution of processes.
Relocation
Protection
Sharing
Operating system 22
Logical organization
Physical organization
2. Load-Time Binding
3. Execution-Time Binding
Operating system 23
4. What is RAID structure in OS? What are the different levels of
RAID configuration?
RAID (Redundant Arrays of Independent Disks) is a method used to store data on
Multiple hard disks therefore it is considered as data storage virtualization
technology that combines multiple hard disks. It simply balances data protection,
system performance, storage space, etc. It is used to improve the overall
performance and reliability of data storage. It also increases the storage capacity
of the system and its main purpose is to achieve data redundancy to reduce data
loss.
turn values.
System Calls are the only way through which a process can go into kernel mode
from user mode.
Operating system 24
uses a bootloader named Windows Boot Manager (Bootmgr.exe), most Linux
systems use GRUB, and Macs use something called boot.efi
Registers in the PCB, it is a data structure. When a processes is running and it's
time slice expires, the current value of process specific registers would be stored
in the PCB and the process would be swapped out. When the process is
scheduled to be run, the register values is read from the PCB and written to the
CPU registers. This is the main purpose of the registers in the PCB.
convoy effect : if one process has longer BT. It will have major effect on average
WT of diff processes, called Convoy effect. it is basically seen in FCFS algo
Multilevel Queue
Ready Q is divide into multiple q depending upon priority
1. System process
2. Interactive process
Operating system 25
3. Batch process
Multilevel feedback q
allows process to move between sub queue
Peterson’s solution can be used to avoid race condition but holds good for only 2
process/threads.
Contention: one thread has acquired the lock, other threads will be
busy waiting, what if thread that had acquired the lock dies, then all
other threads will be in infinite waiting
Contention is not present in conditional variables bcz there is no busy waiting in
here.
Banker Algorithm
When a process requests a set of resources, the system must determine whether
allocating
these resources will leave the system in a safe state. If yes, then the resources
may be
allocated to the process. If not, then the process must wait till other processes
release
enough resources
Operating system 26
Free List
Operating system 27
if page fault occur OS creates trap to manage page access from swap space and
after page bring back into mem process continue its execution
Operating system 28