0% found this document useful (0 votes)
5 views13 pages

OS_Notes_Class5_Class6

This document provides comprehensive notes on operating systems, focusing on process management concepts such as context switching, Process Control Block (PCB), and threads. It explains the definitions, structures, advantages, and disadvantages of context switching and PCB, as well as the differences between processes and threads. Additionally, it covers multithreading concepts, benefits, types of threads, and various multithreading models, making it a valuable resource for exam preparation in operating systems.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

OS_Notes_Class5_Class6

This document provides comprehensive notes on operating systems, focusing on process management concepts such as context switching, Process Control Block (PCB), and threads. It explains the definitions, structures, advantages, and disadvantages of context switching and PCB, as well as the differences between processes and threads. Additionally, it covers multithreading concepts, benefits, types of threads, and various multithreading models, making it a valuable resource for exam preparation in operating systems.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OPERATING SYSTEMS

Exam-Ready Notes
Process State Transitions • Context Switching • PCB • Threads • Multithreading

Covers Class-5 & Class-6 Lecture Content

Satyapriya Tripathy
MCA, 3rd Semester — Reg. No. 2505260021
RIMS Cybernetics, Rourkela (BPUT), Session 2025–27
UNIT 1

Process State Transitions, PCB & Context Switching


Class-5 — Core process management concepts for OS viva & written exams

1. Context Switching
★ Key Point (frequently asked)
Context Switching = saving the state of a running process + restoring the saved state of another process, so the
CPU can resume that process exactly where it left off. It is the mechanism that makes multitasking possible.

Formal definition: Context switching is the process of saving the context (state) of the currently running process
and restoring the saved context of another process so that CPU execution can continue from where that process
previously stopped. The operating system performs context switching to achieve multitasking.

1.1 What is “Context”?


Context means the complete execution environment of a process at a given instant. It includes:
• Program Counter (PC)
• CPU Registers
• Stack Pointer
• Process State
• Memory Information
• Scheduling Information

→ All of this information is stored in the Process Control Block (PCB).

1.2 Why Context Switching is Needed


• Multitasking
• Time sharing
• Process scheduling
• Interrupt handling
• Efficient CPU utilization

1.3 Steps in Context Switching


1. CPU is executing Process A.
2. A timer interrupt occurs (time quantum ends).
3. OS saves Process A's context into its PCB — Program Counter, Registers, Stack Pointer, Process State.
4. Scheduler selects Process B (next process to run).
5. OS loads Process B's context from its PCB.
6. CPU starts executing Process B from its saved point.
CPU
|
V
+--------------------+
| Process A Runs |
+--------------------+
| Timer Interrupt
V
Save Context ---> PCB of Process A
|
V
Scheduler
|
V
Load PCB of Process B
|
V
+--------------------+
| Process B Runs |
+--------------------+

1.4 What Happens During a Context Switch


Saved (from outgoing process) Restored (for incoming process)

CPU Registers Registers

Program Counter Program Counter

Stack Pointer Memory Information

Process State Stack Pointer

⚠ Common Trap / Exam Confusion


Students often write “memory information” as something saved during a switch — in this syllabus's list it appears
only under Restored, not Saved. Also, note Process State is saved but not separately listed under Restored (it's
implicit as the new state becomes Running).

1.5 Context Switching Overhead


Context switching is pure overhead — it does NOT perform any useful work by itself; it only changes which
process is running.
Sources of overhead:
• Saving register values
• Loading register values
• Cache invalidation
• Translation Lookaside Buffer (TLB) effects
• Scheduler execution time
⇒ Excessive/too-frequent context switching can reduce overall system performance (this is a favourite one-mark
/ fill-in-the-blank question).

1.6 Advantages & Disadvantages of Context Switching


Advantages Disadvantages

Enables multitasking Introduces execution overhead

Improves CPU utilization Consumes CPU time

Supports time-sharing systems May reduce cache efficiency

Provides quick response to users Frequent switching lowers performance

Allows fair CPU allocation

2. Process Control Block (PCB)


★ Key Point (frequently asked)
The PCB (also called a Task Control Block) is a data structure maintained by the OS for every process. It stores all
information needed to manage and resume that process — essentially the process's “identity card” in the OS.

2.1 PCB Structure

+------------------------------------+
| Process ID (PID) |
+------------------------------------+
| Process State |
+------------------------------------+
| Program Counter |
+------------------------------------+
| CPU Registers |
+------------------------------------+
| CPU Scheduling Information |
+------------------------------------+
| Memory Management Information |
+------------------------------------+
| Accounting Information |
+------------------------------------+
| I/O Status Information |
+------------------------------------+

2.2 Detailed Fields Stored in PCB


Field Details / Example

1. Process ID (PID) Unique identification number. Example: PID = 2054

2. Process State Current state: Ready, Running, Waiting, Terminated

3. Program Counter Address of the next instruction to execute. Example: PC = 4050

4. CPU Registers General Registers, Stack Pointer, Base Register, Index Register — restored
Field Details / Example

during context switching

5. CPU Scheduling Info Priority, Queue Pointer, Time Slice

6. Memory Mgmt. Info Base Register, Limit Register, Page Tables, Segment Tables

7. Accounting Info Used for billing/monitoring: CPU time used, Job number, Process number,
Time limits

8. I/O Status Info Open files, Devices allocated, Pending I/O requests

2.3 Lifecycle of a PCB

Process Created
|
PCB Created
|
Execution
|
Context Switching
|
PCB Updated
|
Process Ends
|
PCB Deleted

2.4 Advantages & Limitations of PCB


Advantages Limitations

Efficient process management Consumes memory

Supports multitasking Frequent updates increase overhead

Enables scheduling Large number of processes increases management cost

Preserves execution state

Facilitates context switching

⚠ Common Trap / Exam Confusion


PCB is a data structure maintained by the OS, NOT by the process itself, and it is NOT visible to user programs. A
common wrong answer is calling PCB “part of the process's own memory” — it actually resides in kernel/OS
space.

3. Exam-Focused Q&A (Short Answer / Part A style)


Q1. Define a process.
A process is a program in execution — an active entity with its own PCB, memory space, and current state
(Ready/Running/Waiting/Terminated), as opposed to a program which is a passive set of instructions on disk.
Q2. Explain the five process states.
New (process being created) → Ready (waiting for CPU allocation) → Running (instructions being executed) →
Waiting/Blocked (waiting for an I/O or event) → Terminated (execution finished). A running process can also go
back to Ready (time-slice expiry) or move to Waiting (I/O request), then back to Ready once the event
completes.

Q3. Differentiate between Ready and Waiting states.


A process in the Ready state has all resources except the CPU and is waiting only for CPU allocation by the
scheduler. A process in the Waiting (Blocked) state is waiting for some event or I/O completion (e.g., disk read)
and cannot run even if the CPU is given to it.

Q4. What is PCB?


PCB (Process Control Block) is a data structure maintained by the OS, one per process, that stores all information
required to manage that process — PID, state, program counter, registers, scheduling info, memory info,
accounting info, and I/O status.

Q5. List any six fields stored in PCB.


Process ID, Process State, Program Counter, CPU Registers, CPU Scheduling Information, Memory Management
Information (also: Accounting Information, I/O Status Information).

Q6. Define context switching.


Context switching is the mechanism of saving the state of the currently running process into its PCB and loading
the saved state of another process from its PCB, so the CPU can resume that other process from where it
stopped.

Q7. Why is PCB required?


Because the CPU handles multiple processes and each process must be resumed exactly where it stopped. The
PCB stores this state so nothing is lost during context switching, enabling multitasking and scheduling.

Q8. What happens during a context switch?


The OS saves the outgoing process's registers, program counter, stack pointer, and state into its PCB; the
scheduler picks the next process; the OS then restores that process's registers, program counter, memory
information, and stack pointer from its PCB and resumes execution.

4. Descriptive / Long Answer Prep (Part B style)


Process State Transition Diagram

admitted
New -------------------> Ready
| ^
dispatch | | interrupt
V |
Running
/ \
exit / \ I/O or event wait
V V
Terminated Waiting
|
I/O or event completion
|
V
(back to Ready)

Explanation: A process is admitted into Ready. The scheduler dispatches it to Running. From Running it can be
interrupted back to Ready (time quantum expiry), moved to Waiting (I/O/event wait), or moved to Terminated
(exit). From Waiting, once the event/I/O completes, it returns to Ready — never directly to Running.

Structure of PCB with examples


Answer using the PCB field table above (Section 2.2), citing example values such as PID = 2054 and PC = 4050 to
show applied understanding rather than a bare list.

