0% found this document useful (0 votes)
3 views9 pages

Thread

The document provides an overview of threads, highlighting their role as lightweight processes that enable concurrent execution within a program. It explains the advantages of threads, such as reduced overhead, improved performance, and efficient communication, as well as the differences between user-level and kernel-level threads. Additionally, it discusses various multithreading models, including many-to-many, many-to-one, and one-to-one relationships, and their implications for system performance and resource management.
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)
3 views9 pages

Thread

The document provides an overview of threads, highlighting their role as lightweight processes that enable concurrent execution within a program. It explains the advantages of threads, such as reduced overhead, improved performance, and efficient communication, as well as the differences between user-level and kernel-level threads. Additionally, it discusses various multithreading models, including many-to-many, many-to-one, and one-to-one relationships, and their implications for system performance and resource management.
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

1. A thread is a flow of execution through the process code. It performs tasks within a
program and helps the program run multiple operations at the same time.
Each thread contains:
 Program Counter → keeps track of the next instruction
 Registers → store temporary working data
 Stack → stores execution history and function calls
So, a thread can independently perform its own task.

2. A thread shares information with peer threads. Threads belonging to the same process
share: Code segment, Data segment, Open files. Because resources are shared,
communication between threads becomes fast and efficient.

3. If one thread modifies shared data, the other threads of the same process can access
the updated data immediately. Example: If one thread updates a document, another
thread can display the updated version instantly.

4. A thread is called a lightweight process. Because a thread requires: Less memory,


Less CPU overhead, Fewer resources than a normal process.

5. Threads improve application performance through parallelism (Threads allow


different tasks to execute simultaneously)
Example:
In a web browser:
 One thread loads webpages
 Another thread downloads files
 Another thread plays videos
This improves speed and responsiveness.

6. Threads reduce operating system overhead: Creating and managing threads is easier
and faster than managing separate processes.

7. Each thread belongs to exactly one process and no thread can exist outside a process.
A process may contain: Single thread or Multiple threads. Each thread represents a
separate flow of control (performs its own independent task)
Example:
In a music application:
 One thread plays audio
 One thread displays lyrics
 One thread handles internet streaming
All threads work separately but inside the same application.

8. Threads are used in network servers and web servers.


9. Threads support parallel execution on multiprocessors. In multicore systems, different
threads can run on different CPU cores simultaneously. Advantages: Faster
processing, better performance, Efficient multitasking.

10. The following figure shows single threaded and multi-threaded process.

Difference between Process and Thread

Process Thread

When a program is under execution, then it A segment of a process is known as thread.


is known as a process.

It consumes maximum time to stop. It consumes minimum time to stop.

It needs more time for work and It needs less time for work and conception.
conception.

Context switching takes maximum time Here, context switching takes minimum time.
here.
It is not that effective in terms of It is effective in terms of communication.
communication.

It takes more resources. It takes less resources.

It is a heavy weight process. It is a light weight process.

If one process is obstructed then it will not If one thread is obstructed then it will affect
affect the operation of another process. the execution of another process.

Life Cycle of Thread


1. Born State: A thread that has just been created.
2. Ready State: The thread is waiting for the processor (CPU).
3. Running: The System assigns the processor to the thread means that the thread is being
executed.
4. Blocked State: The thread is waiting for an event to occur or waiting for an I/O device.
5. Sleep: A sleeping thread becomes ready after the designated sleep time expires.
6. Dead: The execution of the thread is finished.

Advantages of Threads
1. Threads minimize context switching time: Switching between threads takes less
time compared to switching between processes because threads share the same
memory and resources. This improves system performance.
2. Threads provide concurrency within a process: Multiple threads can run
simultaneously within the same process, allowing different tasks to be performed at
the same time.
3. Threads enable efficient communication: Since threads share the same memory
space, communication between them is faster and easier than communication between
separate processes.
4. Threads are economical to create and manage: Creating and managing threads
requires fewer system resources and less memory compared to processes, making
them more efficient.
5. Threads improve utilization of multiprocessor architectures; In multiprocessor or
multicore systems, multiple threads can run on different processors simultaneously,
increasing efficiency and improving overall performance.

