An Operating System is system software that manages computer hardware and software
resources. It provides essential services like memory management, process scheduling, file
handling, and device control. The OS acts as a bridge between the user and the hardware,
ensuring programs run efficiently and hiding complex underlying hardware
Program: A Program is an executable file which contains a certain set of instructions written
to complete the specific job or operation on your computer.
• It’s a compiled code. Ready to be executed.
• Stored in Disk
Process: Program under execution.with its own memory space, registers, and system
resources. The OS manages process creation, scheduling, and termination.
Inter-Process Communication (IPC) is the mechanism that allows two or
more processes to exchange data and coordinate with each other.
● Processes are isolated by the OS (each has its own memory space).
● IPC provides ways to share data or send messages between them.
Thread
A thread is the smallest unit of execution within a process.
Multiple threads in the same process share memory and resources but have their own
registers and stack.
Threads improve performance by enabling parallel execution within the same application.
Context Switching
Context switching is the process where the CPU saves the state of the currently running
process or thread and loads the state of the next one to execute.
This allows multiple processes or threads to share a single CPU, enabling multitasking.
It involves saving registers, program counter, and other information so that the process can
resume later without losing progress.
Multitasking
Multitasking is the ability of an OS to run multiple processes at the same time.
The CPU quickly switches between processes, giving the illusion that they run
simultaneously.
Examples: Listening to music while browsing the internet.
Multithreading
Multithreading is running multiple threads within the same process.
Threads share the same memory and resources, enabling faster communication and less
overhead than separate processes.
Example: A web browser loading multiple tabs or images in parallel.
Kernel
The kernel is the core part of an Operating System that directly interacts with hardware.
It manages system resources like CPU, memory, and devices, and provides essential
services to applications.
It operates in a privileged mode, ensuring secure and controlled access to hardware.
Functions of Kernel
1. Process Management – Creates, schedules, and terminates processes.
2. Memory Management – Allocates and frees memory for processes.
3. Device Management – Controls and communicates with hardware devices.
4. File System Management – Handles file storage, retrieval, and permissions.
5. System Calls Handling – Provides an interface for user applications to request OS
services.
“We use multithreading to make better use of CPU cores, speed up execution for
independent tasks, keep applications responsive, and handle multiple operations
concurrently, especially in I/O-heavy or real-time systems. Since threads share the
same memory space, communication between them is also faster than between
separate processes.”
. Critical Section
A critical section is a part of a program where a shared resource (like a
variable, file, or database) is accessed and modified.
Only one thread/process should execute in the critical section at a time to
avoid errors.
Example:
java
CopyEdit
// Critical section: sharedCounter is shared by multiple threads
sharedCounter++;
2. Critical Section Problem
The problem is:
● Multiple threads/processes may try to enter the critical section at the same time.
● This can cause incorrect results because operations may overlap.
3. Race Condition
A race condition happens when the program's output depends on the
timing/order of threads.
This occurs when multiple threads access shared data without proper
synchronization.
Example:
● Two threads increment counter from 0 → Expected: counter = 2,
but without control, both read 0 and write 1 → result = 1 (wrong).
A mutex is a locking mechanism that allows only one thread to access a resource at
a time and must be unlocked by the same thread. A semaphore is a signaling
mechanism with permits — it can allow multiple threads to access a fixed number of
resources. A binary semaphore acts like a mutex, while a counting semaphore is for
managing resource pools.”
Think of a Semaphore as parking spaces
● Imagine a parking lot with N parking spots.
● Only N cars can park at the same time.
● If the parking lot is full, other cars wait outside.
● When a car leaves, one spot becomes free, and another waiting car can enter.
Types of Semaphore
1. Binary Semaphore → Parking lot with 1 spot.
○ Works just like a mutex — only one thread can enter.
2. Counting Semaphore → Parking lot with more than 1 spot.
Multiple threads can enter at the same time, up to the limit.
The Producer-Consumer problem describes two types of threads:
Producer → generates data and puts it into a shared buffer.
Consumer → takes data out of the shared buffer and processes it.
The challenge is to synchronize them so:
The producer doesn’t add data if the buffer is full.
The consumer doesn’t remove data if the buffer is empty.
4. How to Solve
We need:
● Mutual Exclusion → Only one thread modifies the buffer at a time (mutex or
synchronized).
● Signaling → Notify producer when a spot is free, and notify consumer when
an item is available (semaphore, wait()/notify() in Java).
Deadlock is a situation where two or more processes are unable to proceed because
each is waiting for a resource held by another.
It causes all involved processes to wait indefinitely, halting system progress.
Deadlock Necessary Condition: 4 Condition should hold simultaneously.
a. Mutual Exclusion
i. Only 1 process at a time can use the resource, if another process requests that
resource, the requesting process must wait until the resource has been released.
b. Hold & Wait
i. A process must be holding at least one resource & waiting to acquire additional
resources that are currently being held by other processes.
c. No-preemption
i. Resource must be voluntarily released by the process after completion of
execution. (No resource preemption)
d. Circular wait
i. A set {P0, P1, ... ,Pn} of waiting processes must exist such that P0 is waiting for a
resource held by P1, P1 is waiting for a resource held by P2, and so on.
1. Internal Fragmentation: if the size of the process is lesser then the total size of
the partition then some size of the partition gets wasted and remain unused.
This is wastage of the memory and called internal fragmentation.
2. External Fragmentation: The total unused space of various partitions cannot be
used to load the processes even though there is space available but not in the
contiguous form.