Operating System
Operating System
Definition of
Operating System 3. Multiprogramming
Operating System
An Operating System (OS) is system
software that acts as an interface between Several programs are kept in
the computer hardware and the user. It memory at the same time.
manages computer resources and provides CPU switches between programs to
services for application programs. maximize utilization.
Improves system efficiency.
Definition:
5. Multiprocessing
Types of Operating Operating System
Systems Uses two or more processors/CPUs.
Increases processing speed and
An Operating System can be classified into reliability.
several types based on how it manages Common in modern computer
resources and users. systems.
Types of Operating
Components of System Structures
Operating System
1. Simple Structure
Structure
No clear separation between
1. Kernel components.
Easy to design but difficult to
Core part of the operating system. maintain.
Manages CPU, memory, and devices. Example: Early operating systems like
Acts as an interface between MS-DOS.
hardware and software.
2. Layered Structure
2. Process Management
OS is divided into layers.
Creates, schedules, and terminates Each layer performs a specific
processes. function.
Handles multitasking. Easier to debug and maintain.
Thread
Objectives of Process A thread is the smallest unit of CPU
Management execution within a process. It is often called
a lightweight process.
Multiple threads can exist within a single Process Thread
process and share the same memory and more complex easier
resources.
More resource Less resource
Characteristics of a Thread consumption consumption
Failure of one process Failure of one thread
Shares memory and resources with usually does not may affect the entire
other threads of the same process. affect others process
Has its own program counter,
registers, and stack.
Executes tasks concurrently. Process Control
Example Block (PCB) in
In a web browser: Operating System
One thread handles user input.
Another thread loads web pages.
Definition
Another thread plays media.
A Process Control Block (PCB) is a data
structure maintained by the Operating
Types of Threads System that stores all information about a
process. It helps the OS manage and control
1. User-Level Threads processes during execution.
2. Kernel-Level Threads
Purpose of PCB
Managed directly by the operating
system. Keeps track of each process.
OS schedules and controls them. Stores process-related information.
More reliable but slightly slower. Helps in process scheduling and
context switching.
Allows the operating system to
manage multiple processes efficiently.
Process Thread
Independent Smallest execution
execution unit unit
Has its own memory Shares memory with PCB Structure
space other threads
+-------------------------+
Creation is slower Creation is faster | Process ID (PID) |
Communication is Communication is +-------------------------+
| Process State |
+-------------------------+ 1. Shared Memory
| Program Counter |
+-------------------------+
| CPU Registers | A memory region is shared among
+-------------------------+ multiple processes.
| Scheduling Information | Processes can read and write data
+-------------------------+ directly in the shared area.
| Memory Information | Fast communication because data
+-------------------------+
does not need to be copied
| Accounting Information |
+-------------------------+ repeatedly.
| I/O Status Information |
+-------------------------+ Advantages:
High speed.
Inter-Process Efficient for large amounts of data.
Communication Disadvantage:
Send(Message)
Receive(Message)
Need for IPC
Advantages:
Data sharing between processes.
Information exchange. Easy to implement.
Process synchronization. Suitable for distributed systems.
Resource sharing.
Coordination among multiple
Disadvantage:
processes.
Slower than shared memory.
P3 8
Example P2 11
P2 1 3 WT = TAT - BT
P3 2 2 Process TAT BT WT
Process TAT BT WT Process Burst Time (BT) Priority
P1 6 6 0 P1 5 3
P2 10 3 7 P2 3 1
P3 6 2 4 P3 4 2
P4 13 5 8 P4 2 4
Priority Scheduling P2 → P3 → P1 → P4
P1 12
P4 14
Example
Consider the following processes:
Process TAT BT WT P3 2
P2 3 3 0
P3 7 4 3
Time Quantum = 2 ms
P1 12 5 7
P4 14 2 12
P2 4
Waiting Time (WT)
Formula: do {
Entry Section
Critical Section
WT = TAT - Burst Time Exit Section
Remainder Section
Process TAT BT WT } while (true);
P1 11 5 6 1. Entry Section
P2 10 4 6
P3 6 2 4 Requests permission to enter the
critical section.
Average Waiting Time:
2. Critical Section
(6+6+4)/3=5.33(6+6+4)/3 =
5.33(6+6+4)/3=5.33 Executes code that accesses shared
resources.
Average Waiting Time = 5.33 ms
3. Exit Section
Definition Synchronization in
The Critical Section Problem is a problem Operating System
in Operating Systems that occurs when
multiple processes or threads access and
modify shared resources at the same time. Definition
A critical section is a part of a program Process Synchronization is a technique
where a process accesses shared data or used by the Operating System to control the
resources. execution of multiple processes or threads
that access shared resources at the same
Example: Multiple processes updating the time.
same bank account balance or shared file.
It ensures that data remains consistent and
prevents problems caused by concurrent
execution.
Structure of a Process
Types of Synchronization
A process using a critical section generally
has four parts: 1. Process Synchronization
Bounded Buffer problem is also
called producer consumer
problem. This problem is One set of data is shared among a
generalized in terms of the number of processes.
Producer-Consumer problem.
Solution to this problem is, Once a writer is ready, it performs its
creating two counting semaphores
write. Only one writer may write at a
“full” and “empty” to keep track of
the current number of full and time.
empty buffers respectively.
Producers produce a product and
consumers consume the product,
Handling Deadlocks
but both use of one of the containers
each time Definition of Deadlock
[Link] philosopher problem: A deadlock is a situation in an Operating
System where two or more processes are
The Dining Philosopher Problem states unable to continue execution because each
that K philosophers seated around a process is waiting for a resource held by
circular table with one chopstick between another process.
each pair of philosophers. There is one
chopstick between each philosopher. A Example:
philosopher may eat if he can pickup the
Process P1 holds Resource R1 and
two chopsticks adjacent to him. One
waits for R2.
chopstick may be picked up by any one of Process P2 holds Resource R2 and
its adjacent followers but not both. This waits for R1.
problem involves the allocation of limited Both processes wait forever
resources to a group of processes in a
deadlock-free and starvation-free Methods for Handling
manner. Deadlocks
2. Readers and Writers Problem:
1. Deadlock Prevention
Suppose that a database is to be shared
among several concurrent processes. Ensures that at least one deadlock
condition never occurs.
Some of these processes may want only
Techniques:
to read the database, whereas others o Allow resource sharing.
may want to update (that is, to read o Request all resources at once.
and write) the database. We o Allow resource preemption.
distinguish between these two types of
Disadvantage: May reduce resource
processes by referring to the former as
utilization.
readers and to the latter as writers.
Precisely in OS we call this situation as
the readerswriters problem. Problem
parameters: 2. Deadlock Avoidance
The OS checks whether resource Contiguous Non-Contiguous
allocation keeps the system in a safe Allocation Allocation
state.
A common method is: block multiple blocks
Simple Complex
Banker’s Algorithm implementation implementation
Causes external Reduces
Used to avoid deadlocks by checking fragmentation fragmentation
safe resource allocation. Less memory Better memory
utilization utilization
Example: Fixed Example: Paging,
partitioning Segmentation
3. Deadlock Detection and
Recovery Page Replacement
The OS allows deadlocks to occur, then
Algorithms
detects and removes them.
Definition
Detection:
Page Replacement Algorithm is a
Uses resource allocation graphs. technique used by the Operating System to
Checks for circular waiting. decide which page should be removed from
main memory when a new page needs to be
Recovery Methods: loaded and the memory is full.
1 2 3 4 Memory:
Frames = 3 [1 2 3]
Disadvantages: Example
May remove frequently used pages. Reference string:
Can cause Belady’s anomaly.
1 2 3 1 4
Frames = 3
2. Optimal Page
Before page 4 arrives:
Replacement
[1 2 3]
Explanation
Page 2 was used least recently, so:
Replaces the page that will not be
[1 4 3]
used for the longest time in the
future.
Advantages:
Gives the minimum possible page
faults.
Performs better than FIFO.
Uses past behavior for prediction.
Example
Disadvantages:
Reference string:
Requires tracking page usage.
1 2 3 4 1
Advantages:
Best performance. Memory Management in
Disadvantages: Linux
Future requests are unknown, so it is Definition
difficult to implement.
Memory Management in Linux is the
function of the Linux Operating System that
manages the allocation, usage, and release of
4. LFU (Least Frequently main memory (RAM) for processes and
system operations.
Used)
Linux uses efficient memory management
Explanation techniques to provide better performance
and support multitasking.
Replaces the page that has not been
used for the longest time in the past.
The name used to identify a file. Stores information about the user
Example: [Link] who owns the file.
2. Identifier
10. Move
1. Create
3. Read
Operations:
Types of Directory
Read at position
Write at position Structures
Seek
1. Single-Level Directory
Advantages:
All files are stored in one single
Faster access to specific data. directory.
Suitable for databases. Simple but causes naming conflicts.
Disadvantages: Example: