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

PPTX
Operating system and its types
PPT
Operating Systems - "Chapter 4: Multithreaded Programming"
PDF
System calls
PPTX
Basic web security model
PDF
Multithreading
PPTX
Unix Operating System
PDF
Os lab manual
PPTX
Operating system overview concepts ppt
Operating system and its types
Operating Systems - "Chapter 4: Multithreaded Programming"
System calls
Basic web security model
Multithreading
Unix Operating System
Os lab manual
Operating system overview concepts ppt

What's hot (20)

PPTX
DoS or DDoS attack
PPT
OS Structure
PPTX
Process management in linux
PPT
Process scheduling linux
PPT
Shell programming
PPTX
Operating system
PDF
Introduction to Operating Systems
PPTX
Practical Malware Analysis: Ch 5: IDA Pro
PPTX
Linux shell env
PPTX
Threads (operating System)
PPTX
File Management in Operating System
PPT
Introduction to Operating Systems
PPTX
Linux.ppt
PPTX
Function of Operating system
PPTX
Evolution of operating system
PPTX
Evolution of Microsoft windows operating systems
PPTX
Stuxnet worm
PPTX
Osi security architecture in network.pptx
PPT
Introduction to System Calls
PPTX
Threads and multi threading
DoS or DDoS attack
OS Structure
Process management in linux
Process scheduling linux
Shell programming
Operating system
Introduction to Operating Systems
Practical Malware Analysis: Ch 5: IDA Pro
Linux shell env
Threads (operating System)
File Management in Operating System
Introduction to Operating Systems
Linux.ppt
Function of Operating system
Evolution of operating system
Evolution of Microsoft windows operating systems
Stuxnet worm
Osi security architecture in network.pptx
Introduction to System Calls
Threads and multi threading
Ad

Viewers also liked (20)

PDF
Process Management - Part3
PDF
Process Management - Part1
PDF
Introduction to Operating Systems - Part3
PDF
Introduction to Operating Systems - Part2
PDF
Process Management - Part2
PDF
Process Synchronization - Part2
PDF
Process Synchronization - Part1
PDF
Protection
PDF
Cloud Computing
PDF
Security
PDF
Main Memory - Part2
PDF
IO Systems
PDF
CPU Scheduling - Part2
PDF
Storage
PDF
The Stratosphere Big Data Analytics Platform
PDF
Introduction to Operating Systems - Part1
PDF
Data Intensive Computing Frameworks
PDF
Deadlocks
PDF
File System Interface
PDF
CPU Scheduling - Part1
Process Management - Part3
Process Management - Part1
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part2
Process Management - Part2
Process Synchronization - Part2
Process Synchronization - Part1
Protection
Cloud Computing
Security
Main Memory - Part2
IO Systems
CPU Scheduling - Part2
Storage
The Stratosphere Big Data Analytics Platform
Introduction to Operating Systems - Part1
Data Intensive Computing Frameworks
Deadlocks
File System Interface
CPU Scheduling - Part1
Ad

Similar to Threads (20)

PPT
PPTX
Lecture 3 threads
PPTX
Multi threaded programming
PPT
Os Threads
PPT
Operating System-Threads-Galvin
PPTX
PDF
Multithreaded Programming in oprating system
PPTX
Networking threads
PPTX
Operating system: threads(mulithreading,benefits of threads, types of thread)
PPTX
PPT
multi-threading
PPTX
Operating systems - Introduction to Threads
PPT
Ch04 threads
PPTX
Threads ppt
PPTX
Topic 4- processes.pptx
PPTX
Engineeering Operating systemsOS UNIT 3 Threads.pptx
PPTX
OS Module-2.pptx
PPTX
threads-ppfldkgsh;reghuiregiuhrughet.pptx
PPTX
thread os.pptx
PPTX
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
Lecture 3 threads
Multi threaded programming
Os Threads
Operating System-Threads-Galvin
Multithreaded Programming in oprating system
Networking threads
Operating system: threads(mulithreading,benefits of threads, types of thread)
multi-threading
Operating systems - Introduction to Threads
Ch04 threads
Threads ppt
Topic 4- processes.pptx
Engineeering Operating systemsOS UNIT 3 Threads.pptx
OS Module-2.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptx
thread os.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx

More from Amir Payberah (10)

PDF
P2P Content Distribution Network
PDF
The Spark Big Data Analytics Platform
PDF
Linux Module Programming
PDF
File System Implementation - Part2
PDF
File System Implementation - Part1
PDF
Virtual Memory - Part2
PDF
Virtual Memory - Part1
PDF
Main Memory - Part1
PDF
Mesos and YARN
PDF
Graph processing - Powergraph and GraphX
P2P Content Distribution Network
The Spark Big Data Analytics Platform
Linux Module Programming
File System Implementation - Part2
File System Implementation - Part1
Virtual Memory - Part2
Virtual Memory - Part1
Main Memory - Part1
Mesos and YARN
Graph processing - Powergraph and GraphX

Recently uploaded (20)

PPTX
assetexplorer- product-overview - presentation
PPTX
"Secure File Sharing Solutions on AWS".pptx
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
CNN LeNet5 Architecture: Neural Networks
PDF
Website Design Services for Small Businesses.pdf
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PDF
MCP Security Tutorial - Beginner to Advanced
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PDF
CCleaner 6.39.11548 Crack 2025 License Key
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Topaz Photo AI Crack New Download (Latest 2025)
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
assetexplorer- product-overview - presentation
"Secure File Sharing Solutions on AWS".pptx
Types of Token_ From Utility to Security.pdf
Monitoring Stack: Grafana, Loki & Promtail
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
CNN LeNet5 Architecture: Neural Networks
Website Design Services for Small Businesses.pdf
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
MCP Security Tutorial - Beginner to Advanced
Why Generative AI is the Future of Content, Code & Creativity?
Designing Intelligence for the Shop Floor.pdf
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
CCleaner 6.39.11548 Crack 2025 License Key
Advanced SystemCare Ultimate Crack + Portable (2025)
Topaz Photo AI Crack New Download (Latest 2025)
Oracle Fusion HCM Cloud Demo for Beginners
How to Use SharePoint as an ISO-Compliant Document Management System

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