Steps in Context Switching (descriptive)


Write out the 6-step sequence from Section 1.3, and optionally draw the block diagram from Section 1.3 to earn
diagram marks.

Why context switching introduces overhead


Explain that a context switch does no useful computation itself; time is spent saving/restoring registers, plus
indirect costs like cache invalidation and TLB flushing, and the scheduler's own decision-making time. Conclude
that too many switches (thrashing at the CPU level) reduce throughput.

Compare Process, Program, and Thread


Aspect Program Process Thread

Nature Passive set of instructions Active, executing instance Smallest unit of CPU execution
on disk of a program inside a process

Memory No memory allocated at Has its own independent Shares memory of its parent
rest memory space process

Creation cost N/A Expensive (heavyweight) Cheap (lightweight)

State info None Maintained via PCB Own PC, registers, stack; rest
shared
UNIT 2

Threads, Multithreading & Thread Models


Class-6 — Threads, benefits, types, and multithreading models for OS viva & written exams

1. Introduction to Threads
★ Key Point (frequently asked)
A thread is the smallest unit of CPU execution within a process — often called a lightweight process (LWP)
because it needs fewer resources than a full process.

A process may consist of one or more threads executing concurrently while sharing the same process resources.
Threads of the same process SHARE:
• Program code (Text Section)
• Data Section
• Heap Memory
• Open Files
• Signals
Each thread maintains its OWN:
• Program Counter (PC)
• Register Set
• Stack

So every thread executes independently while sharing the resources of its parent process.

1.1 Process vs Thread


Process Thread

Heavyweight Lightweight

Own memory space Shares memory of process

Expensive to create Faster to create

Context switch is slow Context switch is fast

Independent Dependent on parent process

Communication through IPC Direct communication using shared memory

1.2 Structure of a Thread

-------------------------
Thread
-------------------------
Thread ID
Program Counter
Registers
Stack
-------------------------
Shared Among Threads
-------------------------
Code | Data | Heap | Files
-------------------------

1.3 Single-Threaded vs Multi-Threaded Process

Single-Threaded Process Multi-Threaded Process


+------------------+ +----------------------------+
| Code | | Code | Data | Heap |
| Data | +----------------------------+
| Heap | Thread1 Thread2 Thread3
| Stack | Stack Stack Stack
| One Thread | Registers Registers Registers
+------------------+ PC PC PC

Single-threaded example: Notepad, a simple calculator — only one activity happens at a time. In a multi-
threaded process, all threads share Code/Data/Heap but each has its own Stack, Registers, and PC.

2. Concept of Multithreading
★ Key Point (frequently asked)
Multithreading is the capability of a process to execute multiple threads concurrently within the same process,
instead of creating several separate processes to perform different tasks simultaneously.

2.1 Working Principle (Example)


Example: An online shopping application splits work across threads that all share the same application data:
• Thread 1 — User Login
• Thread 2 — Product Search
• Thread 3 — Payment Processing
• Thread 4 — Notification

2.2 Real-Life Analogy — Restaurant


Process = Restaurant. Threads = Taking Orders, Cooking, Billing, Cleaning. All workers (threads) share the
Kitchen, Food, and Equipment (shared resources), while each performs a different task independently — exactly
like threads sharing Code/Data/Heap but running independent instruction streams.

3. Benefits of Threads
Benefit Explanation

1. Responsiveness Application keeps running even if one thread is blocked

2. Resource Sharing Threads share Code, Data, Memory, Files

3. Economy Creating threads is much cheaper than creating processes


Benefit Explanation

4. Faster Context Switching Faster since threads share the same address space

5. Better CPU Utilization Multiple threads keep the CPU busy

6. Scalability On multi-core processors, threads run on different cores simultaneously

7. Parallelism Multiple threads execute simultaneously on multiple processors

⚠ Common Trap / Exam Confusion


Concurrency vs Parallelism: Concurrency means multiple threads make progress in an interleaved manner
(possible even on a single core). Parallelism means threads literally execute at the same instant on different
cores. Multithreading enables concurrency always, but true parallelism only on multi-core hardware —
examiners frequently test this distinction.

3.1 Applications of Multithreading


• Web browsers
• Video streaming
• Operating systems
• Databases
• Android applications
• Banking systems
• Compiler design
• Online gaming
• Cloud computing
• Machine Learning

