Amrita
School
of
Engineering,
Bangalore
Ms. Harika Pudugosula
Teaching Assistant
Department of Electronics & Communication Engineering
• Overview
• Multicore Programming
• Multithreading Models
• Thread Libraries
• Implicit Threading
• Threading Issues
• Operating System Examples
2
Multithreading Models
• Types of Threads
1. User Threads
- Supported above the kernel and are managed without kernel support
- These threads are managed by the user level thread library
- Kernel knows nothing about the 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.
- All thread creation and scheduling is done in user space
- Advantage- fast to create and manage
- Disadvantage- if the kernel is single threaded then any user level thread
performing a blocking s/m call will cause entire process to block even if other
threads available to run within application
3
Multithreading Models
• Types of Threads
2. Kernel Threads
- Supported and managed directly by the operating system
- These threads are managed by the kernel
- All modern operating systems support kernel level threads
- Kernel performs- thread creation, scheduling, management
- Advantage - if thread performs blocking s/m call kernel can schedule another
thread in application for execution
- Disadvantage - slower to create and manage threads
• Ultimately, a relationship must exist between user threads and kernel threads
• Many-to-One Model
• One-to-One Model
• Many-to-Many Model
4
Many-to-One Model
• Maps many user-level threads to one
kernel thread
• Thread management is done by the
thread library in user space, so it is
efficient
• Limitation - the entire process will block
if a thread makes a blocking system call
• Limitation - because only one thread can
access the kernel at a time, multiple
threads are unable to run in parallel on
multicore systems
5
One-to-One Model
• Maps each user thread to a kernel thread
• Provides more concurrency than the many-to-
one model by allowing another thread to run
when a thread makes a blocking system call
• Also allows multiple threads to run in parallel on
multiprocessors
• Limitation - 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 systems
6
Many-to-Many Model
• Multiplexes many user-level threads to a smaller
or equal number of kernel threads
• The number of kernel threads may be specific to
either a particular application or a particular
machine
• Developer to create as many user threads as
necessary, and the corresponding kernel threads
can run in parallel on a multiprocessor
• When a thread performs a blocking system call,
the kernel can schedule another thread for
execution
7
Two-level model
• It enables the processor to execute two threads,
or sets of instructions, at the same time
• It allows threads to be executed in parallel, it is
almost like having two seperate processors
working together
• Also known as Hyperthreading, Simultaneous
Multithreading (SMT)
8
9
One thread blocking causes all threads to block. Identify the type of multithreading
model and explain with neat diagram.
Amdahl’s Law
• Identifies performance gains from adding additional cores to an application that
has both serial and parallel components
• S is serial portion
• N processing cores
• That is, if application is 75% parallel / 25% serial, moving from 1 to 2 cores results in
speedup of 1.6 times
• As N approaches infinity, speedup approaches 1 / S
• Principle behind Amdahl’s Law : Serial portion of an application has
disproportionate effect on performance gained by adding additional cores
• But does the law take into account contemporary multicore systems?
• No, Amdahl’s Law may cease to be applicable as the number of processing cores
continues to increase on modern computer systems
Amdahl’s Law
• Using Amdahl’s Law, calculate the speedup gain: 67 percent parallel with
a) two processing cores
b) four processing cores
• If 60% of the computation can be parallelized, Using Amdahl’s law what is the
maximum speed up achievable using 4 processor.
• By adding cores will definitely have an impact on the improvement of the system
• If the system has more serial operations than parallel operations, adding cores will
not have much improvement
Threads Libraries
• A thread library provides the programmer with an API for creating and managing
threads
• There are two primary ways of implementing a thread library
• First approach -
• 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
• Second approach -
• To implement a kernel-level library supported directly by the operating system
• All code and data structures for the library exist in kernel space
• This means invoking a function in the API for the library typically results in a
system call to the kernel
12
Threads Libraries
• Three main thread libraries are in use today: POSIX Pthreads, Windows, and Java
• Pthreads, the threads extension of the POSIX standard, may be provided as either a
user-level or a kernel-level library
• The Windows thread library is a kernel-level library available on Windows systems
• The Java thread API allows threads to be created and managed directly in Java
programs
• Most instances the JVM is running on top of a host operating system, the Java thread
API is generally implemented using a thread library available on the host system
• So, on Windows systems, Java threads are typically implemented using the
Windows API; on UNIX and Linux systems often use Pthreads
• For POSIX and Windows threading, any data declared globally—that is, declared
outside of any function—are shared among all threads belonging to the same
process
13
Threads Libraries
• Java has no notion of global data, access to shared data must be explicitly arranged
between threads
• Data declared local to a function are typically stored on the stack
• Two general strategies for creating multiple threads:
- Asynchronous threading
- Synchronous threading
• Asynchronous threading
• Once the parent creates a child thread, the parent resumes its execution, so that
the parent and child execute concurrently
• Each thread runs independently of every other thread, and the parent thread
need not know when its child terminates
• Because the threads are independent, there is typically little data sharing
between threads
• Example: Multithreaded server (refer topic: Motivation)
14
Threads Libraries
• Two general strategies for creating multiple threads:
- Asynchronous threading
- Synchronous threading
• Synchronous threading
• When the parent thread creates one or more children and then parent must wait
for all of its children to terminate before it resumes - fork-join strategy
• Here, the threads(children) created by the parent perform work concurrently,
but the parent cannot continue until threads(children) work has been
completed
• Once each thread(child) has finished its work, it terminates and joins with its
parent
• Only after all of the children have joined can the parent resume execution
• Involves significant data sharing among threads
• Example: The parent thread may combine the results calculated by its various
children 15
Pthreads
• Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread
creation and synchronization
• This is a specification for thread behavior, not an implementation
• Operating-system designers may implement the specification in any way they wish
• In a Pthreads program, separate threads begin execution in a specified function
• Example: the basic Pthreads API for constructing a multithreaded program that
calculates the summation of a non-negative integer in a separate thread
16
Pthreads Example
17
#include <pthread.h>
#include <stdio.h>
int sum; /* this data is shared by the thread(s) */
void *runner(void *param); /* threads call this function */
int main(int argc, char *argv[])
{
pthread t tid; /* the thread identifier for the thread that we create */
pthread attr t attr; /* represents the attributes for the thread, we set the
attributes in the function call pthread attr init(&attr)*/
if (argc != 2) {
fprintf(stderr,"usage: a.out <integer value>n");
return -1; }
if (atoi(argv[1]) < 0) {
fprintf(stderr,"%d must be >= 0n",atoi(argv[1]));
return -1; }
Pthreads Example
18
pthread attr init(&attr); /* get the default attributes */
pthread create(&tid,&attr,runner,argv[1]); /* get the default attributes */
pthread join(tid,NULL); /* wait for the thread to exit */
printf("sum = %dn",sum);
}
void *runner(void *param) /* The thread will begin control in this function */
{
int i, upper = atoi(param);
sum = 0;
for (i = 1; i <= upper; i++)
sum += i;
pthread exit(0);
}
Pthreads Code for Joining 10 Threads
19
#define NUM THREADS 10
/* an array of threads to be joined upon */
pthread t workers[NUM THREADS];
for (int i = 0; i < NUM THREADS; i++)
pthread join(workers[i], NULL);
Windows Threads
• The technique for creating threads using the Windows thread library is similar to the
Pthreads technique
• Must include the windows.h header file when using the Windows API
• Data shared by the separate threads (similar to Pthreads)
20
Windows Threads
21
Windows Threads
22
Windows Threads
• In situations that require waiting for multiple threads to complete, the
WaitForMultipleObjects() function is used. This function is passed four parameters:
1. The number of objects to wait for
2. A pointer to the array of objects
3. A flag indicating whether all objects have been signaled
4. A timeout duration (or INFINITE)
• For example, if THandles is an array of thread HANDLE objects of size N, the parent
thread can wait for all its child threads to complete with this statement:
WaitForMultipleObjects(N, THandles, TRUE, INFINITE);
23
Java Threads
• Threads are the fundamental model of program execution in a Java program, and the
Java language and its API provide a rich set of features for the creation and
management of threads
• All Java programs comprise at least a single thread of control—even a simple Java
program consisting of only a main() method runs as a single thread in the JVM
• There are two techniques for creating threads in a Java program
• One approach is to create a new class that is derived from the Thread class and to
override its run() method
• An alternative—and more commonly used— technique is to define a class that
implements the Runnable interface
public interface Runnable{
public abstract void run(); }
24
Java Threads
• An alternative—and more commonly used— technique is to define a class that
implements the Runnable interface
public interface Runnable {
public abstract void run(); }
• When a class implements Runnable, it must define a run() method
• The code implementing the run() method is what runs as a separate thread
• Example:
• Java version of a multithreaded program that determines the summation of a non-
negative integer
• The Summation class implements the Runnable interface
• Thread creation is performed by creating an object instance of the Thread class and
passing the constructor a Runnable object
25
Java Threads
• Creating a Thread object does not specifically create the new thread; rather, the
start() method creates the new thread. 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
• 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
• The parent threads in the Pthreads and Windows libraries use pthread join() and
WaitForSingleObject() to wait for the summation threads to finish before proceeding
• The join() method in Java provides similar functionality
26
Java Multithreaded Program
27
Java Multithreaded Program (Cont.)
28
29
References
1. Silberscatnz and Galvin, “Operating System Concepts,” Ninth Edition, John
Wiley and Sons, 2012.
30
Thank you