You are on page 1of 26

Threads

Introduction
• What is a Thread?
• A thread is a flow of execution through the process code, with its own program
counter that keeps track of which instruction to execute next, system registers
which hold its current working variables, and a stack which contains the execution
history.
• A thread is a single sequence stream within in a process.
• Because threads have some of the properties of processes, they are sometimes
called lightweight processes.
• A thread shares with its peer threads few information like code segment, data
segment and open files.
• When one thread alters a code segment memory item, all other threads see that.
Introduction ctd’
• Threads provide a way to improve application performance through parallelism.
• The CPU switches rapidly back and forth among the threads giving illusion that
the threads are running in parallel.
• Threads represent a software approach to improving performance of operating
system by reducing the overhead thread is equivalent to a classical process.
• Each thread belongs to exactly one process and no thread can exist outside a
process.
• Each thread represents a separate flow of control.
• Threads have been successfully used in implementing network servers and web
server.
• They also provide a suitable foundation for parallel execution of applications on
shared memory multiprocessors.
Introduction ctd’
• Like a traditional process i.e., process with one thread, a thread can be in any of
several states (Running, Blocked, Ready or Terminated).
• Each thread has its own stack since thread will generally call different procedures
and thus a different execution history. This is why thread needs its own stack.
• An operating system that has thread facility, the basic unit of CPU utilization is a
thread.
• A thread has or consists of a program counter (PC), a register set, and a stack
space.
• Threads are not independent of one other like processes as a result threads
shares with other threads their code section, data section, OS resources also
known as task, such as open files and signals.
Single-threaded and a multithreaded process.
Processes Vs Threads
Threads operate in the same way as that of processes. Some of the similarities and
differences are:
Similarities
• Like processes, threads share CPU and only one thread active (running) at a time.
• Like processes, threads within a process, execute sequentially.
• Like processes, thread can create children.
• And like process, if one thread is blocked, another thread can run.
Differences
• Unlike processes, threads are not independent of one another.
• Unlike processes, all threads can access every address in the task.
• Unlike processes, threads are designed to assist one another. Note that processes might
or might not assist one another because processes may originate from different users.
Difference between Process and Thread
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.
Why Threads?
• Following are some reasons why we use threads in designing operating systems.
A process with multiple threads makes a great server for example printer server.
Because threads can share common data, they do not need to use inter process
communication.
Because of the very nature, threads can take advantage of multiprocessors.

• Threads are cheap in the sense that


They only need a stack and storage for registers therefore, threads are cheap to create.
Threads use very little resources of an operating system in which they are working. That is,
threads do not need new address space, global data, program code or operating system
resources.
Context switching are fast when working with threads. The reason is that we only have to save
and/or restore PC, SP and registers.
Types of Thread
• There are two types of threads :
i. User Threads
ii. Kernel Threads
User Level Threads
• In this case, the thread management kernel is not aware of the
existence of threads.
• These are user managed 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.
Advantages of User-level threads:
• 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.
• User level threads are fast to create and manage.
• 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 of user-level threads:


• 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. It is up to each thread to relinquish control to other threads.
• User-level threads require 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. For example, if one thread causes a page fault, the process blocks.
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.
• Advantages of kernel-level threads:
• 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 can simultaneously schedule multiple threads from the same process on
multiple processes.
• Kernel-level threads are especially good for applications that frequently block - If one
thread in a process is blocked, the Kernel can schedule another thread of the same
process.

• Disadvantages of kernel level threads:


• 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 requires 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.
Difference between User-Level & Kernel-
Level Thread
S.N. User-Level Threads Kernel-Level Thread

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

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


the user level. Kernel threads.

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

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


advantage of multiprocessing. multithreaded.
Multithreading Models
• Some operating system provide a combined user level thread and Kernel
level thread facility e.g. Solaris is a good example of this combined
approach.
• 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.

