SlideShare a Scribd company logo
1 of 96
Download to read offline
Threads
Amir H. Payberah
amir@sics.se
Amirkabir University of Technology
(Tehran Polytechnic)
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 1 / 45
Motivation
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 2 / 45
Thread
A basic unit of CPU utilization.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 3 / 45
Threads (1/3)
A traditional process: has a single thread.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45
Threads (1/3)
A traditional process: has a single thread.
Multiple threads in a process: performing more than one task at a
time.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45
Threads (1/3)
A traditional process: has a single thread.
Multiple threads in a process: performing more than one task at a
time.
Threads in a process share code section, data section, and other OS
resources, e.g., open files.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45
Threads (2/3)
Multiple tasks of an application can be implemented by separate
threads.
• Update display
• Fetch data
• Spell checking
• Answer a network request
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 5 / 45
Threads (3/3)
Multi-threaded web-server architecture
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 6 / 45
Threads Benefits (1/2)
Responsiveness: may allow continued execution if part of process is
blocked, especially important for user interfaces.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 7 / 45
Threads Benefits (1/2)
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.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 7 / 45
Threads Benefits (2/2)
Economy: cheaper than process creation, thread switching lower
overhead than context switching.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 8 / 45
Threads Benefits (2/2)
Economy: cheaper than process creation, thread switching lower
overhead than context switching.
Scalability: process can take advantage of multiprocessor architec-
tures
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 8 / 45
Context Switching vs. Threads Switching
Inter-process switching: context switching from process-to-process.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 9 / 45
Context Switching vs. Threads Switching
Inter-process switching: context switching from process-to-process.
Intra-process switching: switching from thread-to-thread.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 9 / 45
Context Switching vs. Threads Switching
Inter-process switching: context switching from process-to-process.
Intra-process switching: switching from thread-to-thread.
The cost of intra-process switching is less than the cost of inter-
process switching.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 9 / 45
Multicore Programming
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 10 / 45
Multiprocessor and Multicore Systems (1/2)
Users need more computing performance: single-CPU → multi-CPU
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 11 / 45
Multiprocessor and Multicore Systems (1/2)
Users need more computing performance: single-CPU → multi-CPU
A similar trend in system design: place multiple computing cores on
a single chip.
• Each core appears as a separate processor.
• Multi-core systems.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 11 / 45
Multiprocessor and Multicore Systems (2/2)
Multi-threaded programming
• More efficient use of multiple cores.
• Improved concurrency.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 12 / 45
Concurrency vs. Parallelism (1/2)
Parallelism
• Performing more than one task simultaneously.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
Concurrency vs. Parallelism (1/2)
Parallelism
• Performing more than one task simultaneously.
Concurrency
• Supporting more than one task by allowing all the tasks to make
progress.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
Concurrency vs. Parallelism (1/2)
Parallelism
• Performing more than one task simultaneously.
Concurrency
• Supporting more than one task by allowing all the tasks to make
progress.
• Single processor/core: scheduler providing concurrency.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
Concurrency vs. Parallelism (1/2)
Parallelism
• Performing more than one task simultaneously.
Concurrency
• Supporting more than one task by allowing all the tasks to make
progress.
• Single processor/core: scheduler providing concurrency.
• It is possible to have concurrency without parallelism.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
Concurrency vs. Parallelism (2/2)
Concurrent execution on a single-core system.
Parallelism on a multi-core system.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 14 / 45
Types of Parallelism
Data parallelism
• Distributes subsets of the same data across multiple cores, same
operation on each.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 15 / 45
Types of Parallelism
Data parallelism
• Distributes subsets of the same data across multiple cores, same
operation on each.
Task parallelism
• Distributes threads across cores, each thread performing unique
operation.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 15 / 45
Programming Challenges
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 16 / 45
Multi-threading Models
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 17 / 45
User Threads and Kernel Threads
User threads: management done by user-level threads library.
Kernel threads: supported by the Kernel.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 18 / 45
User Threads and Kernel Threads
User threads: management done by user-level threads library.
• Three primary thread libraries:
• POSIX pthreads
• Windows threads
• Java threads
Kernel threads: supported by the Kernel.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 18 / 45
User Threads and Kernel Threads
User threads: management done by user-level threads library.
• Three primary thread libraries:
• POSIX pthreads
• Windows threads
• Java threads
Kernel threads: supported by the Kernel.
• All general purpose operating systems, including:
Windows, Solaris, Linux, Tru64 UNIX, Mac OS X
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 18 / 45
Multi-Threading Models
Many-to-One
One-to-One
Many-to-Many
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 19 / 45
Many-to-One Model
Many user-level threads mapped to single kernel thread.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
Many-to-One Model
Many user-level threads mapped to single kernel thread.
One thread blocking causes all to block.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
Many-to-One Model
Many user-level threads mapped to single kernel thread.
One thread blocking causes all to block.
Multiple threads may not run in parallel on multicolor system
because only one may be in kernel at a time.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
Many-to-One Model
Many user-level threads mapped to single kernel thread.
One thread blocking causes all to block.
Multiple threads may not run in parallel on multicolor system
because only one may be in kernel at a time.
Few systems currently use this model.
• Solaris green threads
• GNU portable threads
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
One-to-One Model
Each user-level thread maps to a kernel thread.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
One-to-One Model
Each user-level thread maps to a kernel thread.
Creating a user-level thread creates a kernel thread.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
One-to-One Model
Each user-level thread maps to a kernel thread.
Creating a user-level thread creates a kernel thread.
More concurrency than many-to-one.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
One-to-One Model
Each user-level thread maps to a kernel thread.
Creating a user-level thread creates a kernel thread.
More concurrency than many-to-one.
Number of threads per process sometimes restricted due to
overhead.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
One-to-One Model
Each user-level thread maps to a kernel thread.
Creating a user-level thread creates a kernel thread.
More concurrency than many-to-one.
Number of threads per process sometimes restricted due to
overhead.
Examples:
• Windows
• Linux
• Solaris 9 and later
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
Many-to-Many Model
Allows many user-level threads to be mapped to many kernel
threads.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45
Many-to-Many Model
Allows many user-level threads to be mapped to many kernel
threads.
Allows the OS to create a sufficient number of kernel threads.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45
Many-to-Many Model
Allows many user-level threads to be mapped to many kernel
threads.
Allows the OS to create a sufficient number of kernel threads.
Examples:
• Solaris prior to version 9
• Windows with the ThreadFiber package
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45
Thread Libraries
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 23 / 45
Thread Libraries (1/2)
Thread library provides programmer with API for creating and
managing threads.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 24 / 45
Thread Libraries (1/2)
Thread library provides programmer with API for creating and
managing threads.
Two primary ways of implementing:
• Library entirely in user-space.
• Kernel-level library supported by the OS.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 24 / 45
Thread Libraries (2/2)
Pthread
• Either a user-level or a kernel-level library.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45
Thread Libraries (2/2)
Pthread
• Either a user-level or a kernel-level library.
Windows thread
• Kernel-level library.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45
Thread Libraries (2/2)
Pthread
• Either a user-level or a kernel-level library.
Windows thread
• Kernel-level library.
Java thread
• Uses a thread library available on the host system.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 26 / 45
Pthreads
A POSIX API for thread creation and synchronization.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
Pthreads
A POSIX API for thread creation and synchronization.
Specification, not implementation.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
Pthreads
A POSIX API for thread creation and synchronization.
Specification, not implementation.
API specifies behavior of the thread library, implementation is up
to development of the library.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
Pthreads
A POSIX API for thread creation and synchronization.
Specification, not implementation.
API specifies behavior of the thread library, implementation is up
to development of the library.
Common in UNIX OSs, e.g., Solaris, Linux, Mac OS X
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
Thread ID
The thread ID (TID) is the thread analogue to the process ID (PID).
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
Thread ID
The thread ID (TID) is the thread analogue to the process ID (PID).
The PID is assigned by the Linux kernel, and TID is assigned in the
Pthread library.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
Thread ID
The thread ID (TID) is the thread analogue to the process ID (PID).
The PID is assigned by the Linux kernel, and TID is assigned in the
Pthread library.
Represented by pthread t.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
Thread ID
The thread ID (TID) is the thread analogue to the process ID (PID).
The PID is assigned by the Linux kernel, and TID is assigned in the
Pthread library.
Represented by pthread t.
Obtaining a TID at runtime:
#include <pthread.h>
pthread_t pthread_self(void);
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
Creating Threads
pthread create() defines and launches a new thread.
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
start routine has the following signature:
void *start_thread(void *arg);
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 29 / 45
Terminating Threads
Terminating yourself by calling pthread exit().
#include <pthread.h>
void pthread_exit(void *retval);
Terminating others by calling pthread cancel().
#include <pthread.h>
int pthread_cancel(pthread_t thread);
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 30 / 45
Joining and Detaching Threads
Joining allows one thread to block while waiting for the termination
of another.
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
int pthread_detach(pthread_t thread);
[https://computing.llnl.gov/tutorials/pthreads/#Joining]
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 31 / 45
Joining and Detaching Threads
Joining allows one thread to block while waiting for the termination
of another.
You use join if you care about what value the thread returns when
it is done, and use detach if you do not.
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
int pthread_detach(pthread_t thread);
[https://computing.llnl.gov/tutorials/pthreads/#Joining]
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 31 / 45
A Threading Example
void *start_thread(void *message) {
printf("%sn", (const char *)message);
return message;
}
int main(void) {
pthread_t thread11, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
// Create two threads, each with a different message.
pthread_create(&thread1, NULL, start_thread, (void *)message1);
pthread_create(&thread2, NULL, start_thread, (void *)message2);
// Wait for the threads to exit.
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 32 / 45
Implicit Threading
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 33 / 45
Implicit Threading
Increasing the number of threads: program correctness more difficult
with explicit threads.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45
Implicit Threading
Increasing the number of threads: program correctness more difficult
with explicit threads.
Implicit threading: creation and management of threads done by
compilers and run-time libraries rather than programmers.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45
Implicit Threading
Increasing the number of threads: program correctness more difficult
with explicit threads.
Implicit threading: creation and management of threads done by
compilers and run-time libraries rather than programmers.
Three methods explored:
• Thread Pools
• OpenMP
• Grand Central Dispatch
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45
Thread Pools
Create a number of threads in a pool where they await work.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45
Thread Pools
Create a number of threads in a pool where they await work.
Usually slightly faster to service a request with an existing thread
than create a new thread.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45
Thread Pools
Create a number of threads in a pool where they await work.
Usually slightly faster to service a request with an existing thread
than create a new thread.
Allows the number of threads in the application(s) to be bound to
the size of the pool.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45
OpenMP (1/2)
Set of compiler directives and APIs for C, C++, FORTRAN.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45
OpenMP (1/2)
Set of compiler directives and APIs for C, C++, FORTRAN.
Identifies parallel regions: blocks of code that can run in parallel.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45
OpenMP (1/2)
Set of compiler directives and APIs for C, C++, FORTRAN.
Identifies parallel regions: blocks of code that can run in parallel.
#pragma omp parallel: create as many threads as there are cores.
#pragma omp parallel for: run for loop in parallel.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45
OpenMP (2/2)
#include <omp.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
/* sequential code */
#pragma omp parallel
{
printf("I am a parallel region.");
}
/* sequential code */
return 0;
}
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 37 / 45
Grand Central Dispatch (1/2)
Apple technology for Mac OS X and iOS: extensions to C, C++
API, and run-time library.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
Grand Central Dispatch (1/2)
Apple technology for Mac OS X and iOS: extensions to C, C++
API, and run-time library.
Allows identification of parallel sections.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
Grand Central Dispatch (1/2)
Apple technology for Mac OS X and iOS: extensions to C, C++
API, and run-time library.
Allows identification of parallel sections.
Block is in ˆ{ }: ˆ{ printf("I am a block"); }
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
Grand Central Dispatch (1/2)
Apple technology for Mac OS X and iOS: extensions to C, C++
API, and run-time library.
Allows identification of parallel sections.
Block is in ˆ{ }: ˆ{ printf("I am a block"); }
Blocks placed in dispatch queue.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
Grand Central Dispatch (2/2)
Two types of dispatch queues:
Serial: blocks removed in FIFO order, queue is per process.
Concurrent: removed in FIFO order but several may be removed at
a time.
dispatch_queue_t queue = dispatch_get_global_queue
(DISPATCH QUEUE PRIORITY DEFAULT, 0);
dispatch_async(queue, ^{ printf("I am a block."); });
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 39 / 45
Threading Issues
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 40 / 45
Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
• Some UNIXes have two versions of fork.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 41 / 45
Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
• Some UNIXes have two versions of fork.
exec() usually works as normal: replaces the running process in-
cluding all threads.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 41 / 45
Signal Handling
Where should a signal be delivered for multi-threaded?
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
Signal Handling
Where should a signal be delivered for multi-threaded?
• Deliver the signal to the thread to which the signal applies.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
Signal Handling
Where should a signal be delivered for multi-threaded?
• Deliver the signal to the thread to which the signal applies.
• Deliver the signal to every thread in the process.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
Signal Handling
Where should a signal be delivered for multi-threaded?
• Deliver the signal to the thread to which the signal applies.
• Deliver the signal to every thread in the process.
• Deliver the signal to certain threads in the process.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
Signal Handling
Where should a signal be delivered for multi-threaded?
• Deliver the signal to the thread to which the signal applies.
• Deliver the signal to every thread in the process.
• Deliver the signal to certain threads in the process.
• Assign a specific thread to receive all signals for the process.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
Summary
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 43 / 45
Summary
Single-thread vs. Multi-thread
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
Summary
Single-thread vs. Multi-thread
Interprocess vs. Intraprocess context switchings
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
Summary
Single-thread vs. Multi-thread
Interprocess vs. Intraprocess context switchings
Concurrency vs. parallelism
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
Summary
Single-thread vs. Multi-thread
Interprocess vs. Intraprocess context switchings
Concurrency vs. parallelism
Multi-threading models: many-to-one, one-to-one, many-to-many
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
Summary
Single-thread vs. Multi-thread
Interprocess vs. Intraprocess context switchings
Concurrency vs. parallelism
Multi-threading models: many-to-one, one-to-one, many-to-many
Multi-thread libraries: pthread
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
Summary
Single-thread vs. Multi-thread
Interprocess vs. Intraprocess context switchings
Concurrency vs. parallelism
Multi-threading models: many-to-one, one-to-one, many-to-many
Multi-thread libraries: pthread
Implicit threading
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 45 / 45

More Related Content

What's hot

30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
raj732723
 

What's hot (20)

Operating Systems - "Chapter 4: Multithreaded Programming"
Operating Systems - "Chapter 4:  Multithreaded Programming"Operating Systems - "Chapter 4:  Multithreaded Programming"
Operating Systems - "Chapter 4: Multithreaded Programming"
 
Basic os-concepts
Basic os-conceptsBasic os-concepts
Basic os-concepts
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
 
Unit 1
Unit 1Unit 1
Unit 1
 
Multithreading
Multithreading Multithreading
Multithreading
 
Multi core processors
Multi core processorsMulti core processors
Multi core processors
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
 
Real-Time Scheduling
Real-Time SchedulingReal-Time Scheduling
Real-Time Scheduling
 
Distributed Operating System_1
Distributed Operating System_1Distributed Operating System_1
Distributed Operating System_1
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Unix case-study
Unix case-studyUnix case-study
Unix case-study
 
Threads ppt
Threads pptThreads ppt
Threads ppt
 
Open mp
Open mpOpen mp
Open mp
 
WEP
WEPWEP
WEP
 
distributed Computing system model
distributed Computing system modeldistributed Computing system model
distributed Computing system model
 
Communication model of parallel platforms
Communication model of parallel platformsCommunication model of parallel platforms
Communication model of parallel platforms
 
Advanced Operating System- Introduction
Advanced Operating System- IntroductionAdvanced Operating System- Introduction
Advanced Operating System- Introduction
 
User and Operating System Interface
User and Operating System InterfaceUser and Operating System Interface
User and Operating System Interface
 
Process creation and termination In Operating System
Process creation and termination In Operating SystemProcess creation and termination In Operating System
Process creation and termination In Operating System
 
slides.08.pptx
slides.08.pptxslides.08.pptx
slides.08.pptx
 

Viewers also liked

Viewers also liked (20)

Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3
 
Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1
 
Protection
ProtectionProtection
Protection
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Security
SecuritySecurity
Security
 
Main Memory - Part2
Main Memory - Part2Main Memory - Part2
Main Memory - Part2
 
IO Systems
IO SystemsIO Systems
IO Systems
 
CPU Scheduling - Part2
CPU Scheduling - Part2CPU Scheduling - Part2
CPU Scheduling - Part2
 
Storage
StorageStorage
Storage
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics Platform
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing Frameworks
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
File System Interface
File System InterfaceFile System Interface
File System Interface
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1
 

Similar to Threads

Multithreading models
Multithreading modelsMultithreading models
Multithreading models
anoopkrishna2
 

Similar to Threads (20)

Chubby
ChubbyChubby
Chubby
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
W-9.pptx
W-9.pptxW-9.pptx
W-9.pptx
 
Parallel and Distributed Computing chapter 3
Parallel and Distributed Computing chapter 3Parallel and Distributed Computing chapter 3
Parallel and Distributed Computing chapter 3
 
Virtual Memory - Part2
Virtual Memory - Part2Virtual Memory - Part2
Virtual Memory - Part2
 
process and thread.pptx
process and thread.pptxprocess and thread.pptx
process and thread.pptx
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
Mesos and YARN
Mesos and YARNMesos and YARN
Mesos and YARN
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Threads are using in operating systems.pptx
Threads are using in operating systems.pptxThreads are using in operating systems.pptx
Threads are using in operating systems.pptx
 
Dynamo and NoSQL Databases
Dynamo and NoSQL DatabasesDynamo and NoSQL Databases
Dynamo and NoSQL Databases
 
Threads.ppt
Threads.pptThreads.ppt
Threads.ppt
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
Thread
ThreadThread
Thread
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Introduction to cloud computing and big data - part1
Introduction to cloud computing and big data - part1Introduction to cloud computing and big data - part1
Introduction to cloud computing and big data - part1
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading models
 
thread os.pptx
thread os.pptxthread os.pptx
thread os.pptx
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 

More from Amir Payberah (8)

P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution Network
 
The Spark Big Data Analytics Platform
The Spark Big Data Analytics PlatformThe Spark Big Data Analytics Platform
The Spark Big Data Analytics Platform
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1
 
Virtual Memory - Part1
Virtual Memory - Part1Virtual Memory - Part1
Virtual Memory - Part1
 
Main Memory - Part1
Main Memory - Part1Main Memory - Part1
Main Memory - Part1
 
Graph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXGraph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphX
 

Recently uploaded

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Recently uploaded (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 

Threads

  • 1. Threads Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 1 / 45
  • 2. Motivation Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 2 / 45
  • 3. Thread A basic unit of CPU utilization. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 3 / 45
  • 4. Threads (1/3) A traditional process: has a single thread. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45
  • 5. Threads (1/3) A traditional process: has a single thread. Multiple threads in a process: performing more than one task at a time. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45
  • 6. Threads (1/3) A traditional process: has a single thread. Multiple threads in a process: performing more than one task at a time. Threads in a process share code section, data section, and other OS resources, e.g., open files. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45
  • 7. Threads (2/3) Multiple tasks of an application can be implemented by separate threads. • Update display • Fetch data • Spell checking • Answer a network request Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 5 / 45
  • 8. Threads (3/3) Multi-threaded web-server architecture Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 6 / 45
  • 9. Threads Benefits (1/2) Responsiveness: may allow continued execution if part of process is blocked, especially important for user interfaces. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 7 / 45
  • 10. Threads Benefits (1/2) 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. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 7 / 45
  • 11. Threads Benefits (2/2) Economy: cheaper than process creation, thread switching lower overhead than context switching. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 8 / 45
  • 12. Threads Benefits (2/2) Economy: cheaper than process creation, thread switching lower overhead than context switching. Scalability: process can take advantage of multiprocessor architec- tures Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 8 / 45
  • 13. Context Switching vs. Threads Switching Inter-process switching: context switching from process-to-process. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 9 / 45
  • 14. Context Switching vs. Threads Switching Inter-process switching: context switching from process-to-process. Intra-process switching: switching from thread-to-thread. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 9 / 45
  • 15. Context Switching vs. Threads Switching Inter-process switching: context switching from process-to-process. Intra-process switching: switching from thread-to-thread. The cost of intra-process switching is less than the cost of inter- process switching. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 9 / 45
  • 16. Multicore Programming Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 10 / 45
  • 17. Multiprocessor and Multicore Systems (1/2) Users need more computing performance: single-CPU → multi-CPU Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 11 / 45
  • 18. Multiprocessor and Multicore Systems (1/2) Users need more computing performance: single-CPU → multi-CPU A similar trend in system design: place multiple computing cores on a single chip. • Each core appears as a separate processor. • Multi-core systems. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 11 / 45
  • 19. Multiprocessor and Multicore Systems (2/2) Multi-threaded programming • More efficient use of multiple cores. • Improved concurrency. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 12 / 45
  • 20. Concurrency vs. Parallelism (1/2) Parallelism • Performing more than one task simultaneously. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
  • 21. Concurrency vs. Parallelism (1/2) Parallelism • Performing more than one task simultaneously. Concurrency • Supporting more than one task by allowing all the tasks to make progress. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
  • 22. Concurrency vs. Parallelism (1/2) Parallelism • Performing more than one task simultaneously. Concurrency • Supporting more than one task by allowing all the tasks to make progress. • Single processor/core: scheduler providing concurrency. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
  • 23. Concurrency vs. Parallelism (1/2) Parallelism • Performing more than one task simultaneously. Concurrency • Supporting more than one task by allowing all the tasks to make progress. • Single processor/core: scheduler providing concurrency. • It is possible to have concurrency without parallelism. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 13 / 45
  • 24. Concurrency vs. Parallelism (2/2) Concurrent execution on a single-core system. Parallelism on a multi-core system. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 14 / 45
  • 25. Types of Parallelism Data parallelism • Distributes subsets of the same data across multiple cores, same operation on each. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 15 / 45
  • 26. Types of Parallelism Data parallelism • Distributes subsets of the same data across multiple cores, same operation on each. Task parallelism • Distributes threads across cores, each thread performing unique operation. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 15 / 45
  • 27. Programming Challenges Dividing activities Balance Data splitting Data dependency Testing and debugging Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 16 / 45
  • 28. Multi-threading Models Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 17 / 45
  • 29. User Threads and Kernel Threads User threads: management done by user-level threads library. Kernel threads: supported by the Kernel. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 18 / 45
  • 30. User Threads and Kernel Threads User threads: management done by user-level threads library. • Three primary thread libraries: • POSIX pthreads • Windows threads • Java threads Kernel threads: supported by the Kernel. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 18 / 45
  • 31. User Threads and Kernel Threads User threads: management done by user-level threads library. • Three primary thread libraries: • POSIX pthreads • Windows threads • Java threads Kernel threads: supported by the Kernel. • All general purpose operating systems, including: Windows, Solaris, Linux, Tru64 UNIX, Mac OS X Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 18 / 45
  • 32. Multi-Threading Models Many-to-One One-to-One Many-to-Many Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 19 / 45
  • 33. Many-to-One Model Many user-level threads mapped to single kernel thread. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
  • 34. Many-to-One Model Many user-level threads mapped to single kernel thread. One thread blocking causes all to block. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
  • 35. Many-to-One Model Many user-level threads mapped to single kernel thread. One thread blocking causes all to block. Multiple threads may not run in parallel on multicolor system because only one may be in kernel at a time. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
  • 36. Many-to-One Model Many user-level threads mapped to single kernel thread. One thread blocking causes all to block. Multiple threads may not run in parallel on multicolor system because only one may be in kernel at a time. Few systems currently use this model. • Solaris green threads • GNU portable threads Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45
  • 37. One-to-One Model Each user-level thread maps to a kernel thread. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
  • 38. One-to-One Model Each user-level thread maps to a kernel thread. Creating a user-level thread creates a kernel thread. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
  • 39. One-to-One Model Each user-level thread maps to a kernel thread. Creating a user-level thread creates a kernel thread. More concurrency than many-to-one. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
  • 40. One-to-One Model Each user-level thread maps to a kernel thread. Creating a user-level thread creates a kernel thread. More concurrency than many-to-one. Number of threads per process sometimes restricted due to overhead. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
  • 41. One-to-One Model Each user-level thread maps to a kernel thread. Creating a user-level thread creates a kernel thread. More concurrency than many-to-one. Number of threads per process sometimes restricted due to overhead. Examples: • Windows • Linux • Solaris 9 and later Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45
  • 42. Many-to-Many Model Allows many user-level threads to be mapped to many kernel threads. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45
  • 43. Many-to-Many Model Allows many user-level threads to be mapped to many kernel threads. Allows the OS to create a sufficient number of kernel threads. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45
  • 44. Many-to-Many Model Allows many user-level threads to be mapped to many kernel threads. Allows the OS to create a sufficient number of kernel threads. Examples: • Solaris prior to version 9 • Windows with the ThreadFiber package Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45
  • 45. Thread Libraries Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 23 / 45
  • 46. Thread Libraries (1/2) Thread library provides programmer with API for creating and managing threads. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 24 / 45
  • 47. Thread Libraries (1/2) Thread library provides programmer with API for creating and managing threads. Two primary ways of implementing: • Library entirely in user-space. • Kernel-level library supported by the OS. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 24 / 45
  • 48. Thread Libraries (2/2) Pthread • Either a user-level or a kernel-level library. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45
  • 49. Thread Libraries (2/2) Pthread • Either a user-level or a kernel-level library. Windows thread • Kernel-level library. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45
  • 50. Thread Libraries (2/2) Pthread • Either a user-level or a kernel-level library. Windows thread • Kernel-level library. Java thread • Uses a thread library available on the host system. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45
  • 51. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 26 / 45
  • 52. Pthreads A POSIX API for thread creation and synchronization. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
  • 53. Pthreads A POSIX API for thread creation and synchronization. Specification, not implementation. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
  • 54. Pthreads A POSIX API for thread creation and synchronization. Specification, not implementation. API specifies behavior of the thread library, implementation is up to development of the library. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
  • 55. Pthreads A POSIX API for thread creation and synchronization. Specification, not implementation. API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX OSs, e.g., Solaris, Linux, Mac OS X Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45
  • 56. Thread ID The thread ID (TID) is the thread analogue to the process ID (PID). Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
  • 57. Thread ID The thread ID (TID) is the thread analogue to the process ID (PID). The PID is assigned by the Linux kernel, and TID is assigned in the Pthread library. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
  • 58. Thread ID The thread ID (TID) is the thread analogue to the process ID (PID). The PID is assigned by the Linux kernel, and TID is assigned in the Pthread library. Represented by pthread t. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
  • 59. Thread ID The thread ID (TID) is the thread analogue to the process ID (PID). The PID is assigned by the Linux kernel, and TID is assigned in the Pthread library. Represented by pthread t. Obtaining a TID at runtime: #include <pthread.h> pthread_t pthread_self(void); Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45
  • 60. Creating Threads pthread create() defines and launches a new thread. #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); start routine has the following signature: void *start_thread(void *arg); Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 29 / 45
  • 61. Terminating Threads Terminating yourself by calling pthread exit(). #include <pthread.h> void pthread_exit(void *retval); Terminating others by calling pthread cancel(). #include <pthread.h> int pthread_cancel(pthread_t thread); Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 30 / 45
  • 62. Joining and Detaching Threads Joining allows one thread to block while waiting for the termination of another. #include <pthread.h> int pthread_join(pthread_t thread, void **retval); int pthread_detach(pthread_t thread); [https://computing.llnl.gov/tutorials/pthreads/#Joining] Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 31 / 45
  • 63. Joining and Detaching Threads Joining allows one thread to block while waiting for the termination of another. You use join if you care about what value the thread returns when it is done, and use detach if you do not. #include <pthread.h> int pthread_join(pthread_t thread, void **retval); int pthread_detach(pthread_t thread); [https://computing.llnl.gov/tutorials/pthreads/#Joining] Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 31 / 45
  • 64. A Threading Example void *start_thread(void *message) { printf("%sn", (const char *)message); return message; } int main(void) { pthread_t thread11, thread2; const char *message1 = "Thread 1"; const char *message2 = "Thread 2"; // Create two threads, each with a different message. pthread_create(&thread1, NULL, start_thread, (void *)message1); pthread_create(&thread2, NULL, start_thread, (void *)message2); // Wait for the threads to exit. pthread_join(thread1, NULL); pthread_join(thread2, NULL); return 0; } Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 32 / 45
  • 65. Implicit Threading Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 33 / 45
  • 66. Implicit Threading Increasing the number of threads: program correctness more difficult with explicit threads. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45
  • 67. Implicit Threading Increasing the number of threads: program correctness more difficult with explicit threads. Implicit threading: creation and management of threads done by compilers and run-time libraries rather than programmers. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45
  • 68. Implicit Threading Increasing the number of threads: program correctness more difficult with explicit threads. Implicit threading: creation and management of threads done by compilers and run-time libraries rather than programmers. Three methods explored: • Thread Pools • OpenMP • Grand Central Dispatch Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45
  • 69. Thread Pools Create a number of threads in a pool where they await work. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45
  • 70. Thread Pools Create a number of threads in a pool where they await work. Usually slightly faster to service a request with an existing thread than create a new thread. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45
  • 71. Thread Pools Create a number of threads in a pool where they await work. Usually slightly faster to service a request with an existing thread than create a new thread. Allows the number of threads in the application(s) to be bound to the size of the pool. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45
  • 72. OpenMP (1/2) Set of compiler directives and APIs for C, C++, FORTRAN. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45
  • 73. OpenMP (1/2) Set of compiler directives and APIs for C, C++, FORTRAN. Identifies parallel regions: blocks of code that can run in parallel. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45
  • 74. OpenMP (1/2) Set of compiler directives and APIs for C, C++, FORTRAN. Identifies parallel regions: blocks of code that can run in parallel. #pragma omp parallel: create as many threads as there are cores. #pragma omp parallel for: run for loop in parallel. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45
  • 75. OpenMP (2/2) #include <omp.h> #include <stdio.h> int main(int argc, char *argv[]) { /* sequential code */ #pragma omp parallel { printf("I am a parallel region."); } /* sequential code */ return 0; } Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 37 / 45
  • 76. Grand Central Dispatch (1/2) Apple technology for Mac OS X and iOS: extensions to C, C++ API, and run-time library. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
  • 77. Grand Central Dispatch (1/2) Apple technology for Mac OS X and iOS: extensions to C, C++ API, and run-time library. Allows identification of parallel sections. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
  • 78. Grand Central Dispatch (1/2) Apple technology for Mac OS X and iOS: extensions to C, C++ API, and run-time library. Allows identification of parallel sections. Block is in ˆ{ }: ˆ{ printf("I am a block"); } Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
  • 79. Grand Central Dispatch (1/2) Apple technology for Mac OS X and iOS: extensions to C, C++ API, and run-time library. Allows identification of parallel sections. Block is in ˆ{ }: ˆ{ printf("I am a block"); } Blocks placed in dispatch queue. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45
  • 80. Grand Central Dispatch (2/2) Two types of dispatch queues: Serial: blocks removed in FIFO order, queue is per process. Concurrent: removed in FIFO order but several may be removed at a time. dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH QUEUE PRIORITY DEFAULT, 0); dispatch_async(queue, ^{ printf("I am a block."); }); Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 39 / 45
  • 81. Threading Issues Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 40 / 45
  • 82. Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? • Some UNIXes have two versions of fork. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 41 / 45
  • 83. Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? • Some UNIXes have two versions of fork. exec() usually works as normal: replaces the running process in- cluding all threads. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 41 / 45
  • 84. Signal Handling Where should a signal be delivered for multi-threaded? Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
  • 85. Signal Handling Where should a signal be delivered for multi-threaded? • Deliver the signal to the thread to which the signal applies. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
  • 86. Signal Handling Where should a signal be delivered for multi-threaded? • Deliver the signal to the thread to which the signal applies. • Deliver the signal to every thread in the process. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
  • 87. Signal Handling Where should a signal be delivered for multi-threaded? • Deliver the signal to the thread to which the signal applies. • Deliver the signal to every thread in the process. • Deliver the signal to certain threads in the process. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
  • 88. Signal Handling Where should a signal be delivered for multi-threaded? • Deliver the signal to the thread to which the signal applies. • Deliver the signal to every thread in the process. • Deliver the signal to certain threads in the process. • Assign a specific thread to receive all signals for the process. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 42 / 45
  • 89. Summary Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 43 / 45
  • 90. Summary Single-thread vs. Multi-thread Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
  • 91. Summary Single-thread vs. Multi-thread Interprocess vs. Intraprocess context switchings Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
  • 92. Summary Single-thread vs. Multi-thread Interprocess vs. Intraprocess context switchings Concurrency vs. parallelism Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
  • 93. Summary Single-thread vs. Multi-thread Interprocess vs. Intraprocess context switchings Concurrency vs. parallelism Multi-threading models: many-to-one, one-to-one, many-to-many Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
  • 94. Summary Single-thread vs. Multi-thread Interprocess vs. Intraprocess context switchings Concurrency vs. parallelism Multi-threading models: many-to-one, one-to-one, many-to-many Multi-thread libraries: pthread Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
  • 95. Summary Single-thread vs. Multi-thread Interprocess vs. Intraprocess context switchings Concurrency vs. parallelism Multi-threading models: many-to-one, one-to-one, many-to-many Multi-thread libraries: pthread Implicit threading Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45
  • 96. Questions? Acknowledgements Some slides were derived from Avi Silberschatz slides. Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 45 / 45