You are on page 1of 32

Chapter : Threads

Chapter : Threads

• Overview
• Benefits of Threads vs Processes
• Single and Multithreaded Processes
• Types of Threads
• Multithreading Models
• Thread Libraries
Threads
• It is single sequential (flow of) execution of tasks of process
• A thread (or lightweight process) is a basic unit of CPU
utilization; it consists of:
– program counter
– register set
– stack space
• A thread shares with its peer threads its:
– code section
– data section
– operating-system resources

• A traditional or heavyweight process is equal to a task with


one thread
Threads (Cont.)
• In a multiple threaded task, while one thread is blocked and
waiting, a second thread in the same task can run.

– 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
• 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

• Less time to switch between two threads - newly created


thread uses the current process address space.

• Less communication overheads - threads share everything:


address space, So, data produced by one thread is
immediately available to all the other threads.
Single and Multithreaded Processes
Threads States
• Three key states: running, ready, blocked
• They have no suspend state because all
threads within the same process share the
same address space
– Suspending (ie: swapping) a single thread involves
suspending all threads of the same process
• Termination of a process, terminates all
threads within the process

8
Types of Threads
1. User Level Threads:
1.Threads of user application process.

2.These are implemented in user space in main


memory.

3.The kernel is not aware of the existence of threads

4.User Level Library is used for – Thread Creation,


Scheduling and Management
• Thread switching does not require kernel mode
privileges (no mode switch) and scheduling is
application specific.
Kernel Level Threads(KLT)
1. Threads of processes defined by OS itself

2. Kernel performs Thread Creation, Scheduling and


Management in kernel space.

3. No thread library but system calls to the kernel facility exists.

4. Some processes may be blocked , User processes must not


call blocked processes
Combined ULT/KLT Approaches
• Thread creation done in the user space
• Bulk of scheduling and synchronization of threads
done in the user space
• The programmer may adjust the number of KLTs
• May combine the best of both approaches
• Example is Solaris
• Lightweight processes (LWP) each LWP supports one
or more ULTs and maps to exactly one KLT
Thread library
The interface to multithreading support is through a library.

• creating and destroying threads


• passing messages and data between threads
• scheduling thread execution
• saving and restoring thread contexts
Thread library
Thread libraries provide:
Thread generations and management in the development of
multithreaded applications.

The implementation of such libraries are performed via two


principal approaches:

1. User Levels

In the user level approach:

-Actual code and data structures are held within the user space

-Invoking a library method will result in a call to a local method


within the user space rather than a system call.
Thread library
2. Kernel Level Approach:

- The actual code and data structures are held within the kernel
space

- Invoking a library method results in a system call to the kernel


since kernel level libraries are implemented by the operating
system itself.
Multithreading Models
• One-to-One

• 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.

Advantage:
facilitates the running of multiple threads in parallel.

Drawback:
Generation of every new user thread must include the creation
of a corresponding kernel thread causing an overhead
One-to-one Model

The one-to-one model allows for greater


concurrency, but the developer has to be careful
not to create too many threads within an
application
Many-to-One
• The many-to-one
model associates all
user- level threads to
a single kernel-level
thread.
Many-to-Many Model
• A number of user-level threads are associated to an
equal or smaller number of kernel-level threads.

• The Allows many user level threads to be mapped to


many kernel threads
Many-to-Many Model
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.
1) The model in which one kernel thread is
mapped to many user-level threads is called:
a) Many to One model
b) One to Many model
c) Many to Many model
d) One to One model
• Which of the following is the drawback of the
One to One Model ?
a) increased concurrency provided by this
model
b) decreased concurrency provided by this
model
c) creating so many threads at once can crash
the system
d) creating a user thread requires creating the
corresponding kernel thread
• Which one of the following is not shared by
threads?
a) program counter
b) stack
c) both (a) and (b)
d) none of the mentioned
• If one thread opens a file with read privileges
then
a) other threads in the another process can
also read from that file
b) other threads in the same process can also
read from that file
c) any other thread can not read from that file
d) all of the mentioned
• Termination of the process terminates
a) first thread of the process
b) first two threads of the process
c) all threads within the process
d) no thread within the process
• A thread shares its resources(like data section,
code section, open files, signals) with :
a) other process similar to the one that the
thread belongs to
b) other threads that belong to similar
processes
c) other threads that belong to the same
process
d) All of these
• A heavy weight process :
a) has multiple threads of execution
b) has a single thread of execution
c) can have multiple or a single thread for
execution
d) None of these
• A thread is also called :
a) Light Weight Process(LWP)
b) Heavy Weight Process(HWP)
c) Process
d) None of these
• Which one of the following is not a valid state
of a thread?
a) running
b) parsing
c) ready
d) blocked

You might also like