4 Threads
4 Threads
Threads
A thread is a basic unit of CPU utilization; it
comprises a thread ID, a program counter, a register
set, and a stack.
It shares with other threads belonging to the same
process its code section, data section, and other
operating-system resources, such as open files and
signals.
A traditional (or heavyweight) process has a single
thread of control.
A process has multiple threads of control, it can
perform more than one task at a time.
Multithreading
Many software packages that run on modern desktop
PCs are multithreaded.
An application typically is implemented as a separate
process with several threads of control.
A web browser might have one thread display images
or text while another thread retrieves data from the
network,
For example. A word processor may have a thread for
displaying graphics, another thread for responding to
keystrokes from the user, and a third thread for
performing spelling and grammar checking in the
background.
Single and Multithreaded
Processes
Benefits
Advantages:
The most obvious advantage of this technique is that a user-level
threads package can be implemented on an Operating System that
does not support threads. Some other advantages are
User-level threads does not require modification to operating
systems.
Simple Representation: Each thread is represented simply by a PC,
registers, stack and a small control block, all stored in the user
process address space.
Simple Management: This simply means that creating a thread,
switching between threads and synchronization between threads
can all be done without intervention of the kernel.
Fast and Efficient: Thread switching is not much more
expensive than a procedure call.
Disadvantages:
There is a lack of coordination between threads and
operating system kernel. Therefore, process as whole gets
one time slice irrespective of whether process has one
thread or 1000 threads within.
User-level threads requires non-blocking systems call i.e., a
multithreaded kernel. Otherwise, entire process will
blocked in the kernel, even if there are runnable threads left
in the processes.
Kernel Threads
In this method, the kernel knows about and manages the
threads.
Instead of process table in each process, the kernel has
a thread table that keeps track of all threads in the
system. In addition, the kernel also maintains the
traditional process table to keep track of processes.
Operating Systems kernel provides system call to create
and manage threads.
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Advantages:
Because kernel has full knowledge of all threads, Scheduler
may decide to give more time to a process having large number
of threads than process having small number of threads.
Kernel-level threads are especially good for applications that
frequently block.
Disadvantages:
The kernel-level threads are slow and inefficient. For instance,
threads operations are hundreds of times slower than that of
user-level threads.
Since kernel must manage and schedule threads as well as
processes. It require a full thread control block (TCB) for each
thread to maintain information about threads. As a result there
is significant overhead and increased in kernel complexity.
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Many-to-One
The many-to-one model maps many user-level threads
to one kernel thread.
Thread management is done by the thread library in
user space, so it is efficient;
but the entire process will block if a thread makes a
blocking system call. Also, because only one thread
can access the kernel at a time,
Multiple threads are unable to run in parallel on
multiprocessors.
Example:
Solaris Green Threads
Many-to-One Model
One-to-One
The one-to-one model maps each user thread to a
kernel thread.
It provides more concurrency than the many-to-one
model by allowing another thread to run when a thread
makes a blocking system call;
It also allows multiple threads to run in parallel on
multiprocessors.
The only drawback to this model is that creating a user
thread requires creating the corresponding kernel
thread. Because the overhead of creating kernel
threads can burden the performance of an application.
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
One-to-one Model
Many-to-Many Model
The many-to-many model multiplexes many user-level
threads to a smaller or equal number of kernel threads.
The number of kernel threads may be specific to either a
particular application or a particular machine (an
application may be allocated more kernel threads on a
multiprocessor than on a uniprocessor).
In many-to-many model Developers can create as many
user threads as necessary, and the corresponding kernel
threads can run in parallel on a multiprocessor.
When a thread performs a blocking system call, the
kernel can schedule another thread for execution.
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Many-to-Many Model