Types of Thread
Threads are implemented in following two ways –
 User Level Threads − User managed threads.
 Kernel Level Threads − Operating System managed threads acting on kernel, an
operating system core.
User Level Threads
1. In this case, the thread management kernel is not aware of the existence of threads.
2. The thread library contains functions for creating and destroying threads, passing
messages and data between threads, scheduling thread execution, and saving and
restoring thread contexts.
3. The application begins execution with a single thread. When a program starts, it
initially has only one thread called the main thread.
Advantages of User-Level Threads
1. Thread switching does not require kernel mode privileges
Switching between user-level threads can be done without involving the operating
system kernel. Therefore, thread switching becomes faster and more efficient.
2. User-level threads can run on any operating system
Since user-level threads are managed by a thread library rather than the operating
system, they can work on different operating systems without major changes.
3. Scheduling can be application-specific
In user-level threading, the application itself can decide how threads should be
scheduled and executed according to its own requirements.
4. User-level threads are fast to create and manage
Creating, managing, and terminating user-level threads requires less time and fewer
system resources, making them efficient.
Disadvantages of User-Level Threads
1. Most system calls are blocking
In many operating systems, if one user-level thread performs a blocking system call,
the entire process may stop execution until the operation is completed.
2. Cannot fully utilize multiprocessing
User-level threads cannot take full advantage of multiple processors or multicore
systems because the kernel is not aware of these threads. Therefore, true parallel
execution is limited.

In kernel-level threading, thread management is handled directly by the operating


system kernel. Unlike user-level threads, there is no thread management code present
in the application area.
Kernel Thread
Kernel threads are directly supported and managed by the operating system. Any
application can be designed as a multithreaded application, where multiple threads
execute within a single process.
The operating system kernel maintains context information for both:
 the entire process, and
 each individual thread within the process.
The kernel performs scheduling on a thread basis, which means each thread is
scheduled independently for execution.
All thread-related operations such as:
 thread creation,
 scheduling,
 synchronization, and
 management
are carried out in the kernel space by the operating system.
Kernel-level threads are generally slower to create and manage compared to user-level
threads because every thread operation requires interaction with the operating system
kernel.
Advantages
 Kernel can simultaneously schedule multiple threads from the same process on
multiple processes.
 If one thread in a process is blocked, the Kernel can schedule another thread of the
same process.
 Kernel routines themselves can be multithreaded.
Disadvantages
 Kernel threads are generally slower to create and manage than the user threads.
 Transfer of control from one thread to another within the same process requires a
mode switch to the Kernel.

Multithreading Models
Some operating system provide a combined user level thread and Kernel level thread
facility. Solaris is a good example of this combined approach. In a combined system,
multiple threads within the same application can run in parallel on multiple processors
and a blocking system call need not block the entire process. Multithreading models
are
three types
 Many to many relationships.
 Many to one relationship.
 One to one relationship.
1. Many to Many Model
 The many-to-many model multiplexes any number of user threads onto an
equal or smaller number of kernel threads.
 The following diagram shows the many-to-many threading model where 6
user level threads are multiplexing with 6 kernel level threads.
 In this model, developers can create as many user threads as necessary and
the corresponding Kernel threads can run in parallel on a multiprocessor
machine.
 This model provides the best accuracy on concurrency and when a thread
performs a blocking system call, the kernel can schedule another thread for
execution.

Many to One Model


 Many-to-one model maps many user level threads to one Kernel-level thread.
Thread management is done in user space by the thread library.
 When thread makes a blocking system call, the entire process will be blocked.
 Only one thread can access the Kernel at a time, so multiple threads are unable
to run in parallel on multiprocessors.
 If the user-level thread libraries are implemented in the operating system in
such a way that the system does not support them, then the Kernel threads use
the many-to-one relationship modes.
One to One Model:
 There is one-to-one relationship of user-level thread to the kernel-level thread.
 This model provides more concurrency than the many-to-one model. It also
allows another thread to run when a thread makes a blocking system call.
 It supports multiple threads to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the
corresponding Kernel thread. OS/2, windows NT and windows 2000 use one
to one relationship model.

You might also like