Os Lec 5 Thread
Os Lec 5 Thread
Threads, like process, are a mechanism to allow a program to do more than one thing at a time.
Conceptually, a thread (also called lightweight process) exists within a process (heavyweight
process). Threads are a finer-grained unit of execution than processes
Traditional threads have its own address space and a single thread of control.
The term multithreading is used to describe the situation of allowing the multiple threads in same
process.
When multithreaded process is run on a single-CPU system, the threads take turns running as in
the multiple processes.
All threads share the same address space, global variables, set of open file, child processes,
alarms and signals etc
( a) Three process each with one thread (b) one process with three threads. Figure (a) organization is
used when three processes are unrelated whereas (b) would be appropriate when the three threads are
actually part of the same job and are actively and closely cooperating with each other.
1 Process is heavy weight or resource Thread is light weight, taking lesser resources than a
intensive. process.
2 Process switching needs interaction with Thread switching does not need to interact with
operating system. operating system.
3 In multiple processing environments, All threads can share same set of open files, child
each process executes the same code but processes.
has its own memory and file resources.
4 If one process is blocked, then no other While one thread is blocked and waiting, a second
process can execute until the first process thread in the same task can run.
is unblocked.
5 Multiple processes without using threads Multiple threaded processes use fewer resources.
use more resources.
6 In multiple processes each process One thread can read, write or change another thread's
operates independently of the others. data.
Advantages of Thread
Threads minimize the context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
It is more economical to create and context switch threads.
Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.
Responsiveness.
Resource sharing.
Economy.
Types of Thread
Threads are implemented in following two ways −
User Level Threads − User managed threads.
Kernel Level Threads − Operating System managed threads acting on kernel,
an operating system core.
User Level Threads
In this case, the thread management kernel is not aware of the existence of threads. The thread
library contains code for creating and destroying threads, for passing message and data between
threads, for scheduling thread execution and for saving and restoring thread contexts. The
application starts with a single thread.
Feature
– Implemented as a library
– Library provides support for thread creation, scheduling and management with no support from
the kernel.
– Fast to create
– If kernel is single threaded, blocking system calls will cause the entire process to block.
Example: POSIX Pthreads, Mach C-threads.
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.
1 User-level threads are faster to create and Kernel-level threads are slower to
manage. create and manage.
3 User-level thread is generic and can run on any Kernel-level thread is specific to the
operating system. operating system.
Thread Processes
Single Threaded Processes
Multithreaded Processes
Single Threaded Processes
Single threaded processes contain the execution of instructions in a single sequence. In other
words, one command is processes at a time.
Multithreading Models
Most multithreading models fall into one of the following categories of
threading implementation:
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One Model (Green Threads)
Implementations of the many-to-one model (many user threads to one kernel thread) allow the
application to create any number of threads that can execute concurrently.
In a many-to-one (user-level threads) implementation, all threads activity is restricted to user
space. Additionally, only one thread at a time can access the kernel, so only one schedulable
entity is known to the operating system.
As a result, this multithreading model provides limited concurrency and does not exploit
multiprocessors.
Many-to-one model maps many user level threads to one Kernel-level thread. Thread
management is done in user space by the thread library.
When thread makes a blocking system call, the entire process will be blocked. Only one thread
can access the Kernel at a time, so multiple threads are unable to run in parallel on
multiprocessors.
If the user-level thread libraries are implemented in the operating system in such a way that the
system does not support them, then the Kernel threads use the many-to-one relationship modes.
The initial implementation of Java threads on the Solaris system was many-to-one.
One-to-One Model
The one-to-one model (one user thread to one kernel thread) is among the earliest
implementations of true multithreading. In this implementation, each user-level thread created by
the application is known to the kernel, and all threads can access the kernel at the same time.
The main problem with this model is that it places a restriction on you to be careful and careful
with threads, as each additional thread adds more "weight" to the process. Consequently, many
implementations of this model, such as Windows NT and the OS/2 threads package, limit the
number of threads supported on the system
There is one-to-one relationship of user-level thread to the kernel-level thread. This model
provides more concurrency than the many-to-one model.
It also allows another thread to run when a thread makes a blocking system call. It supports
multiple threads to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the corresponding Kernel
thread. OS/2, windows NT and windows 2000 use one to one relationship model
Many-to-Many Model
• The many-to-many model (many user-level threads to many kernel-level threads) avoids
many of the limitations of the one-to-one model, while extending multithreading
capabilities even further.
• The many-to-many model, also called the two-level model, minimizes programming
effort while reducing the cost and weight of each thread.
• In the many-to-many model, a program can have as many threads as are appropriate
without making the process too heavy or burdensome.
• The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads.
• The following diagram shows the many-to-many threading model where 6 user level
threads are multiplexing with 6 kernel level threads.
• 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.