i. Many-To-One Model
ii. One-To-One Model
iii. Many-To-Many Model
Many-To-One Threading Model
• In the many-to-one model, many user-level threads are all mapped onto a
single kernel thread.
• Thread management is handled by the thread library in user space, which is
very efficient.
• However, if a blocking system call is made, then the entire process blocks,
even if the other user threads would otherwise be able to continue.
• Because a single kernel thread can operate only on a single CPU, the many-
to-one model does not allow individual processes to be split across multiple
CPUs.
• Green threads for Solaris and GNU Portable Threads implement the many-
to-one model in the past, but few systems continue to do so today.
Many-to-one threading model diagram
One-To-One Threading Model
• The one-to-one model creates a separate kernel thread to handle each
user thread.
• One-to-one model overcomes the problems listed above involving
blocking system calls and the splitting of processes across multiple CPUs.
• However the overhead of managing the one-to-one model is more
significant, involving more overhead and slowing down the system.
• Most implementations of this model place a limit on how many threads
can be created.
• Linux and Windows from 95 implement the one-to-one model for threads.
One-to-one threading model diagram
Many-To-Many Threading Model
• The many-to-many model multiplexes any number of user threads
onto an equal or smaller number of kernel threads, combining the
best features of the one-to-one and many-to-one models.
• Users have no restrictions on the number of threads created.
• Blocking kernel system calls do not block the entire process.
• Processes can be split across multiple processors.
• Individual processes may be allocated variable numbers of kernel
threads, depending on the number of CPUs present and other factors.
Many-To-Many Threading Model
diagram
Thread Libraries
• Thread libraries provides programmers with API for creating and managing of threads.
• Thread libraries may be implemented either in user space or in kernel space.
• The user space involves API functions implemented solely within user space, with no kernel support.
• The kernel space involves system calls, and requires a kernel with thread library support.
Application that Benefits from Threads
• A proxy server satisfying the requests for a number of computers on a LAN would be benefited by a
multi-threaded process.
• In general, any program that has to do more than one task at a time could benefit from multitasking.
• For example, a program that reads input, process it, and outputs could have three threads, one for
each task.
Application that cannot Benefit from Threads
• Any sequential process that cannot be divided into parallel task will not benefit from thread, as they
would block until the previous one completes.
• For example, a program that displays the time of the day would not benefit from multiple threads.
Benefits of Multithreading
• Responsiveness
• Resource sharing, hence allowing better utilization of resources - Threads
allow the sharing of a lot resources that cannot be shared in process, for
example, sharing code section, data section, Operating System resources
like open file etc.
• Economy. Creating and managing threads becomes easier.
• Scalability. One thread runs on one CPU. In Multithreaded processes,
threads can be distributed over a series of processors to scale.
• Context Switching is smooth. Context switching refers to the procedure
followed by CPU to change from one task to another.
Resources used in Thread
Creation and Process Creation
• When a new thread is created it shares its code section, data section and
operating system resources like open files with other threads.
• But it is allocated its own stack, register set and a program counter.
• The creation of a new process differs from that of a thread mainly in the fact
that all the shared resources of a thread are needed explicitly for each process.
• So though two processes may be running the same piece of code they need to
have their own copy of the code in the main memory to be able to run.
• Two processes also do not share other resources with each other.
• This makes the creation of a new process very costly compared to that of a
new thread.
Multithreading Issues
• Thread Cancellation - Thread cancellation means terminating a thread before it has finished working -
are no longer needed. There can be two approaches for this, one is Asynchronous cancellation, which
terminates the target thread immediately. The other is Deferred cancellation allows the target thread
to periodically check if it should be cancelled.
• Signal Handling - Signals are used in UNIX systems to notify a process that a particular event has
occurred. Now in when a Multithreaded process receives a signal, to which thread it must be
delivered?
• There are four major options: a) Deliver the signal to the thread to which the signal applies, b) Deliver
the signal to every thread in the process, c) Deliver the signal to certain threads in the process, d)
Assign a specific thread to receive all signals in a process.
• fork() System Call - fork() is a system call executed in the kernel through which a process creates a
copy of itself. Now the problem in Multithreaded process is, if one thread forks, will the entire process
be copied or not? The answers included a) its system dependent, b) If the new process executes right
away, there is no need to copy all the other threads. If it doesn't, then the entire process should be
copied, c) A: Many versions of UNIX provide multiple versions of the fork call for this purpose.
• Security Issues because of extensive sharing of resources between multiple threads.

You might also like