Threads in Operating System | DIT Notes

Threads in Operating System | DIT Notes Hub
DIT Operating System 3.5 - Threads
DIT • SEMESTER 1 • OPERATING SYSTEM • ARTICLE 3.5

Threads

A simple, student-friendly and exam-oriented guide to threads, multithreading, thread components, shared resources, and types of threads.

Unit 3ProcessesArticle 3.5 ThreadsEasy Notes 6 Short1 Long10 MCQs

Quick Access

Jump directly to any major part of Article 3.5.

3.5.1 What is a Thread?

The basic idea of a thread in an Operating System.

A thread is the smallest unit of execution within a process. A process can contain one or more threads, and each thread can perform a particular task.

In simple words: A thread is a lightweight unit of execution inside a process.

Example: A web browser may use separate threads for loading a webpage, playing a video, handling user input, and downloading files.

FIGURE 1 • THREAD BASICS
One Process Can Contain Multiple Threads
ProcessA program in execution.
→
Thread 1Example: Load a webpage.
+
Thread 2 / 3 / ...Other tasks such as video, input, or downloads.
Caption: A process may contain one or more threads, with each thread performing a particular task.

3.5.2 Process and Thread

Understand the simple relationship between a process and its threads.

A process is a program in execution, while a thread is an execution path within a process.

ProcessThread
Program in execution.Unit of execution within a process.
Has its own address space.Shares address space with threads of the same process.
Generally requires more resources.Generally requires fewer resources.
Communication between processes can be more complex.Threads can communicate through shared process resources.
Creation is relatively expensive.Creation is generally faster.
Can contain one or more threads.Exists within a process.
Easy point to remember: Process is the larger unit; thread is a smaller execution unit inside that process.

3.5.3 Single-Threaded and Multithreaded Process

Two simple ways to understand how many threads a process contains.

Single-Threaded Process

A single-threaded process contains only one thread of execution. The process performs one sequence of instructions at a time.

Example: A simple program that performs one task from beginning to end can be considered single-threaded.

Multithreaded Process

A multithreaded process contains two or more threads that can execute different tasks.

FIGURE 2 • SINGLE VS MULTI
Single Thread vs. Multiple Threads
Single-ThreadedProcess → Thread 1
One sequence of instructions at a time.
MultithreadedProcess → Thread 1 + Thread 2 + Thread 3
Different tasks can make progress within the same process.
Caption: The source uses a media application as an example: one thread can play audio, another can display video, and another can handle user input.

3.5.4 Components of a Thread

Each thread normally has its own execution-related information.

  1. Thread ID: Uniquely identifies a thread within its process.
  2. Program Counter: Stores the address of the next instruction to be executed by the thread.
  3. CPU Registers: Store the CPU register values required during execution.
  4. Stack: Stores function calls, local variables, and temporary data.
FIGURE 3 • THREAD COMPONENTS
What Each Thread Keeps for Its Own Execution
Thread IDIdentifies the thread.
Program CounterNext instruction address.
CPU RegistersCurrent execution values.
StackCalls, local data, temporary data.
Caption: These items allow each thread to maintain its own execution state.

3.5.5 Shared Process Resources

Threads in the same process share important process resources.

Threads belonging to the same process generally share:

  • Program code
  • Data section
  • Heap
  • Open files
  • Other process resources
Why is this useful? Sharing makes communication between threads easier than communication between separate processes.

3.5.6 Advantages of Threads

Why threads are useful in applications.

1. ResponsivenessA program can continue responding to the user while another thread performs a time-consuming task.
2. Resource SharingThreads of the same process share code, data, and other resources.
3. EconomyCreating and managing threads generally requires fewer resources than creating separate processes.
4. Better PerformanceOn a multicore processor, multiple threads may execute in parallel for suitable applications.
5. Improved Application StructureComplex applications can be divided into smaller tasks using separate threads.

3.5.7 Types of Threads

The source classifies threads into user-level and kernel-level threads.

1. User-Level Threads

User-level threads are managed by a user-level thread library rather than directly by the operating system kernel.

  • Advantages: Fast to create and manage; less overhead; can be implemented without extensive kernel support.
  • Limitation: If one thread performs a blocking operation, depending on the implementation, the entire process may be affected.

2. Kernel-Level Threads

Kernel-level threads are managed directly by the Operating System. The OS schedules these threads and manages their execution.

  • Advantages: Better support for parallel execution; one blocked thread does not necessarily block other threads of the same process; suitable for multicore systems.
  • Limitation: Thread management may involve more overhead than user-level threads.
FIGURE 4 • THREAD TYPES
User-Level vs. Kernel-Level Threads
User-LevelManaged by a user-level thread library.
Fast and less overhead.
Blocking may affect the process depending on implementation.
Kernel-LevelManaged by the OS.
Better support for parallel execution and multicore systems.
May have more management overhead.
Caption: The source distinguishes the two types mainly by who manages the threads and their management overhead.

