Download - Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Transcript
Page 1: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads

Amir H. [email protected]

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 1 / 45

Page 2: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Motivation

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 2 / 45

Page 3: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread

A basic unit of CPU utilization.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 3 / 45

Page 4: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads (1/3)

I A traditional process: has a single thread.

I Multiple threads in a process: performing more than one task at atime.

I Threads in a process share code section, data section, and other OSresources, e.g., open files.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45

Page 5: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads (1/3)

I A traditional process: has a single thread.

I Multiple threads in a process: performing more than one task at atime.

I Threads in a process share code section, data section, and other OSresources, e.g., open files.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45

Page 6: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads (1/3)

I A traditional process: has a single thread.

I Multiple threads in a process: performing more than one task at atime.

I Threads in a process share code section, data section, and other OSresources, e.g., open files.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 4 / 45

Page 7: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads (2/3)

I Multiple tasks of an application can be implemented by separatethreads.

• Update display• Fetch data• Spell checking• Answer a network request

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 5 / 45

Page 8: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads (3/3)

I Multi-threaded web-server architecture

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 6 / 45

Page 9: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads Benefits (1/2)

I Responsiveness: may allow continued execution if part of process isblocked, especially important for user interfaces.

I Resource Sharing: threads share resources of process, easier thanshared memory or message passing.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 7 / 45

Page 10: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads Benefits (1/2)

I Responsiveness: may allow continued execution if part of process isblocked, especially important for user interfaces.

I Resource Sharing: threads share resources of process, easier thanshared memory or message passing.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 7 / 45

Page 11: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads Benefits (2/2)

I Economy: cheaper than process creation, thread switching loweroverhead than context switching.

I Scalability: process can take advantage of multiprocessor architec-tures

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 8 / 45

Page 12: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threads Benefits (2/2)

I Economy: cheaper than process creation, thread switching loweroverhead than context switching.

I Scalability: process can take advantage of multiprocessor architec-tures

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 8 / 45

Page 13: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Context Switching vs. Threads Switching

I Inter-process switching: context switching from process-to-process.

I Intra-process switching: switching from thread-to-thread.

I 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

Page 14: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Context Switching vs. Threads Switching

I Inter-process switching: context switching from process-to-process.

I Intra-process switching: switching from thread-to-thread.

I 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

Page 15: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Context Switching vs. Threads Switching

I Inter-process switching: context switching from process-to-process.

I Intra-process switching: switching from thread-to-thread.

I 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

Page 16: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Multicore Programming

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 10 / 45

Page 17: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Multiprocessor and Multicore Systems (1/2)

I Users need more computing performance: single-CPU → multi-CPU

I A similar trend in system design: place multiple computing cores ona single chip.

• Each core appears as a separate processor.• Multi-core systems.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 11 / 45

Page 18: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Multiprocessor and Multicore Systems (1/2)

I Users need more computing performance: single-CPU → multi-CPU

I A similar trend in system design: place multiple computing cores ona single chip.

• Each core appears as a separate processor.• Multi-core systems.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 11 / 45

Page 19: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Multiprocessor and Multicore Systems (2/2)

I Multi-threaded programming• More efficient use of multiple cores.• Improved concurrency.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 12 / 45

Page 20: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Concurrency vs. Parallelism (1/2)

I Parallelism• Performing more than one task simultaneously.

I 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

Page 21: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Concurrency vs. Parallelism (1/2)

I Parallelism• Performing more than one task simultaneously.

I 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

Page 22: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Concurrency vs. Parallelism (1/2)

I Parallelism• Performing more than one task simultaneously.

I 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

Page 23: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Concurrency vs. Parallelism (1/2)

I Parallelism• Performing more than one task simultaneously.

I 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

Page 24: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Concurrency vs. Parallelism (2/2)

I Concurrent execution on a single-core system.

I Parallelism on a multi-core system.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 14 / 45

Page 25: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Types of Parallelism

I Data parallelism• Distributes subsets of the same data across multiple cores, same

operation on each.

I Task parallelism• Distributes threads across cores, each thread performing unique

operation.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 15 / 45

Page 26: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Types of Parallelism

I Data parallelism• Distributes subsets of the same data across multiple cores, same

operation on each.

I Task parallelism• Distributes threads across cores, each thread performing unique

operation.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 15 / 45

Page 27: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Programming Challenges

I Dividing activities

I Balance

I Data splitting

I Data dependency

I Testing and debugging

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 16 / 45

Page 28: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Multi-threading Models

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 17 / 45

Page 29: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

User Threads and Kernel Threads

I User threads: management done by user-level threads library.

• Three primary thread libraries:• POSIX pthreads• Windows threads• Java threads

I 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

Page 30: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

User Threads and Kernel Threads

