Lecture1313 22078 Threads
Lecture1313 22078 Threads
Chapter 4: Threads
• Overview
• Multithreading Models
• Thread Libraries
• Threading Issues
• Operating System Examples
• Windows XP Threads
• Linux Threads
Threads
• Smallest unit of process.
• A thread (or lightweight process) is a basic unit of CPU , it consists of:
– program counter
– Thread ID
– register set
– stack space
• A thread shares with its peer threads its:
– code section
– data section
– operating-system resources
MOTIVATION:
– Many software packages that run on modern computers are
multithreaded.
– In web browser, one thread displays images, other text, other
fetches data from network.
– Word processor, graphics, response to keystrokes, spell check.
Benefits of Threads vs Processes
If implemented correctly then threads have some advantages of (multi) processes,
They take:
• Less time to create a new thread than a process, because the newly created
thread uses the current process address space.
• Less time to terminate a thread than a process.
• Less time to switch between two threads within the same process, partly because
the newly created thread uses the current process address space.
• Less communication overheads -- communication between the threads of one
process is simple because the threads share everything: address space, in
particular. So, data produced by one thread is immediately available to all the
other threads.
Single and Multithreaded Processes
Single threading-- when the OS does not recognize the concept of thread
Multithreading-- when the OS supports multiple threads of execution within a single process
Some example popular OSs and their thread support is:
• MS-DOS-- support a single user process and a single thread
• UNIX-- supports multiple user processes but only supports one thread per process
• Solaris-- supports multiple threads
Benefits
• Responsiveness:-
• Resource Sharing
• Economy
Multithreaded Server Architecture
User Level Threads(ULT)
• In this level, the kernel is not aware of the existence of threads -- All thread
management is done by the application by using a thread library.
• Thread switching does not require kernel mode privileges (no mode switch) and
scheduling is application specific.
• Context switching is faster than kernel level thread.
• If one user level thread perform blocking operation then entire process get block.
• Many-to-One
• Many-to-Many
One-to-One
• Each user-level thread maps to kernel thread
The one-to-one model associates a single user-level thread to a single kernel-level
thread.
This type of relationship facilitates the running of multiple threads in parallel.
However, this benefit comes with its own drawback such that generation of every
new user thread must include the creation of a corresponding kernel thread
causing an overhead, which can hinder the performance somehow.
• Examples
– Windows NT/XP/2000
– Linux
– Solaris 9 and later
One-to-one Model
In the user level approach, all the actual code and data structures
are held within the user space such that invoking a library method
will result in a call to a local method within the user
space rather than a system call.
In the kernel level approach, the actual code and data structures
are held within the kernel space such that invoking a library
method results in a system call to the kernel since kernel level
libraries are implemented by the operating system itself.
The main thread libraries used today are POSIX Pthreads, Win32
threads and Java threads.
Semantics of fork() and exec()
• Does fork() duplicate only the calling thread or all threads?
• Some UNIX versions have chosen to have 2 versions of fork()
• One that duplicates all the threads.
• One that duplicates only one thread.
• Exec() call works in the same way.
Thread Cancellation
• Thread cancellation is the task of terminating a thread
before it has completed.
– For example, if multiple threads are concurrently
searching through a database and one thread returns
the result, the remaining threads might be canceled.
– Another situation might occur when a user presses a
button on a web browser that stops a web page from
loading any further.
• A thread that is to be canceled is often referred to as the target thread. Cancellation of
a target thread may occur in two different scenarios:
– Asynchronous cancellation. One thread immediately terminates the target thread.
• The difficulty with cancellation occurs in situations where resources have been
allocated to a canceled thread or where a thread is canceled while in the midst
of updating data it is sharing with other threads.
• Often, the OS will reclaim system resources from a canceled thread but will not
reclaim all resources. Therefore, canceling a thread asynchronously may not
free a necessary system-wide resource.
– Deferred cancellation. The target thread periodically checks whether it should
terminate, allowing it an opportunity to terminate itself in an orderly fashion.
• With deferred cancellation, in contrast, one thread indicates that a target
thread is to be canceled, but cancellation occurs only after the target thread
has checked a flag to determine if it should be canceled or not.
• This allows a thread to check whether it should be canceled at a point when it
can be canceled safely. Pthreads refers to such points as cancellation points.
Thread Pools
• Create a number of threads in a pool where they await work
• Advantages:
– Usually slightly faster to service a request with an existing thread than
create a new thread
– A thread pool limits the number of threads that exist at any point. This
is important on the systems that cannot support a large number of
concurrent threads.
Thread Specific Data
• Threads belonging to a process share the data of the process.
However in some circum stances each thread might need its own set
of data. We call that data “thread specific data.”
• Allows each thread to have its own copy of data
• Example is a transaction processing system, we might service each
transaction in a separate thread.
End of Chapter 4