Threads - Amir H. Payberah Threads (1/3) I A traditional process: has a singlethread. I Multiple...

download Threads - Amir H. Payberah Threads (1/3) I A traditional process: has a singlethread. I Multiple threadsin

of 96

  • date post

    16-Oct-2020
  • Category

    Documents

  • view

    0
  • download

    0

Embed Size (px)

Transcript of Threads - Amir H. Payberah Threads (1/3) I A traditional process: has a singlethread. I Multiple...

  • 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)

    I A traditional process: has a single thread.

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

    I 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 (1/3)

    I A traditional process: has a single thread.

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

    I 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 (1/3)

    I A traditional process: has a single thread.

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

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

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

    I Multi-threaded web-server architecture

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

  • Threads Benefits (1/2)

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

    I 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 (1/2)

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

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

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

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

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

  • Threads Benefits (2/2)

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

    I 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

    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

  • 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

  • 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

  • Multicore Programming

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

  • 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 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 (1/2)

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

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

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

    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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • Multi-threading Models

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

  • 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

  • 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

  • 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