I User threads: management done by user-level threads library.• Three primary thread libraries:• POSIX pthreads• Windows threads• Java threads

I 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

Page 31: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

User Threads and Kernel Threads

I User threads: management done by user-level threads library.• Three primary thread libraries:• POSIX pthreads• Windows threads• Java threads

I 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

Page 32: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Multi-Threading Models

I Many-to-One

I One-to-One

I Many-to-Many

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 19 / 45

Page 33: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-One Model

I Many user-level threads mapped to single kernel thread.

I One thread blocking causes all to block.

I Multiple threads may not run in parallel on multicolor systembecause only one may be in kernel at a time.

I Few systems currently use this model.• Solaris green threads• GNU portable threads

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45

Page 34: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-One Model

I Many user-level threads mapped to single kernel thread.

I One thread blocking causes all to block.

I Multiple threads may not run in parallel on multicolor systembecause only one may be in kernel at a time.

I Few systems currently use this model.• Solaris green threads• GNU portable threads

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45

Page 35: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-One Model

I Many user-level threads mapped to single kernel thread.

I One thread blocking causes all to block.

I Multiple threads may not run in parallel on multicolor systembecause only one may be in kernel at a time.

I Few systems currently use this model.• Solaris green threads• GNU portable threads

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45

Page 36: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-One Model

I Many user-level threads mapped to single kernel thread.

I One thread blocking causes all to block.

I Multiple threads may not run in parallel on multicolor systembecause only one may be in kernel at a time.

I Few systems currently use this model.• Solaris green threads• GNU portable threads

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 20 / 45

Page 37: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

One-to-One Model

I Each user-level thread maps to a kernel thread.

I Creating a user-level thread creates a kernel thread.

I More concurrency than many-to-one.

I Number of threads per process sometimes restricted due tooverhead.

I Examples:• Windows• Linux• Solaris 9 and later

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45

Page 38: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

One-to-One Model

I Each user-level thread maps to a kernel thread.

I Creating a user-level thread creates a kernel thread.

I More concurrency than many-to-one.

I Number of threads per process sometimes restricted due tooverhead.

I Examples:• Windows• Linux• Solaris 9 and later

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45

Page 39: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

One-to-One Model

I Each user-level thread maps to a kernel thread.

I Creating a user-level thread creates a kernel thread.

I More concurrency than many-to-one.

I Number of threads per process sometimes restricted due tooverhead.

I Examples:• Windows• Linux• Solaris 9 and later

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45

Page 40: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

One-to-One Model

I Each user-level thread maps to a kernel thread.

I Creating a user-level thread creates a kernel thread.

I More concurrency than many-to-one.

I Number of threads per process sometimes restricted due tooverhead.

I Examples:• Windows• Linux• Solaris 9 and later

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45

Page 41: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

One-to-One Model

I Each user-level thread maps to a kernel thread.

I Creating a user-level thread creates a kernel thread.

I More concurrency than many-to-one.

I Number of threads per process sometimes restricted due tooverhead.

I Examples:• Windows• Linux• Solaris 9 and later

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 21 / 45

Page 42: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-Many Model

I Allows many user-level threads to be mapped to many kernelthreads.

I Allows the OS to create a sufficient number of kernel threads.

I Examples:• Solaris prior to version 9• Windows with the ThreadFiber package

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45

Page 43: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-Many Model

I Allows many user-level threads to be mapped to many kernelthreads.

I Allows the OS to create a sufficient number of kernel threads.

I Examples:• Solaris prior to version 9• Windows with the ThreadFiber package

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45

Page 44: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Many-to-Many Model

I Allows many user-level threads to be mapped to many kernelthreads.

I Allows the OS to create a sufficient number of kernel threads.

I Examples:• Solaris prior to version 9• Windows with the ThreadFiber package

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 22 / 45

Page 45: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Libraries

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 23 / 45

Page 46: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Libraries (1/2)

I Thread library provides programmer with API for creating andmanaging threads.

I 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

Page 47: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Libraries (1/2)

I Thread library provides programmer with API for creating andmanaging threads.

I 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

Page 48: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Libraries (2/2)

I Pthread• Either a user-level or a kernel-level library.

I Windows thread• Kernel-level library.

I Java thread• Uses a thread library available on the host system.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45

Page 49: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Libraries (2/2)

I Pthread• Either a user-level or a kernel-level library.

I Windows thread• Kernel-level library.

I Java thread• Uses a thread library available on the host system.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45

Page 50: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Libraries (2/2)

I Pthread• Either a user-level or a kernel-level library.

I Windows thread• Kernel-level library.

I Java thread• Uses a thread library available on the host system.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 25 / 45

Page 51: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 26 / 45

Page 52: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Pthreads

I A POSIX API for thread creation and synchronization.

I Specification, not implementation.

