0% found this document useful (0 votes)
2 views7 pages

Thread in Operating System

A thread is a lightweight process that runs within a process, sharing resources like code and data, while having its own control block. Threads improve application performance, responsiveness, and resource sharing, but introduce complexity and debugging challenges. There are two types of threads: User-Level Threads (ULTs), managed in user space, and Kernel-Level Threads (KLTs), managed by the operating system, each with distinct advantages and disadvantages.

Uploaded by

240181601076
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)
2 views7 pages

Thread in Operating System

A thread is a lightweight process that runs within a process, sharing resources like code and data, while having its own control block. Threads improve application performance, responsiveness, and resource sharing, but introduce complexity and debugging challenges. There are two types of threads: User-Level Threads (ULTs), managed in user space, and Kernel-Level Threads (KLTs), managed by the operating system, each with distinct advantages and disadvantages.

Uploaded by

240181601076
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

Thread in Operating System


A thread is a single sequence stream within a process. Threads are also


called lightweight processes as they possess some of the properties of
processes. Each thread belongs to exactly one process.
 In an operating system that supports multithreading, a process can
consist of many threads.
 All threads belonging to the same process share code section, data
section, and OS resources (e.g. open files and signals), but each thread
has its own (thread control block) - thread ID, program counter, register
set, and a stack.

Why Do We Need Threads (and Their Benefits)


Threads are needed in modern operating systems and applications because
they:
 Improve Application Performance: Threads can run in parallel, making
programs execute faster.
 Increase Responsiveness: Even if one thread is busy, another can
return results or handle user actions immediately.
 Enable Concurrency: Multiple things can happen at once, such as
background saving, formatting, and user input in Microsoft Word or
Google Docs.
 Simplify Communication: Since threads share the same memory space,
they can directly exchange data without special inter-process
communication mechanisms.
 Support Prioritization: Like processes, threads can have priorities; the
highest-priority thread gets scheduled first.
 Efficient Context Switching: Switching between threads takes less time
than switching between processes because threads use the same
address space.
 Better Multiprocessor Utilization: Threads from the same process can
run on different processors simultaneously, speeding up execution.
 Resource Sharing: Threads within a process share code, data, and files,
which saves resources.
 Higher Throughput: Dividing a process into multiple threads allows more
jobs to finish per unit time.
 Synchronization Support: Since threads share resources,
synchronization tools (locks, semaphores, etc.) ensure safe access to
shared data.
 Thread Management: Each thread has a Thread Control Block
(TCB) that stores its state, register values, and scheduling info for context
switching.
Components of Threads
These are the basic components of the Operating System.
 Stack Space: Stores local variables, function calls, and return addresses
specific to the thread.
 Register Set: Hold temporary data and intermediate results for the
thread's execution.
 Program Counter: Tracks the current instruction being executed by the
thread.
Types of Thread in Operating System
Threads are of two types. These are described below.
 User Level Thread
 Kernel Level Thread
Threads
User-Level Threads (ULTs)
 Managed entirely in user space using a thread library; the kernel is
unaware of them.
 Switching between ULTs is fast since only program counter, registers,
and stack need to be saved/restored.
 Do not require system calls for creation or management, making them
lightweight.
 Blocking Limitation: If one thread makes a blocking system call, the
entire process (all threads) is blocked.
 Scheduling is done by the application itself, which may not be as efficient
as kernel-level scheduling.
 Cannot fully utilize multiprocessor systems because the kernel schedules
processes, not individual user-level threads.
Kernel-Level Threads (KLTs)
 Managed directly by the operating system kernel; each thread has an
entry in the kernel’s thread table.
 The kernel schedules each thread independently, allowing true parallel
execution on multiple CPUs/cores.
 Handles blocking system calls efficiently; if one thread blocks, the kernel
can run another thread from the same process.
 Provides better load balancing across processors since the kernel
controls all threads.
 Context switching is slower compared to ULTs because it requires
switching between user mode and kernel mode.
 Implementation is more complex and requires frequent interaction with the
kernel.
 Large numbers of threads may add extra load on the kernel scheduler,
potentially affecting performance.
Threading Issues
 The fork() and exec() System Calls : The semantics of the fork() and
