A thread is the smallest unit of processing that can be performed in an OS.
A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history.
Each thread belongs to exactly one process and no thread can exist outside a process.
Threads are also known as Lightweight processes.
For example, in a browser, many tabs can be viewed as threads. MS Word uses many threads - formatting text from one thread, processing input from another thread, etc.
2. A thread is the smallest unit of processing that can be performed in an OS.
A thread is a flow of execution through the process code, with its own program
counter that keeps track of which instruction to execute next, system registers which
hold its current working variables, and a stack which contains the execution history.
Each thread belongs to exactly one process and no thread can exist outside a process.
Threads are also known as Lightweight processes.
For example, in a browser, many tabs can be viewed as threads. MS Word uses
many threads - formatting text from one thread, processing input from another
thread, etc.
3. In the operating system, there are two types of threads.
User Level Threads − User managed threads.
Kernel Level Threads − Operating System managed threads acting on kernel, an
operating system core.
4. The user-level threads are managed by users and the kernel is not aware of it.
These threads are faster to create and manage.
The kernel manages them as if it was a single-threaded process.
It is implemented using user-level libraries and not by system calls. So, no call to
the operating system is made when a thread switches the context.
Each process has its own private thread table to keep the track of the threads.
5. The kernel knows about the thread and is supported by the OS.
The threads are created and implemented using system calls.
The thread table is not present here for each process. The kernel has a thread
table to keep the track of all the threads present in the system.
Kernel-level threads are slower to create and manage as compared to user-level
threads.
6. Multithreading is a phenomenon of executing multiple threads at the same time.
Example: Playing a video and downloading it at the same time is an example of
multithreading.
For user-level thread and kernel-level thread to function together there must exist
a relationship between them.
This relation is established by using Multithreading Models.
7. There are three common ways of establishing this relationship.
Many-to-One Model: multiple user threads are associated or mapped with one
kernel thread.
One-to-One Model: The one to one model creates a separate kernel thread to
handle each and every user thread
Many-to-Many Model: The many to many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads, combining the best
features of the one-to-one and many-to-one models.
8. Thread Control Blocks (TCBs) represents threads generated in the system.
It contains information about the threads, such as it’s ID and states.
9. The components have been defined below:
Thread ID: It is a unique identifier assigned by the Operating System to the
thread when it is being created.
Thread states: These are the states of the thread which changes as the thread
progresses through the system
CPU information: It includes everything that the OS needs to know about, such as
how far the thread has progressed and what data is being used.
Thread Priority: It indicates the weight (or priority) of the thread over other
threads which helps the thread scheduler to determine which thread should be
selected next from the READY queue.
A pointer which points to the process which triggered the creation of this thread.
A pointer which points to the thread(s) created by this thread.
10. Performance: Threads improve the overall performance(throughput, computational
speed, responsiveness) of a program.
Resource sharing: As the threads can share the memory and resources of any
process it allows any application to perform multiple activities inside the same
address space.
Utilization of Multiple Processor Architecture: The different threads can run
parallel on the multiple processors hence, this enables the utilization of the
processor to a large extent and efficiency.
Reduced Context Switching Time: The threads minimize the context switching time
as in Thread Switching, the virtual memory space remains the same.
Concurrency: Thread provides concurrency within a process.
Parallelism: Parallel programming techniques are easier to implement.