2. What Is a Thread?
• A thread is a sequence or stream of executable
code within a process that is scheduled for
execution by the operating system on a processor
or core.
• All processes have a primary thread.
• The primary thread is a process ’ s flow of control
or thread of execution.
• A process with multiple threads has as many
flows of controls as there are threads. Each
thread executes independently and concurrently
with its own sequence of instructions.
3. user - level threads and kernel - level
threads.
• A process with multiple threads is multithreaded.
• There are user - level threads and kernel - level
threads.
• Kernel - level threads are a lighter burden to
create, maintain, and manage on the operating
system as compared to a process because very
little information is associated with a thread.
A kernel thread is called a lightweight process
because it has less overhead than a process.
4. User - and Kernel - Level Threads
• There are three implementation models for
threads:
• User - or application - level threads
• Kernel - level threads
• Hybrid of user - and kernel - level threads
8. user or kernel space or mode.
• These threads run in user or kernel space or
mode.
• In user mode , a process or thread is executing
instructions in the program or linked library.
• They are not making any calls to the operating
system kernel.
• In kernel mode , the process or thread is making
system calls such as accessing resources or
throwing exceptions. Also, in kernel mode, the
process or thread can access objects that are
defined in kernel space.
9. User - level threads
• User - level threads reside in user space or mode.
• The runtime library, also in user space, manages these threads.
• They are not visible to the operating system and, therefore,
cannot be scheduled to a processor core.
• Each thread does not have its own thread context.
• there is only one thread per process that will be running at any
given time and only a single processor core allocated to that
process.
• The runtime library schedules and dispatches these threads.
• User - level threads are considered a “ many - to - one ” thread
mapping.
10. Kernel - level threads
• Kernel - level threads reside in kernel space and are kernel
objects.
• With kernel threads, each user thread is mapped to or
bound to a kernel thread.
• The user thread is bound to that kernel thread for the life of
the user thread. Once the user thread terminates, both
threads leave the system. This is called a “ one - to - one ”
thread mapping
• The operating system scheduler manages, schedules, and
dispatches these threads. The runtime library requests a
kernel - level thread for each of the user – level threads.
• The operating system creates a context for each thread.
11. A hybrid thread
• A hybrid thread implementation is a cross between user
and kernel threads and allows both the library and the
operating system to manage the threads.
• The operating system selects a user thread and maps it to
one of the available kernel threads in the pool.
• More than one user thread may be assigned to the same
kernel thread.
12. Thread Context
• When one process is removed from a core and another
process becomes active, a context switch takes place between
those processes.
• The operating system must keep track of all the information
that is needed to restart that process and start the new
process in order for it to become active.
• This information is called the context and describes the present
state of the process. When the process becomes active, it can
continue execution right where it was preempted.
The information or context of the process includes:
• Process id
• Pointer to executable
• The stack
• Memory for static and dynamically allocated variables
• Processor registers
13. Hardware Threads and Software
Threads
• Chip manufacturers implement cores that have multiple hardware
threads that serve as logical cores
• Cores with multiple hardware threads are called simultaneous
multithreaded (SMT) cores.
• SMT brings to hardware the concept of multithreading
• SMT - enabled processors execute many software threads or
processes simultaneously within the processor cores
15. Threads & Processes
Both have id, set of registers, state, priority, and scheduling
policy.
Both have attributes that describe the entity to the OS.
Both have an information block.
Both share resources with the parent process
Both function as independent entities from the parent
process.
The creator can exercise some control over the thread or
process.
Both can change their attributes.
Both can create new resources.
Neither can access the resources of another process.
16. Threads Processes
Threads share the address
space of the process that
created it
processes have their own
address.
Threads have direct access to
the data segment
of their process
processes have their own copy
of
the data segment of the
parent process.
Threads can directly
communicate with
other threads of their process;
processes must use
Interprocess Communication
to communicate
with sibling processes.
Threads have almost no
overhead
processes
have considerable overhead.
17. Threads Processes
New threads are easily
created
new processes
require duplication of the
parent process.
Threads can exercise
considerable control over
threads of the same process
processes can
exercise control only over
child processes.
Changes to the main thread
(cancellation, priority
change, and so on) may affect
the behavior of the
other threads of the process
changes to the
parent process do not affect
child processes.
18. Setting Thread Attributes
The POSIX thread library defines a thread attribute object that
encapsulates a subset of the properties of the thread.
These attributes are accessible and modifiable by the creator of
the thread.
These are the thread attributes that are modifiable:
• Contention scope
• Stack size
• Stack address
• Detached state
• Priority
• Scheduling policy and parameters
19. The scope attribute describes which threads a
particular thread competes with for resources.
Threads contend for resources within two
contention scopes:
1. Process scope
2. System scope
20. • Threads with process scope compete with threads
within the same process
• threads with system wide contention scope compete
for resources with threads of other processes allocated
across the system.
• A thread that has system scope is prioritized and
scheduled with respect to all of the system wide
threads.
• The scheduling policy also determines when a thread is
assigned the processor. FIFO, round robin (RR), and
other scheduling policies are available.
23. Thread States
• The thread is the unit of execution when a
process is scheduled to be executed.
• If the process has only one thread, it is the
primary thread that is assigned to a processor
core.
• If a process has multiple threads and there
are multiple processors available to the
process, all of the threads are assigned to
processors.
24. Thread States
• When a thread is scheduled to execute on a processor core, it
changes its state.
• A thread state is the mode or condition that a thread is in at
any given time.
• Threads have the same states and transitions as for processes.
• There are four commonly implemented states:
Runnable
Running (active)
Stopped
Sleeping (blocked)
• There are several transitions:
Preempt
Signaled
Dispatch
Timer runout
25. Thread States cont.
• The primary thread can determine the state of an
entire process.
• The state of the primary thread is that same as the
state of the process, if it ’ s the only thread.
• If the primary thread is sleeping, the process is
sleeping.
• If the primary thread is running, the process is running.
• For a process that has multiple threads,
all threads of the process have to be in a sleeping or
stopped state in order for the whole process to be
considered sleeping or stopped.