0% found this document useful (0 votes)
5 views

Module 2 Multi Threaded Programming

This document provides an overview of multithreaded programming, detailing the structure and benefits of threads, including responsiveness, resource sharing, economy, and scalability. It discusses various threading models, such as many-to-one, one-to-one, and many-to-many, as well as the implementation of thread libraries like POSIX Pthreads, Win32, and Java threads. Additionally, it addresses challenges in multicore programming and threading issues related to system calls.

Uploaded by

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

Module 2 Multi Threaded Programming

This document provides an overview of multithreaded programming, detailing the structure and benefits of threads, including responsiveness, resource sharing, economy, and scalability. It discusses various threading models, such as many-to-one, one-to-one, and many-to-many, as well as the implementation of thread libraries like POSIX Pthreads, Win32, and Java threads. Additionally, it addresses challenges in multicore programming and threading issues related to system calls.

Uploaded by

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

Module-2

Multi-threaded Programming

Department of CSE- Data Science


Contents

 Overview

 Multithreading models

 Thread Libraries

 Threading issues.

Department of CSE- Data Science


Overview
 A thread is a basic unit of CPU utilization. It comprises a thread ID, a program
counter, a register set, and a stack.
 It shares with other threads belonging to the same process its code section, data
section, and other operating-system resources, such as open files and signals.
 A traditional (or heavyweight) process has a single thread of control. If a process has
multiple threads of control, it can perform more than one task at a time.

Department of CSE- Data Science


Department of CSE- Data Science
 Many software packages that run on modern desktop PCs are multithreaded.
 An application typically is implemented as a separate process with several threads of
control.
 A Web browser might have one thread display images or text while another thread
retrieves data from the network, for example.
 A word processor may have a thread for displaying graphics, another thread for
responding to keystrokes from the user, and a third thread for performing spelling
and grammar checking in the background.

Department of CSE- Data Science


 In certain situations, a single application may be required to perform several similar tasks.
 For example, a Web server accepts client requests for Web pages, images, sound, and so
forth.
‣ problem:
- A busy Web server may have several clients concurrently accessing it.
- If the Web server ran as a traditional single threaded process, it would be able to
service only one client at a time
- client might have to wait a very long time for its request to be serviced.
‣ solution
- Have the server run as a single process that accepts requests.
- When the server receives a request, it creates a separate process to service that
request.
- Process creation is time consuming and resource intensive

Department of CSE- Data Science


 More efficient to use one process that contains multiple threads.
 If the Web-server process is multithreaded, the server will create a separate thread that
listens for client requests.
 When a request is made, rather than creating another process, the server will create a
new thread to service the request and resume listening for additional requests.

Figure : Multithreaded server architecture

Department of CSE- Data Science


 Threads also play a vital role in remote procedure call (RPC) systems. Typically, RPC
servers are multithreaded.
‣ When a server receives a message, it services the message using a separate thread.
‣ This allows the server to service several concurrent requests.
 Finally, most operating system kernels are now multithreaded

‣ several threads operate in the kernel, and each thread performs a specific task,
such as managing devices or interrupt handling.
‣ For example Solaris creates a set of threads in the kernel specifically for interrupt
handling;
‣ Linux uses a kernel thread for managing the amount of free memory in the system.

Department of CSE- Data Science


Benefits
 The benefits of multithreaded programming can be broken down into four major categories:
1. Responsiveness.
‣ Multithreading an interactive application may allow a program to continue running even
if part of it is blocked or is performing a lengthy operation, thereby increasing
responsiveness to the user.
‣ For instance a multithreaded Web browser could allow user interaction in one thread
while an image was being loaded in another thread.
2. Resource sharing.
‣ Threads share the memory and the resources of the process to which they belong by
default.
‣ The benefit of sharing code and data is that it allows an application to have several
different threads of activity within the same address space.

Department of CSE- Data Science


3. Economy.
‣ Because threads share the resources of the process to which they belong, it is
more economical to create and context-switch threads
‣ In Solaris for example, creating a process is about thirty times slower than is
creating a thread, and context switching is about five times slower.
4. Scalability.
‣ The benefits of multithreading can be greatly increased in a multiprocessor
architecture, where threads may be running in parallel on different processors.
‣ Multithreading on a multiCPU machine increases parallelism.

Department of CSE- Data Science


Multicore Programming
 A recent trend in system design has been to place multiple computing cores on a single
chip, where each core appears as a separate processor to the operating system
 Multithreaded programming provides a mechanism for more efficient use of multiple
cores and improved concurrency.
 Consider an application with four threads.
‣ On a system with a single computing core, concurrency merely means that the
execution of the threads will be interleaved over time , as the processing core is
capable of executing only one thread at a time.

Department of CSE- Data Science