exec() system calls change in a multithreaded program. If one thread in a
program calls fork(), does the new process duplicate all threads, or is the
new process single-threaded? Some UNIX systems have chosen to have
two versions of fork(), one that duplicates all threads and another that
duplicates only the thread that invoked the fork() system call. The exec()
system , That is, if a thread invokes the exec() system call , the program
specified in the parameter to exec() will replace the entire process—
including all threads.
 Signal Handling : A signal is used in UNIX systems to notify a process
that a particular event has occurred. A signal may be received either
synchronously or asynchronously depending on the source of and the
reason for the event being signaled. All signals, whether synchronous or
asynchronous, follow the same pattern:1. A signal is generated by the
occurrence of a particular event.2. The signal is delivered to a process.3.
Once delivered, the signal must be handled. A signal may be handled by
one of two possible handlers: 1. A default signal handler .2. A user-
defined signal handler. Every signal has a default signal handler that the
kernel runs when handling that signal. This default action can be
overridden by a user-defined signal handler that is called to handle the
signal.
 Thread Cancellation : Thread cancellation involves terminating a thread
before it has completed. For example, if multiple threads are concurrently
searching through a database and one thread returns the result, the
remaining threads might be canceled. Another situation might occur when
a user presses a button on a web browser that stops a web page from
loading any further. Often, a web page loads using several threads—each
image is loaded in a separate thread. When a user presses the stop
button on the browser, all threads loading the page are canceled. A
thread that is to be canceled is often referred to as the target thread.
Cancellation of a target thread may occur in two different scenarios:1.
Asynchronous cancellation. One thread immediately terminates the target
thread.2. Deferred cancellation. The target thread periodically checks
whether it should terminate, allowing it an opportunity to terminate itself in
an orderly fashion.
 Thread-Local Storage : Threads belonging to a process share the data
of the process. Indeed, this data sharing provides one of the benefits of
multithreaded programming. However, in some circumstances, each
thread might need its own copy of certain data. We will call such data
thread-local storage (or TLS.) For example, in a transaction-processing
system, we might service each transaction in a separate thread.
Furthermore, each transaction might be assigned a unique identifier. To
associate each thread with its unique identifier, we could use thread-local
storage.
 Scheduler Activations : One scheme for communication between the
user-thread library and the kernel is known as scheduler activation. It
works as follows: The kernel provides an application with a set of virtual
processors (LWPs), and the application can schedule user threads onto
an available virtual processor.
Advantages of Threading
 Responsiveness: A multithreaded application increases responsiveness
to the user.
 Resource Sharing: Resources like code and data are shared between
threads, thus allowing a multithreaded application to have several threads
of activity within the same address space.
 Increased Concurrency: Threads may be running parallelly on different
processors, increasing concurrency in a multiprocessor machine.
 Lesser Cost: It costs less to create and context-switch threads than
processes.
 Lesser Context-Switch Time: Threads take lesser context-switch time
than processes.
Disadvantages of Threading
 Complexity: Threading can make programs more complicated to write
and debug because threads need to synchronize their actions to avoid
conflicts.
 Resource Overhead: Each thread consumes memory and processing
power, so having too many threads can slow down a program and use up
system resources.
 Difficulty in Optimization: It can be challenging to optimize threaded
programs for different hardware configurations, as thread performance
can vary based on the number of cores and other factors.
 Debugging Challenges: Identifying and fixing issues in threaded
programs can be more difficult compared to single-threaded programs,
making troubleshooting complex.
Comment
A

Aniket_Dusey
Follow

Difference Between Process and Thread


The primary difference is that threads within the same process run in a
shared memory space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a
result, threads share with other threads their code section, data section, and
OS resources (like open files and signals). But, like a process, a thread has
its own program counter (PC), register set, and stack space.

Process Vs Thread
The table below represents the difference between process and thread.

Process Thread

Program in execution Part of a process

Takes more time to create &


Takes less time to create & terminate
terminate

Context switching is slow Context switching is fast

Heavyweight Lightweight

Has its own memory space Shares memory with other threads

Less efficient communication More efficient communication

Blocking one process doesn’t affect Blocking a user-level thread may block all
Process Thread

others

Uses system calls Created using APIs (may not need OS call)

Has its own PCB, stack, address Shares PCB & address space, has own
space TCB & stack

Does not share data Shares data with other threads

You might also like