RCS214 Threads L24.06
RCS214 Threads L24.06
RCS214_L24.06
Thread
What is a thread
• A thread is a single sequential flow of execution of
tasks of a process (known as thread of execution or
thread of control).
• Thread is often referred to as a lightweight process.
• And a lightweight process (LWP) is a unit of execution
within a larger process that allows for multitasking
and parallelism within a single application
Thus,
• There is a way of thread execution inside the
process of any operating system.
• There can be more than one thread inside a
process.
• Each thread of the same process makes use of
a separate program counter and a stack of
activation records and control blocks.
Many threads in one Process
• The process can be
split down into so
many threads.
• 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.
Need of Thread:
1. It takes far less time to create a new thread in an
existing process than to create a new process.
2. Threads can share the common data, they do not
need to use Inter- Process communication.
3. Context switching is faster when working with
threads.
4. It takes less time to terminate a thread than a
process.
Components of Threads
• Any thread has the following components.
1. Program counter
2. Register set
3. Stack space
Benefits of Threads
1. Enhanced throughput of the system:
– When the process is split into many threads, and each thread is
treated as a job, the number of jobs done in the unit time
increases. That is why the throughput of the system also increases.
2. Effective Utilization of Multiprocessor system:
– When you have more than one thread in one process, you can
schedule more than one thread in more than one processor.
3. Faster context switch:
– The context switching period between threads is less than the
process context switching. The process context switch means more
overhead for the CPU.
Benefit Cont’d
4. Responsiveness:
– When the process is split into several threads, and when a thread
completes its execution, that process can be responded to as soon as
possible.
5. Communication:
– Multiple-thread communication is simple because the threads share the
same address space, while in process, we adopt just a few exclusive
communication strategies for communication between two processes.
6. Resource sharing:
– Resources can be shared between all threads within a process, such as
code, data, and files. Note: The stack and register cannot be shared
between threads. There is a stack and register for each thread
Types of Threads
• There are two types of threads.
1. Kernel level thread.
2. User-level thread.
User space and kernel space
• User space and kernel space are separate
regions of memory that are used to run
different types of programs and processes:
• User space
– The memory area where application software and
some drivers run.
– User space programs are executed by a user in the
operating system, and they run in user mode,
which is a non-privileged execution mode.
User space and kernel space
• Kernel space
– The memory area where the operating system
kernel, kernel extensions, and most device drivers
run.
– The kernel is a special part of the operating system
that handles low-level operations, such as
managing memory, scheduling processes, and
handling interrupts.
– The kernel has unrestricted access to the system's
hardware resources
User-level thread (ULT)
• A user-level thread (ULT) is a thread that is
managed and executed entirely in user space,
without direct involvement from the operating
system's kernel.
• These threads are implemented through a
thread library that provides the necessary APIs
to create, schedule, and manage them.
Key Characteristics of User-Level Threads:
1. Managed by User Space:
– The thread library, not the kernel, is responsible for creating,
scheduling, and managing these threads.
2. Fast Context Switching:
– Since the kernel is not involved in thread management, context
switching between user-level threads is much faster than kernel-
level threads.
3. Process Scope:
– User-level threads are often associated with a single process,
and the kernel is unaware of their existence. If one thread
blocks, the entire process may be affected.
Key Characteristics cont’d
4. Portability:
– These threads can run on any operating system without
modifications because they rely on the user-space thread
library rather than the kernel.
5. Lightweight:
– User-level threads consume fewer system resources
because they do not require the kernel to allocate
resources for each thread.
Advantages of User-level threads
1. The user threads can be easily implemented than the kernel thread.
2. User-level threads can be applied to such types of operating systems
that do not support threads at the kernel-level.
3. It is faster and efficient.
4. Context switch time is shorter than the kernel-level threads.
5. It does not require modifications of the operating system.
6. User-level threads representation is very simple. The register, PC,
stack, and mini thread control blocks are stored in the address space
of the user-level process.
7. It is simple to create, switch, and synchronize threads without the
intervention of the process.
Disadvantages of User-level threads
1. User-level threads lack coordination between the
thread and the kernel.
2. If a thread causes a page fault, the entire process is
blocked.
Examples:
1. POSIX Threads (Pthreads):
– Some implementations of Pthreads can run in user space
without requiring kernel support.
2. Green Threads:
– Implemented entirely in user space, used in some JVMs and
earlier versions of programming environments like Ruby.
3. Cooperative Multithreading:
– Many game engines and runtime systems (like Node.js)
implement cooperative multithreading in user space to improve
performance
Kernel-level thread (KLT)
• A kernel-level thread (KLT) is a thread that is
directly managed and controlled by the operating
system's kernel.
• It is one of the types of threads used in multitasking
environments to execute tasks concurrently.
• Kernel-level threads operate at the core of the
operating system and have direct interaction with the
kernel, enabling them to perform lower-level, system-
critical operations.
Key Characteristics of Kernel-Level Threads:
1. Managed by the Kernel:
– The operating system kernel is responsible for creating,
scheduling, and managing these threads.
2. Preemptive Scheduling:
– The kernel can preempt one thread to give CPU time to another
based on its scheduling algorithm, improving system
responsiveness.
3. Independence from Processes:
– Kernel threads are independent entities that can exist even
without a specific process context, often used for tasks like
handling I/O operations.
Key Characteristics cont’d
4. Context Switching:
– Since the kernel manages context switching between threads, the
process can be slower than in user-level threads because it
involves switching to the kernel mode and back to user mode.
5. System Resource Usage:
– Kernel threads consume system resources (such as memory) for
maintaining their control blocks and context information.
6. Hardware Support:
– Kernel threads often utilize the hardware's ability to perform
multithreading, which can lead to efficient use of multi-core
processors.
Advantages of Kernel-level threads
1. The kernel-level thread is fully aware of all
threads.
2. The scheduler may decide to spend more CPU
time in the process of threads being large
numerical.
3. The kernel-level thread is good for those
applications that block the frequency.
Disadvantages of Kernel-level threads
1. The kernel thread manages and schedules all
threads.
2. The implementation of kernel threads is
difficult than the user thread.
3. The kernel-level thread is slower than user-
level threads.
Example:
• In Linux, kernel-level threads can be observed as part
of the kernel's task scheduler (sched).
• They are created using system calls and have full
support for synchronization mechanisms like
semaphores and mutexes provided by the kernel.
Next: Process Scheduling