4. Types of Threads
Operating systems mainly support two types of threads: User-Level Threads (ULT) and Kernel-Level Threads
(KLT).

4.1 User-Level Threads (ULT)


Managed entirely by a user-level thread library; the OS kernel is not aware of them. Examples: POSIX Pthreads
(user-space implementations), Green Threads, GNU Portable Threads.

Application
Thread1 Thread2 Thread3
|
Thread Library
|
Operating System

Characteristics Advantages Disadvantages

Managed in user space Fast Blocking affects all threads

Fast creation & switching Low overhead Limited parallel execution on


multiple CPUs (in many
implementations)

No kernel support required Portable / easy implementation

A blocking system call in one thread


can block the entire process

4.2 Kernel-Level Threads (KLT)


Directly managed and scheduled by the OS kernel. Examples: Windows Threads, Linux Kernel Threads, Solaris
Threads.

Application
Thread1 Thread2 Thread3
|
Operating System Kernel
|
CPU

Characteristics Advantages Disadvantages

Managed by kernel Better concurrency Thread creation slower than ULT

Kernel schedules each thread True parallelism Higher context-switch overhead


independently (kernel involvement)

Better multi-core support Blocking one thread doesn't stop


others

True parallel execution Suitable for modern OS

4.3 User-Level vs Kernel-Level Threads — Comparison


Feature User-Level Threads Kernel-Level Threads

Managed By User thread library Operating system kernel

Kernel Awareness No Yes

Creation Speed Fast Slower

Context Switch Fast Slower

Blocking System Call Blocks entire process Blocks only the calling thread

Multi-core Support Limited (implementation-dependent) Excellent

Overhead Low Higher

Scheduling User library Kernel scheduler


5. Multithreading Models (ULT ↔ KLT mapping)
Model Mapping Key Trade-off

Many-to-One Many user threads → 1 kernel thread Efficient, but one blocking call blocks all
threads; no true parallelism

One-to-One Each user thread → 1 kernel thread Enables parallelism; uses more kernel
resources per thread

Many-to-Many Many user threads multiplexed over a set of Combines flexibility with parallel execution
kernel threads — best of both worlds

⚠ Common Trap / Exam Confusion


A very common MCQ/fill-blank: “The ______ model maps one user thread to one kernel thread” → One-to-One.
“The ______ model maps multiple user threads to multiple kernel threads” → Many-to-Many. Don't confuse
Many-to-Many with Many-to-One.

6. Multithreading vs Multiprocessing
Multithreading Multiprocessing

Shares memory Separate memory spaces

Faster communication IPC required

Lower overhead Higher overhead

Faster thread creation Slower process creation

Efficient resource sharing Resource duplication

7. Exam-Focused Q&A
Q9. A thread is the __________ unit of CPU execution.
smallest

Q10. Threads are also called __________ processes.


lightweight

Q11. Each thread has its own __________ and program counter.
stack (and register set)

Q12. Threads belonging to the same process share the __________ memory.
heap (also code, data, open files, signals)

Q13. __________ threads are managed by a thread library.


User-level

Q14. __________ threads are managed by the operating system kernel.


Kernel-level

Q15. Multithreading improves the __________ of an application.


responsiveness

Q16. Thread switching is generally __________ than process switching.


faster

Q17. The __________ model maps one user thread to one kernel thread.
One-to-One

Q18. The __________ model maps multiple user threads to multiple kernel threads.
Many-to-Many

8. Descriptive / Long Answer Prep


Explain Threads and their benefits with examples
Define thread → explain what is shared vs. per-thread → state 3–4 benefits (Responsiveness, Economy,
Scalability, Parallelism) with the online-shopping or restaurant example → conclude with an application area
(browsers, banking, ML).

Differentiate ULT and KLT with diagrams


Draw both stack diagrams (Sections 4.1 and 4.2), then reproduce the comparison table (Section 4.3) — this
combination (diagram + table) is what fetches full marks in BPUT-pattern descriptive answers.

Explain the Multithreading Models


Describe Many-to-One, One-to-One, and Many-to-Many with a simple mapping diagram for each (a small box of
user threads connecting via lines to a smaller/equal/multiplexed set of kernel-thread boxes), and state one
advantage and one drawback per model.

End of Notes — Class 5 & Class 6 (Operating Systems, MCA 3rd Semester, BPUT)

You might also like