Module4 Thread PDF
Module4 Thread PDF
1
Threads
Module 4: THREADS
Threads Overview
Despite of the fact that a thread must execute in process, the process and its
associated threads are different concept. Processes are used to group resources together and
threads are the entities scheduled for execution on the CPU. 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. In a process, threads allow multiple executions
of streams. In many respect, threads are popular way to improve application through
parallelism. The CPU switches rapidly back and forth among the threads giving illusion that
the threads are running in parallel. 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.
Benefits
• Responsiveness – may allow continued execution if part of process is blocked,
especially important for user interfaces
• Resource Sharing – threads share resources of process, easier than shared
memory or message passing
• Economy – cheaper than process creation, thread switching lower overhead than
context switching
• Scalability – process can take advantage of multiprocessor architectures
Multi-Threading Model
Many-To-One Model
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.
Many-To-Many Model
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.
Two-Level Model
The two-level model (known commonly as the many-to-many model) is a strict many-to-
many model with the ability to specifically request a one-to-one binding for individual threads. This
model is probably the best of the choices. Several operating systems now use this model (Digital
UNIX, Solaris, IRIX, HP-UX, AIX). The JVMs on these OSs have the option of using any combination of
bound and unbound threads.
The choice of the threading model is an implementation-level decision for writers of the JVM.
Java itself has no concept of LWPs or threading models. This is a very reasonable choice by the Java
designers; Java programs shouldn't be looking at this kind of low-level detail. Unfortunately, it
brings in a very significant area of possible platform behavior difference.
CS-6206: Introduction to Operating System and Its Application
5
Threads
Thread Libraries
A thread library provides the programmer an API for creating and managing threads. There are two
primary ways of implementing a thread library. The first approach is to provide a library entirely in user
space with no kernel support. All code and data structures for the library exist in user space. This means
that invoking a function in the library results in a local function call in user space and not a system call.
The second approach is to implement a kernel-level library supported directly by the operating
system. In this case, code and data structures for the library exist in kernel space. Invoking a function in
the API for the library typically results in a system call to the kernel.
Three main thread libraries are in use today:
1. POSIX Pthreads,
2. Win32, and
3. Java. Pthreads,
The threads extension of the POSIX standard, may be provided as either a user- or kernel-level
library. The Win32 thread library is a kernel-level library available on Windows systems. The Java thread
API allows thread creation and management directly in Java programs. However, because in most
instances the JVM is running on top of a host operating system, the Java thread API is typically
implemented using a thread library available on the host system. This means that on Windows systems,
Java threads are typically implemented using the Win32 API; UNIX and Linux systems often use Pthreads.
Implicit Threading
One way to address the difficulties and better support the design of multithreaded applications is
to transfer the creation and management of threading from application developers to compilers and run-
time libraries. This, termed implicit threading, is a popular trend today.
Implicit threading is mainly the use of libraries or other language support to hide the management
of threads. The most common implicit threading library is OpenMP, in context of C.
OpenMP is a set of compiler directives as well as an API for programs written in C, C++, or FORTRAN
that provides support for parallel programming in shared-memory environments. OpenMP identifies
parallel regions as blocks of code that may run in parallel. Application developers insert compiler directives
Thread Pool
Thread pool is a group of pre-instantiated, idle threads which stand ready to be given work. These
are preferred over instantiating new threads for each task when there is a large number of short tasks to
be done rather than a small number of long ones. This prevents having to incur the overhead of creating a
thread a large number of times.
Implementation will vary by environment, but in simplified terms, you need the following:
• A way to create threads and hold them in an idle state. This can be accomplished by having each thread
wait at a barrier until the pool hands it work. (This could be done with mutexes as well.)
• A container to store the created threads, such as a queue or any other structure that has a way to add
a thread to the pool and pull one out.
• A standard interface or abstract class for the threads to use in doing work. This might be an abstract
class called Task with an execute() method that does the work and then returns.
When the thread pool is created, it will either instantiate a certain number of threads to make available or
create new ones as needed depending on the needs of the implementation.
When the pool is handed a Task, it takes a thread from the container (or waits for one to become available
if the container is empty), hands it a Task, and meets the barrier. This causes the idle thread to resume
execution, invoking the execute() method of the Task it was given. Once execution is complete, the thread
hands itself back to the pool to be put into the container for re-use and then meets its barrier, putting itself
to sleep until the cycle repeats.
Multi-Threaded Issues
Multithreaded programs allow the execution of multiple parts of a program at the same time. These parts
are known as threads and are lightweight processes available within the process.
Threads improve the application performance using parallelism. They share information like data segment,
code segment files etc. with their peer threads while they contain their own registers, stack, counter etc.
Some of the issues with multithreaded programs are as follows −
• Increased Complexity − Multithreaded processes are quite complicated. Coding for these can only
be handled by expert programmers.
CS-6206: Introduction to Operating System and Its Application
7
Threads