0% found this document useful (0 votes)
93 views10 pages

Os Lec 5 Thread

Threads are a mechanism that allow a program to perform multiple tasks simultaneously. Conceptually, threads exist within a process and share its resources like memory. Traditional threads have their own program counter and stack, but share other resources with other threads in the same process like files and signals. Multithreading allows multiple threads to run concurrently within a single process on a single CPU system by rapidly switching between threads. Threads can be implemented at the user level or kernel level.

Uploaded by

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

Os Lec 5 Thread

Threads are a mechanism that allow a program to perform multiple tasks simultaneously. Conceptually, threads exist within a process and share its resources like memory. Traditional threads have their own program counter and stack, but share other resources with other threads in the same process like files and signals. Multithreading allows multiple threads to run concurrently within a single process on a single CPU system by rapidly switching between threads. Threads can be implemented at the user level or kernel level.

Uploaded by

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

What is 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.

Each thread maintain its own


stack
Needs to accept user input, display it on screen, spell check, auto save and grammar check.
– Implicit: Write code that reads user input, displays/formats it on screen, calls spell checked etc.
while making sure that interactive response does not suffer.
– Threads: Use threads to perform each task and communicate using queues and shared data
structures
– Processes: expensive to create and do not share data structures and so explicitly passed.

Others: Spreadsheet, Server for www, browser etc

Difference between Process and Thread

S.N Process Thread


.

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.

Problems: designing complexity

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

 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
In this case, thread management is done by the Kernel. There is no thread management code in
the application area. Kernel threads are supported directly by the operating system. Any
application can be programmed to be multithreaded. All of the threads within an application are
supported within a single process.
The Kernel maintains context information for the process as a whole and for individuals’
threads within the process. Scheduling by the Kernel is done on a thread basis. The Kernel
performs thread creation, scheduling and management in Kernel space. Kernel threads are
generally slower to create and manage than the user threads.
Feature
Kernel Threads: Supported by the Kernel
Kernel performs thread creation, scheduling and management in kernel space. Slower to create
and manage
Blocking system calls are no problem
Most OS’s support these threads
Examples: WinX, Linux
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.

Difference between User-Level & Kernel-Level Thread


S.N. User-Level Threads Kernel-Level Thread

1 User-level threads are faster to create and Kernel-level threads are slower to
manage. create and manage.

2 Implementation is by a thread library at the user Operating system supports creation of


level. Kernel threads.

3 User-level thread is generic and can run on any Kernel-level thread is specific to the
operating system. operating system.

4 Multi-threaded applications cannot take Kernel routines themselves can be


advantage of multiprocessing. multithreaded.

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.

1. In this type of processes a single thread runs at a time.


2. Single threaded model uses a process event loop with polling.
3. CPU time is wasted.
4. Idle time is more.
5. It results in less efficient programs.
6. When one thread is paused, the system waits until this thread is resumed.
Multithreaded Processes
The opposite of single threaded processes are multithreaded processes. These processes allow
the execution of multiple parts of a program at the same time. These are lightweight processes
available within the process

1. In this type of processing multiple threads run at the same time.


2. Multi-threaded model doesn’t use event loop with polling.
3. CPU time is never wasted.
4. Idle time is minimum.
5. It results in more efficient programs.
6. When one thread is paused due to some reason, other threads run as normal

Advantages of Multithreaded Processes


Some of the advantages of multithreaded processes are given as follows −

 Improved throughput: Many concurrent compute operations and I/O requests


within a single process.
 Simultaneous and fully symmetric use of multiple processors for computation and
I/O.
 Superior application responsiveness: If a request can be launched on its own
thread, applications do not freeze. An entire application will not block, or
otherwise wait, pending the completion of another request.
 Improved server responsiveness: Large or complex requests or slow clients
don't block other requests for service. The overall throughput of the server is
much greater.
 Minimized system resource usage: Threads impose minimal impact on system
resources. Threads require less overhead to create, maintain, and manage than
a traditional process.
 Program structure simplification: Threads can be used to simplify the structure
of complex applications, such as server-class and multimedia applications.
Simple routines can be written for each activity, making complex programs easier
to design and code, and more adaptive to a wide variation in user demands.
 Better communication: Thread synchronization functions can be used to
provide enhanced process-to-process communication. In addition, sharing large
amounts of data through separate threads of execution within the same address
space provides extremely high-bandwidth, low-latency communication between
separate tasks within an application.

Disadvantages of Multithreaded Processes


Some of the disadvantages of multithreaded processes are given as follows −
 Multithreaded processes are quite complicated. Coding for these can only be
handled by expert programmers.
 It is difficult to handle concurrency in multithreaded processes. This may lead to
complications and future problems.
 Identification and correction of errors is much more difficult in multithreaded
processes as compared to single threaded processes.

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.

You might also like