I API specifies behavior of the thread library, implementation is upto development of the library.

I Common in UNIX OSs, e.g., Solaris, Linux, Mac OS X

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45

Page 53: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Pthreads

I A POSIX API for thread creation and synchronization.

I Specification, not implementation.

I API specifies behavior of the thread library, implementation is upto development of the library.

I Common in UNIX OSs, e.g., Solaris, Linux, Mac OS X

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45

Page 54: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Pthreads

I A POSIX API for thread creation and synchronization.

I Specification, not implementation.

I API specifies behavior of the thread library, implementation is upto development of the library.

I Common in UNIX OSs, e.g., Solaris, Linux, Mac OS X

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45

Page 55: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Pthreads

I A POSIX API for thread creation and synchronization.

I Specification, not implementation.

I API specifies behavior of the thread library, implementation is upto development of the library.

I Common in UNIX OSs, e.g., Solaris, Linux, Mac OS X

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 27 / 45

Page 56: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread ID

I The thread ID (TID) is the thread analogue to the process ID (PID).

I The PID is assigned by the Linux kernel, and TID is assigned in thePthread library.

I Represented by pthread t.

I Obtaining a TID at runtime:

#include <pthread.h>

pthread_t pthread_self(void);

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45

Page 57: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread ID

I The thread ID (TID) is the thread analogue to the process ID (PID).

I The PID is assigned by the Linux kernel, and TID is assigned in thePthread library.

I Represented by pthread t.

I Obtaining a TID at runtime:

#include <pthread.h>

pthread_t pthread_self(void);

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45

Page 58: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread ID

I The thread ID (TID) is the thread analogue to the process ID (PID).

I The PID is assigned by the Linux kernel, and TID is assigned in thePthread library.

I Represented by pthread t.

I Obtaining a TID at runtime:

#include <pthread.h>

pthread_t pthread_self(void);

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45

Page 59: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread ID

I The thread ID (TID) is the thread analogue to the process ID (PID).

I The PID is assigned by the Linux kernel, and TID is assigned in thePthread library.

I Represented by pthread t.

I Obtaining a TID at runtime:

#include <pthread.h>

pthread_t pthread_self(void);

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 28 / 45

Page 60: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Creating Threads

I 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);

I start routine has the following signature:

void *start_thread(void *arg);

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 29 / 45

Page 61: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Terminating Threads

I Terminating yourself by calling pthread exit().

#include <pthread.h>

void pthread_exit(void *retval);

I 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

Page 62: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Joining and Detaching Threads

I Joining allows one thread to block while waiting for the terminationof another.

I You use join if you care about what value the thread returns whenit 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

Page 63: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Joining and Detaching Threads

I Joining allows one thread to block while waiting for the terminationof another.

I You use join if you care about what value the thread returns whenit 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

Page 64: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

A Threading Example

