0% found this document useful (0 votes)
11 views

4 Threads

Uploaded by

Prajwal Kandel
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

4 Threads

Uploaded by

Prajwal Kandel
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

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

The benefits of multithreaded programming can be broken


down into four major categories:
Responsiveness- Multithreading an interactive
application may allow a program to continue running even if
part of it is blocked or is performing a lengthy operation,
thereby increasing responsiveness to the user.
For instance, a multithreaded web browser could still allow
user interaction in one thread while an image was being
loaded in another thread.

Resource Sharing- By default, threads share the memory


and the resources of the process to which they belong. The
benefit of sharing code and data is that it allows an
application to have several different threads of activity
within the same address space.
 Economy- Allocating memory and resources for process
creation is costly. Because threads share resources of
the process to which they belong, it is more economical
to create and context-switch threads. it is much more
time consuming to create and manage processes than
threads.
 In Solaris, for example, creating a process is about thirty
times slower than is creating a thread, and context
switching is about five times slower.

 Utilization of MP Architectures- The benefits of


multithreading can be greatly increased in a
multiprocessor architecture, where threads may be
running in parallel on different processors. A single
threaded process can only run on one CPU, no matter
how many are available. Multithreading on a multi-CPU
machine increases concurrency.
User Threads
 User-level threads are threads that are visible to the programmer
and are unknown to the kernel.
 user-level threads are faster to create and manage than are kernel
threads, as no intervention from the kernel is required.

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

You might also like