‣ On a system with multiple cores, concurrency means that the threads can run in
parallel, as the system can assign a separate thread to each core

 The trend towards multicore systems has placed pressure on system designers as well as
application programmers to make better use of the multiple computing cores.
‣ Designers of operating systems must write scheduling algorithms that use multiple
processing cores to allow the parallel execution
‣application programmers challenge is to modify existing programs as well as design
new programs that are multithreaded to take advantage of multicore systems.

Department of CSE- Data Science


 In general, five areas present challenges in programming for multicore systems:
1.Dividing activities. This involves examining applications to find areas that can be
divided into separate, concurrent tasks and thus can run in parallel on individual
cores.
2.Balance. While identifying tasks that can run in parallel, programmers must also
ensure that the tasks perform equal work of equal value
3.Data splitting. Just as applications are divided into separate tasks, the data accessed
and manipulated by the tasks must be divided to run on separate cores.

Department of CSE- Data Science


4. Data dependency. The data accessed by the tasks must be examined for
dependencies between two or more tasks. In instances where one task depends on
data from another, programmers must ensure that the execution of the tasks is
synchronized to accommodate the data dependency.
5. Testing and debugging. When a program is running in parallel on multiple cores,
there are many different execution paths. Testing and debugging such concurrent
programs is inherently more difficult than testing and debugging single-threaded
applications

Department of CSE- Data Science


Multithreading models

TYPES OF THREADS
 User Level Threads (User managed threads)
 Kernel Level Threads (Operating System managed threads acting on kernel, an
operating system core)
 All contemporary operating systems-including Windows XP, Linux, Mac OS X,
Solaris, and Tru64 UNIX (formerly Digital UNIX)-support kernel threads.
 A relationship must exist between user threads and kernel threads.
 Three common ways of establishing such a relationship

1. Many to One Model

2. One to One Model

3. Many to Many Model


Department of CSE- Data Science
1. Many to One Model
 Many-to-one model maps many user level threads to one
Kernel-level thread.
 Advantage: Thread management is done in user space by the
thread library.
 Disadvantages:
1. When thread makes a blocking system call, the entire
process will be blocked.
2. Only one thread can access the Kernel at a time, so
multiple threads are unable to run in parallel on
Fig: Many-to-one model
multiprocessors.
• Examples:
– Solaris Green Threads
– GNU Portable Threads
Department of CSE- Data Science
2. One to One Model
 There is one-to-one relationship of user-level thread to the kernel-level thread.

Fig: one-to-one model

 Advantages:
1. This model provides more concurrency than the many-to-one model.
2. It also allows another thread to run when a thread makes a blocking system call.
3. It supports multiple threads to execute in parallel on multiprocessors.

Department of CSE- Data Science


 Disadvantage
1.The only drawback to this model is that creating a user thread requires creating
the corresponding kernel thread.
 Because the overhead of creating kernel threads can burden the performance of an
application, most implementations of this model restrict the number of threads
supported by the system.
 Linux, along with the family of Windows operating systems, implement the one-to-
one model.

Department of CSE- Data Science


3. Many to Many Model
 The many-to-many model multiplexes any number of
user threads onto an equal or smaller number of
kernel threads.
 Advantages:
1. Developers can create as many user threads as
necessary
2. The kernel threads can run in parallel on a
multiprocessor.
3. When a thread performs a blocking system-call,
kernel can schedule another thread for execution. Fig: Many-to-Many model

Department of CSE- Data Science


 Two-level Model

‣ Similar to Many to Many, except that it allows a user thread to be bound to kernel
thread
‣ Examples
- IRIX
- HP-UX
- Solaris 8 and earlier

Fig: Two-level Model

Department of CSE- Data Science


Thread Libraries

 Thread libraries provide programmers with Application programming interface


(API) for creation and management of threads.
 There are two primary ways of implementing a thread library.
1. 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.
2. 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.

Department of CSE- Data Science


Most commonly used Thread Libraries are
1. POSIX Ptheads
‣ extension of the POSIX standard, may be provided as either a user- or kernel-
level library.
2. Win32
‣ a kernel-level library available on Windows systems
3. Java threads
‣The Java thread API allows threads to be created and managed directly in Java
programs.
‣ Since Java generally runs on a Java Virtual Machine, the implementation of
threads is based upon whatever OS and hardware the JVM is running on, i.e.
either Ptheads or Win32 threads depending on the system.
Department of CSE- Data Science
Pthreads
 May be provided either as user-level or kernel-level
 A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
 API specifies behavior of the thread library, implementation is up to development of
the library
 Common in UNIX operating systems (Solaris, Linux, Mac OS X)
 Example :a multithreaded program that performs the summation of a non-negative
