0% found this document useful (0 votes)
18 views33 pages

Os Threads Lecture 7

Uploaded by

ani1mesh2anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views33 pages

Os Threads Lecture 7

Uploaded by

ani1mesh2anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

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
– Thread id
– register set
– stack space
• A thread shares with its peer threads its:
– code section
– data section
– operating-system resources
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.
– Process = [Code + Data + Heap + Stack +
PC + PCB + CPU Registers]
– Thread = [Parent Code, Data and Heap +
Stack, PC and CPU Registers]
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

• Termination of a process, terminates all


threads within the process

8
Types of Threads
1. User Level Threads: (ULT)
• Threads of user application process.
• ULT are supported above the kernel and managed
without kernel support
• These are implemented in user space in main
memory. And managed bu user level library.
• The kernel is not aware of the existence of threads
• User Level Library is used for – Thread Creation,
Scheduling and Management
Threads (Cont.)
• ULT require a kernel system call to operate

• It only takes care of the execution part.

• The lack of cooperation between user level threads


and the kernel is a known disadvantage.
• In this case, the kernel may not favor a process
that has many threads.
• User level threads are typically fast. Creating
threads, switching between threads and
synchronizing threads only needs a procedure call.
• If one thread blocks cause the entire process to
block.
User Level Threads
Advantages
•Thread switching does not require Kernel mode
privileges.
•User level thread can run on any operating system.
•Scheduling can be application specific in the user level
thread.
•User level threads are fast to create and manage.

Disadvantages
•In a typical operating system, most system calls are
blocking.
•Multithreaded application cannot take advantage of
multiprocessing.
Kernel Level Threads(KLT)
• Threads of processes defined by OS itself
• KLT are supported and managed directly by OS.
• Kernel performs Thread Creation, Scheduling and
Management in kernel space.
• No thread library but system calls to the kernel
facility exists.
Kernel Level Threads(KLT)
• Kernel level threads are managed by the OS
• thread operations (ex. Scheduling) are
implemented in the kernel code.
• Kernel level threads may favor thread heavy
processes.
• they can also utilize multiprocessor systems by
splitting threads on different processors or cores.
• If one thread blocks it does not cause the entire
process to block.
• KLT are not portable because the implementation
is operating system dependent.
Kernel Level Threads(KLT)
Advantages
•Kernel can simultaneously schedule multiple threads
from the same process on multiple processes.
•If one thread in a process is blocked, the Kernel can
schedule another thread of the same process.
•Kernel routines themselves can be multithreaded.

Disadvantages
•Kernel threads are generally slower to create and
manage than the user threads.
•Transfer of control from one thread to another within
the same process requires a mode switch to the Kernel.
Combined ULT/KLT Approaches
• Thread creation done in the user space
• In a combined system, multiple threads within the
same application can run in parallel on multiple
processors and a blocking system call need not block
the entire process
• The programmer may adjust the number of KLTs

• 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.
Create and Terminate a thread
Include header file: pthread.h
Create:
pthread_create( &th, NULL, running, NULL);
(address of thread, att. of thread, name of a function where the
newly created thread will start, arg. to th)

To terminate a single thread without killing the entire process,


use the system call
void pthread_exit(void *value_ptr);
Multithreading Models
• One-to-One

• Many-to-One

• Many-to-Many
One-to-One
• Each user-level thread maps to one kernel thread
• The one-to-one model associates a single user-level thread to a
single kernel-level thread.
• kernel level threads follow the one to one model
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.
• User level threads follow the many to
one threading model.
• This means multiple threads managed
by a library in user space but the kernel
is only aware of a single thread of the
process owning these threads.
Many-to-Many Model
• In this model, developers can create as many user
threads as necessary and the corresponding Kernel
threads can run in parallel on a multiprocessor
machine.
• This model provides the best accuracy on concurrency
and when a thread performs a blocking system call,
the kernel can schedule another thread for execution.
Many-to-Many Model
• A number of user-level threads are associated to an
equal or smaller number of kernel-level threads.

• 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.
End of Chapter

You might also like