3.5.8 Process vs. Thread

A quick comparison for exam preparation.

ProcessThread
Program in execution.Unit of execution within a process.
Has its own address space.Shares address space with threads of the same process.
Generally requires more resources.Generally requires fewer resources.
Communication can be more complex.Can communicate through shared process resources.
Creation is relatively expensive.Creation is generally faster.
Can contain one or more threads.Exists within a process.
Exam Tip: Remember the basic idea: a process is the container, while threads are execution units inside that process.

Quick Revision

Read these points once before attempting the questions.

  • A thread is the smallest unit of execution within a process.
  • A process can contain one or more threads.
  • Threads of the same process share code, data, heap, open files, and other process resources.
  • Each thread normally has its own Thread ID, Program Counter, CPU registers, and stack.
  • A single-threaded process has one thread; a multithreaded process has two or more threads.
  • Important advantages include responsiveness, resource sharing, economy, better performance, and improved application structure.
  • User-level threads are managed by a user-level thread library.
  • Kernel-level threads are managed directly by the Operating System.
Exam Focus
  • Learn the definition of a thread.
  • Understand single-threaded vs. multithreaded process.
  • Remember the four main thread components.
  • Remember which resources are shared and which are private to a thread.
  • Know the basic difference between user-level and kernel-level threads.

6 Short Questions & Answers

Simple questions for quick exam preparation.

Q1. What is a thread?

A thread is the smallest unit of execution within a process. It is a lightweight unit of execution inside a process.

Q2. What is a single-threaded process?

A single-threaded process contains only one thread of execution and performs one sequence of instructions at a time.

Q3. What is a multithreaded process?

A multithreaded process contains two or more threads that can execute different tasks within the same process.

Q4. Name the resources shared by threads of the same process.

Threads generally share program code, data, heap memory, open files, and other process resources.

Q5. What is a user-level thread?

A user-level thread is managed by a user-level thread library rather than directly by the operating system kernel.

Q6. What is a kernel-level thread?

A kernel-level thread is managed directly by the operating system. The OS schedules the thread and manages its execution.

1 Long Question & Answer

One focused long question for exam practice.

Q1. What is a thread? Explain single-threaded and multithreaded processes, components of a thread, and the advantages of threads.

A thread is the smallest unit of execution within a process. A process can contain one or more threads, and each thread can perform a particular task.

Single-threaded process: It contains only one thread of execution and performs one sequence of instructions at a time.

Multithreaded process: It contains two or more threads that can execute different tasks. For example, a media application may use separate threads for playing audio, displaying video, and handling user input.

Main components of a thread:

  • Thread ID
  • Program Counter
  • CPU Registers
  • Stack

Advantages: Threads improve responsiveness, allow resource sharing, generally require fewer resources than separate processes, can improve performance on multicore processors, and help divide complex applications into smaller tasks.

10 Important MCQs

Open each question, choose an option, then use “Show Answer”.

1. A thread is the smallest unit of:
A) Storage
B) Execution
C) File management
D) Memory allocation
Show Answer
Correct Answer: B) Execution
2. A process may contain:
A) Only one thread
B) Only two threads
C) One or more threads
D) No threads
Show Answer
Correct Answer: C) One or more threads
3. Which resource is normally private to each thread?
A) Program code
B) Heap
C) Open files
D) Stack
Show Answer
Correct Answer: D) Stack
4. Which resource is generally shared by threads of the same process?
A) Stack
B) Program Counter
C) Program code
D) CPU registers
Show Answer
Correct Answer: C) Program code
5. A process with two or more threads is called:
A) Single-threaded
B) Multithreaded
C) Blocked
D) Terminated
Show Answer
Correct Answer: B) Multithreaded
6. Which is an advantage of multithreading?
A) More resource duplication
B) Responsiveness
C) Slower task handling
D) Separate address space for every thread
Show Answer
Correct Answer: B) Responsiveness
7. User-level threads are managed by:
A) Only the CPU
B) A user-level thread library
C) The hard disk
D) The printer
Show Answer
Correct Answer: B) A user-level thread library
8. Kernel-level threads are managed directly by:
A) The operating system
B) The browser
C) The keyboard
D) The compiler only
Show Answer
Correct Answer: A) The operating system
9. Which type of thread is generally fast to create and manage?
A) User-level thread
B) Kernel-level thread
C) Process
D) Device thread
Show Answer
Correct Answer: A) User-level thread
10. Threads of the same process can communicate more easily because they:
A) Have separate address spaces
B) Share process resources
C) Never use memory
D) Cannot run different tasks
Show Answer
Correct Answer: B) Share process resources

Final Exam Reminder

Last-minute revision checklist.

No comments