integer in a separate thread using the well-known summation function:
sum=
For example, if N were 5, this function would represent the summation of integers
from 0 to 5, which is 15.

Department of CSE- Data Science


// Multithreaded C program using the Pthreads API.

 When this program begins, a single


thread of control begins in main ().
 After some initialization, main ()
creates a second thread that begins
control in the runner () function.
 Both threads share the global data
sum.

 All Pthreads programs must include the pthread. h header file.


 The statement pthread_t tid declares the identifier for the thread we will create.
 Each thread has a set of attributes, including stack size and scheduling
information.
 The pthread_attr_t attr declaration represents the attributes for the thread.

Department of CSE- Data Science


 A separate thread is created with the
pthread_create () function call.
 In addition to passing the thread
identifier and the attributes for the
thread, we also pass the name of the
function where the new thread will
begin execution-in this case, the runner
() function.
 Last, we pass the integer parameter
that was provided on the command
line, argv [1].

Department of CSE- Data Science


 At this point, the program has two threads: the initial (or parent) thread in main()
and the summation (or child) thread performing the summation operation in the
runner() function.
 After creating the summation thread, the parent thread will wait for it to complete
by calling the pthread_join() function.
 The summation thread will complete when it calls the function pthread_exit ().
 Once the summation thread has returned, the parent thread will output the value of
the shared data sum.

Department of CSE- Data Science


Win32 Threads
 The technique for creating threads using the Win32 thread library is similar to the
Pthreads technique in several ways.
// Multithreaded C program using the Win32 API.
 Threads are created in the Win32 API
using the CreateThread() function,
and-just as in Pthreads-a set of
attributes for the thread is passed to
this function.
 These attributes include security
information, the size of the stack, and
a flag that can be set to indicate if the
thread is to start in a suspended state.
Department of CSE- Data Science
 In this program, we use the default values for these attributes (which do not
initially set the thread to a suspended state and instead make it eligible to be run by
the CPU scheduler).
 Once the summation thread is created, the parent must wait for it to complete
before outputting the value of Sum, as the value is set by the summation thread.

Department of CSE- Data Science


 WaitForSingleObject ()function, which causes the creating thread to block until the
summation thread has exited.

Department of CSE- Data Science


Java Threads
 Java threads are managed by the JVM
 Typically implemented using the threads model provided by underlying OS
 Java threads may be created by:
- Extending Thread class
- Implementing the Runnable interface

Department of CSE- Data Science


 Thread creation is performed by
creating an object instance of
the Thread class and passing the
constructor a Runnable object.
 Creating a Thread object does
not specifically create the new
thread; rather, it is the start()
method that creates the new
thread.

Department of CSE- Data Science


 Calling the start() method for the
new object does two things:
1. It allocates memory and
initializes a new thread in the
JVM.
2. It calls the run() method,
making the thread eligible to be
run by the JVM.

Department of CSE- Data Science


 When the summation program runs, two threads are created by the JVM.
 The first is the parent thread, which starts execution in the main () method.
 The second thread is created when the start() method on the Thread object is
invoked.
 This child thread begins execution in the run () method of the Summation class.
 After outputting the value of the summation, this thread terminates when it exits
from its run() method.
 Sharing of data between threads occurs easily in Win32 and Pthreads, since shared
data are simply declared globally.
 As a pure object-oriented language, Java has no such notion of global data; if two or
more threads are to share data in a Java program, the sharing occurs by passing
references to the shared object to the appropriate threads.

Department of CSE- Data Science


 The main thread and the summation thread share the object instance of the Sum
class.
 This shared object is referenced through the appropriate getSum () and setSum()
methods.
 The join() method - to wait for the summation threads to finish before

proceeding.

Department of CSE- Data Science


Threading Issues

 Semantics of fork() and exec() system calls

 Thread cancellation
 Signal handling

 Thread pools

 Thread specific data


 Scheduler activations

Department of CSE- Data Science


Semantics of fork() and exec()
 The semantics of the fork() and exec() system calls change in a multithreaded program.

 If one thread in a program calls fork(),does the new process duplicates all threads or is the
new process single-threaded?
 Some UNIX systems have chosen to have two versions of fork(),one that duplicates all
threads and another duplicates only thread that invoked the fork() system call.
 Which of the two versions of fork() to use depends on the application.

 If thread invokes the exec system call the program specified in the parameter to

exec () will replace the entire process including all threads


 If exec() is called immediately after forking, then duplicating all threads is unnecessary,
as the program specified in the parameters to exec() will replace the process. In this
instance, duplicating only the calling thread is appropriate.

Department of CSE- Data Science


Thread Cancellation
 Thread Cancellation is the task of terminating a thread before it has completed.
 A thread that is to be canceled is often referred to as the target thread
 Cancellation of a target thread may occur in two different scenarios:

1. Asynchronous cancellation terminates the target thread immediately


( De-allocation of resources will be incomplete)
2. Deferred cancellation allows the target thread to periodically check if it
should be cancelled

Department of CSE- Data Science


 The difficulty with cancellation occurs in situations where resources have been allocated
to a canceled thread or where a thread is canceled while in the midst of updating data it
is sharing with other threads.
 This becomes especially troublesome with asynchronous cancellation. Often, the
operating system will reclaim system resources from a canceled thread but will not
reclaim all resources.
 Therefore, canceling a thread asynchronously may not free a necessary system-wide
resource.
 With deferred cancellation, in contrast, one thread indicates that a target thread is to be
canceled, but cancellation occurs only after the target thread has checked a flag to
determine whether or not it should be canceled.
 The thread can perform this check at a point at which it can be canceled safely. Pthreads
refers to such points as cancellation points.
Department of CSE- Data Science
Signal Handling
 In UNIX, a signal is used to notify a process that a particular event has occurred.
 All signals follow this pattern:
1. A signal is generated by the occurrence of a certain event.
2. A generated signal is delivered to a process.
3. Once delivered, the signal must be handled.
 A signal handler is used to process signals.
 A signal may be received either synchronously or asynchronously, depending on the source.
1. Synchronous signals
- Delivered to the same process that performed the operation causing the signal.
- E.g. illegal memory access and division by 0.
2. Asynchronous signals
- Generated by an event external to a running process.
- E.g. user terminating a process with specific keystrokes<ctrl><c>.
Department of CSE- Data Science
 Every signal can be handled by one of two possible handlers:
1. A Default Signal Handler
- Run by the kernel when handling the signal.
2. A User-defined Signal Handler
- Overrides the default signal handler.
 In single-threaded programs, delivering signals is simple (since signals are always delivered
to a process).
 In multithreaded programs, delivering signals is more complex. Then, the following options
exist:
1. Deliver the signal to the thread to which the signal applies.
2. Deliver the signal to every thread in process
3. Deliver the signal to certain threads in the process.
4. Assign a specific thread to receive all signals for the process.

Department of CSE- Data Science


Thread Pools
 The basic idea is to

‣ create a no. of threads at process-startup and


‣ place the threads into a pool (where they sit and wait for work).
 Procedure:
1. When a server receives a request, it awakens a thread from the pool.
2. If any thread is available, the request is passed to it for service.
3. Once the service is completed, the thread returns to the pool.
 Advantages:

‣ Servicing a request with an existing thread is usually faster than waiting to create a
thread.
‣ The pool limits the no. of threads that exist at any one point.
Department of CSE- Data Science
 No. of threads in the pool can be based on actors such as

‣ no. of CPUs

‣ amount of memory and

‣ expected no. of concurrent client-requests.

Department of CSE- Data Science


Thread Specific Data
 Threads belonging to a process share the data of the process.
 This sharing of data provides one of the benefits of multithreaded
programming.
 In some circumstances, each thread might need its own copy of certain data.
We will call such data thread-specific data.
 For example, in a transaction-processing system, we might service each
transaction in a separate thread.
 Furthermore, each transaction may be assigned a unique identifier. To associate
each thread with its unique identifier, we could use thread-specific data.

Department of CSE- Data Science


Scheduler Activations
 Both Many to Many and Two-level models require communication to maintain the
appropriate number of kernel threads allocated to the application.
 One scheme for communication between the user-thread library and the kernel is known as
scheduler activation.
 The kernel provides an application with a set of virtual processors (LWPs), and the
application can schedule user threads onto an available virtual processor.
 Scheduler activations provide upcalls, a communication mechanism from the kernel to the
thread library.
• Upcalls are handled by the thread library with an and upcall handler, and upcall handlers
must run on a virtual processor.
 This communication allows an application to maintain the correct number kernel threads

Department of CSE- Data Science


Question Bank
From Model Question Paper
1 Discuss in detail the multithreading model, its advantages and disadvantages with
suitable illustration. 7 marks
From Previous year question papers
1 What is multithreaded process? Explain the four benefits of multithreaded
programming. Dec’23/Jan’24 6 marks
A multithreaded process is a program execution model that allows multiple threads
to run concurrently within a single process.
2 Explain the various multithreading model and benefits of multithreaded programs.
Jan/Feb’21 6 marks
3 Explain the three multithreading model. Aug/Sept’20 6 marks
4 Why thread is called LWP? Describe any one threading model and threading issue.
Aug/Sept’20 4 marks
Threads are called lightweight processes (LWPs) because they require fewer
resources to create than processes, and they share resources with other
threads and the process they're part of.

Department of CSE- Data Science

You might also like