void *start_thread(void *message) {

printf("%s\n", (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

Page 65: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Implicit Threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 33 / 45

Page 66: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Implicit Threading

I Increasing the number of threads: program correctness more difficultwith explicit threads.

I Implicit threading: creation and management of threads done bycompilers and run-time libraries rather than programmers.

I Three methods explored:• Thread Pools• OpenMP• Grand Central Dispatch

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45

Page 67: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Implicit Threading

I Increasing the number of threads: program correctness more difficultwith explicit threads.

I Implicit threading: creation and management of threads done bycompilers and run-time libraries rather than programmers.

I Three methods explored:• Thread Pools• OpenMP• Grand Central Dispatch

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45

Page 68: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Implicit Threading

I Increasing the number of threads: program correctness more difficultwith explicit threads.

I Implicit threading: creation and management of threads done bycompilers and run-time libraries rather than programmers.

I Three methods explored:• Thread Pools• OpenMP• Grand Central Dispatch

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 34 / 45

Page 69: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Pools

I Create a number of threads in a pool where they await work.

I Usually slightly faster to service a request with an existing threadthan create a new thread.

I Allows the number of threads in the application(s) to be bound tothe size of the pool.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45

Page 70: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Pools

I Create a number of threads in a pool where they await work.

I Usually slightly faster to service a request with an existing threadthan create a new thread.

I Allows the number of threads in the application(s) to be bound tothe size of the pool.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45

Page 71: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Thread Pools

I Create a number of threads in a pool where they await work.

I Usually slightly faster to service a request with an existing threadthan create a new thread.

I Allows the number of threads in the application(s) to be bound tothe size of the pool.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 35 / 45

Page 72: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

OpenMP (1/2)

I Set of compiler directives and APIs for C, C++, FORTRAN.

I Identifies parallel regions: blocks of code that can run in parallel.

I #pragma omp parallel: create as many threads as there are cores.

I #pragma omp parallel for: run for loop in parallel.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45

Page 73: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

OpenMP (1/2)

I Set of compiler directives and APIs for C, C++, FORTRAN.

I Identifies parallel regions: blocks of code that can run in parallel.

I #pragma omp parallel: create as many threads as there are cores.

I #pragma omp parallel for: run for loop in parallel.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45

Page 74: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

OpenMP (1/2)

I Set of compiler directives and APIs for C, C++, FORTRAN.

I Identifies parallel regions: blocks of code that can run in parallel.

I #pragma omp parallel: create as many threads as there are cores.

I #pragma omp parallel for: run for loop in parallel.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 36 / 45

Page 75: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

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

Page 76: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Grand Central Dispatch (1/2)

I Apple technology for Mac OS X and iOS: extensions to C, C++API, and run-time library.

I Allows identification of parallel sections.

I Block is in ˆ{ }: ˆ{ printf("I am a block"); }

I Blocks placed in dispatch queue.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45

Page 77: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Grand Central Dispatch (1/2)

I Apple technology for Mac OS X and iOS: extensions to C, C++API, and run-time library.

I Allows identification of parallel sections.

I Block is in ˆ{ }: ˆ{ printf("I am a block"); }

I Blocks placed in dispatch queue.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45

Page 78: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Grand Central Dispatch (1/2)

I Apple technology for Mac OS X and iOS: extensions to C, C++API, and run-time library.

I Allows identification of parallel sections.

I Block is in ˆ{ }: ˆ{ printf("I am a block"); }

I Blocks placed in dispatch queue.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45

Page 79: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Grand Central Dispatch (1/2)

I Apple technology for Mac OS X and iOS: extensions to C, C++API, and run-time library.

I Allows identification of parallel sections.

I Block is in ˆ{ }: ˆ{ printf("I am a block"); }

I Blocks placed in dispatch queue.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 38 / 45

Page 80: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Grand Central Dispatch (2/2)

I Two types of dispatch queues:

I Serial: blocks removed in FIFO order, queue is per process.

I Concurrent: removed in FIFO order but several may be removed ata 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

Page 81: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Threading Issues

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 40 / 45

Page 82: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Semantics of fork() and exec()

I Does fork() duplicate only the calling thread or all threads?• Some UNIXes have two versions of fork.

I exec() usually works as normal: replaces the running process in-cluding all threads.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 41 / 45

Page 83: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Semantics of fork() and exec()

I Does fork() duplicate only the calling thread or all threads?• Some UNIXes have two versions of fork.

I exec() usually works as normal: replaces the running process in-cluding all threads.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 41 / 45

Page 84: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Signal Handling

I 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

Page 85: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Signal Handling

I 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

Page 86: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Signal Handling

I 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

Page 87: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Signal Handling

I 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

Page 88: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Signal Handling

I 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

Page 89: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 43 / 45

Page 90: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

I Single-thread vs. Multi-thread

I Interprocess vs. Intraprocess context switchings

I Concurrency vs. parallelism

I Multi-threading models: many-to-one, one-to-one, many-to-many

I Multi-thread libraries: pthread

I Implicit threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45

Page 91: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

I Single-thread vs. Multi-thread

I Interprocess vs. Intraprocess context switchings

I Concurrency vs. parallelism

I Multi-threading models: many-to-one, one-to-one, many-to-many

I Multi-thread libraries: pthread

I Implicit threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45

Page 92: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

I Single-thread vs. Multi-thread

I Interprocess vs. Intraprocess context switchings

I Concurrency vs. parallelism

I Multi-threading models: many-to-one, one-to-one, many-to-many

I Multi-thread libraries: pthread

I Implicit threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45

Page 93: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

I Single-thread vs. Multi-thread

I Interprocess vs. Intraprocess context switchings

I Concurrency vs. parallelism

I Multi-threading models: many-to-one, one-to-one, many-to-many

I Multi-thread libraries: pthread

I Implicit threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45

Page 94: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

I Single-thread vs. Multi-thread

I Interprocess vs. Intraprocess context switchings

I Concurrency vs. parallelism

I Multi-threading models: many-to-one, one-to-one, many-to-many

I Multi-thread libraries: pthread

I Implicit threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45

Page 95: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Summary

I Single-thread vs. Multi-thread

I Interprocess vs. Intraprocess context switchings

I Concurrency vs. parallelism

I Multi-threading models: many-to-one, one-to-one, many-to-many

I Multi-thread libraries: pthread

I Implicit threading

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 44 / 45

Page 96: Threads - Amir H. Payberah · Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin a process: performingmore than one taskat a time. I Threads in a processsharecode

Questions?

Acknowledgements

Some slides were derived from Avi Silberschatz slides.

Amir H. Payberah (Tehran Polytechnic) Threads 1393/7/12 45 / 45