Operating Systems:
Internals and Design Principles.
CCS3203/
Operating Systems
Threads
[Link] Ibrahim
Introduction
Processes and Threads
Multithreading
Thread Functionality
Types of Threads
Windows 7 Thread and SMP Management.
2 Operating System Dr. Reem Ibrahim
Processes and Threads
Processes have two characteristics:
Resource ownership - process includes a virtual address
space to hold the process image.
Scheduling/execution path - follows an execution path
that may be interleaved with other processes.
The unit of resource ownership is referred to as a process or
task.
The unit of dispatching is referred to as a thread or lightweight
process.
Multithreading: The ability of an OS to support multiple,
concurrent paths of execution within a single process.
3 Operating System Dr. Reem Ibrahim
Single Thread Approaches
MS-DOS supports a
single user process
and a single thread.
Some UNIX, support
multiple user
processes but only
support one thread
per process.
4 Operating System Dr. Reem Ibrahim
Multithreading
Java run-time
environment is a
single process with
multiple threads.
Multiple processes
and threads are
found in Windows,
Solaris, and many
modern versions of
UNIX.
5 Operating System Dr. Reem Ibrahim
Process & Thread information
In a multithreaded environment, a
process is defined as the unit of
resource allocation and a unit of
protection.
Associated with processes:
A virtual address space which holds the
process image
Protected access to
Processors,
Other processes,
Files,
I/O resources
Each thread has
An execution state (running, ready, etc.).
Saved thread context when not running.
Single-Threaded and Multithreaded
An execution stack.
Some per-thread static storage for local
Process Models
variables.
Access to the memory and resources of
its process (all threads of a process
share this).
6 Operating System Dr. Reem Ibrahim
Benefits of Threads
Takes less time to create a new thread than a process.
Less time to terminate a thread than a process.
Switching between two threads takes less time that
switching processes.
Threads enhance efficiency in communication between
programs.
7 Operating System Dr. Reem Ibrahim
Thread use in a Single-User
Multiprocessing System
Foreground and background work (e.g. Spreadsheet ).
Asynchronous processing (e.g. protection against power failure
within a word processor).
Speed of execution (On thread can compute one batch of data while
another thread reading the next batch from a device)
Modular program structure
Several actions that affect all of the threads in a process
The OS must manage these at the process level.
Examples:
Suspending a process involves suspending all threads of the process.
Termination of a process, terminates all threads within the process.
8 Operating System Dr. Reem Ibrahim
Thread Functionality
Threads have execution states and may synchronize with one
another (Similar to processes).
We look at these two aspects of thread functionality in turn.
States.
Synchronisation.
The Key states for a thread are (running , Ready, Blocked)
There are four basic thread operations associated with a
change in thread State
Spawn : thread within a process may spawn another thread within
the same process,
Block: When a thread needs to wait for an event
Issue: will blocking a thread block other, or all, threads
Unblock : When the event for which a thread is blocked occurs
Finish : When a thread completes.
Deallocate register context and stacks
9 Operating System Dr. Reem Ibrahim
Thread Execution
Example: Remote Procedure Call using Threads
A program that
performs two
remote procedure
calls (RPCs) to
two different
hosts , to obtain a
combined result.
10 Operating System Dr. Reem Ibrahim
Multithreading on a Uniprocessor
11 Operating System Dr. Reem Ibrahim
Threads Synchronization
The threads of a process share the same address space and
other resources
Any alteration of a resource by one thread affects the environment
of the other threads in the same process.
The issues raised and the techniques used in the synchronization
of threads are, in general, the same as for the synchronization of
processes.
12 Operating System Dr. Reem Ibrahim
Types of Threads
User Level Thread (ULT)
Kernel level Thread (KLT) also called:
kernel-supported threads or lightweight processes.
13 Operating System Dr. Reem Ibrahim
User-Level Threads (ULTs)
All thread management is
done by the application.
The kernel is not aware of
the existence of threads.
OS schedules the process,
process decides which
thread(s) to run.
14 Operating System Dr. Reem Ibrahim
Relationships between ULT
Thread and Process States
system call
Thread 2 needs
some action
performed by
Time out thread 1
15 Operating System Dr. Reem Ibrahim
Relationships between ULT
Thread and Process States
All of the activity described in the previous Figure takes place in user
space and within a single process. The kernel is unaware of this activity.
The kernel continues to schedule the process as a unit and assigns a
single execution state (Ready, Running, Blocked, etc.) to that process.
Thread 2 makes a system call that blocks B. For example, an I/O call is
made. This causes control to transfer to the kernel. The kernel invokes I/O
action, places process B in the Blocked state, and switches to another
process(thread 2 of process B is still in the Running state).
A clock interrupt passes control to the kernel, and the kernel determines
that the currently running process (B) has exhausted its time slice. The
kernel places process B in the Ready state and switches to another
process. (thread 2 of process B is still in the Running state).
Thread 2 needs some action performed by thread 1 of process B. Thread
2 enters a Blocked state and thread 1 transitions from Ready to Running.
The process itself remains in the Running state.
16 Operating System Dr. Reem Ibrahim
User-level Threads
Threads managed by a threads library
Kernel is unaware of presence of threads
Advantages:
Thread switching does not require kernel mode privileges.
Scheduling can be application specific.
ULTs can run on any OS.
Disadvantages
In a typical OS, many system calls are blocking. As a result, when a
ULT executes a system call, not only is that thread blocked, but also
all of the threads within the process are blocked.
In a pure ULT strategy, a multithreaded application cannot take
advantage of multiprocessing.
A kernel assigns one process to only one processor at a time. Therefore,
only a single thread within a process can execute at a time.
17 Operating System Dr. Reem Ibrahim
User-level Threads
Overcoming ULT Disadvantages:
Writing an application as multiple processes rather than
multiple threads. But this approach eliminates the main
advantage of threads.
Jacketing . The purpose of jacketing is to convert a blocking
system call into a non-blocking system call.
18 Operating System Dr. Reem Ibrahim
Kernel-Level Threads (KLT)
All of the work of thread
management is done by the kernel.
Kernel maintains context information
for the process and the threads
No thread management done by
application, simply an application
programming interface (API) to the
kernel
Scheduling is done on a thread basis.
Windows is an example of this
approach.
19 Operating System Dr. Reem Ibrahim
Advantages of KLT
The kernel can simultaneously schedule multiple threads from
the same process on multiple processors.
If one thread in a process is blocked, the kernel can schedule
another thread of the same process.
Kernel routines themselves can be multithreaded.
Disadvantage of KLT
The transfer of control from one thread to another within the
same process requires a mode switch to the kernel.
20 Operating System Dr. Reem Ibrahim
Combined Approaches
Thread creation done in the
user space.
Bulk of scheduling and
synchronization of threads by
the application.
Example is Solaris
21 Operating System Dr. Reem Ibrahim
Windows 7 Thread and SMP Management
Processes and services provided by the Windows Kernel
are relatively simple and general purpose
Implemented as objects
An executable process may contain one or more threads
Both processes and thread objects have built-in synchronization
capabilities
22 Operating System Dr. Reem Ibrahim
Windows Process/ Thread Object
23 Operating System Dr. Reem Ibrahim
Thread States
24 Operating System Dr. Reem Ibrahim
Windows SMP Support
Threads can run on any processor
But an application can restrict affinity
Soft Affinity
The dispatcher tries to assign a ready thread to the same
processor it last ran on.
This helps reuse data still in that processor’s memory caches
from the previous execution of the thread.
Hard Affinity
An application restricts threads to certain processor.
25 Operating System Dr. Reem Ibrahim