31
? CS8493 Models and Issues 1 Threads overview, Multithreading

Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

  • Upload
    others

  • View
    60

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

?CS8493 Models and Issues 1Threads overview, Multithreading

Page 2: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Objective

Threads overview, Multithreading Models and IssuesCS8493 2

• Overview• Multithreading Models• Threading Issues

Page 3: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Overview

Threads overview, Multithreading Models and IssuesCS8493 3

• A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.

• A thread is a path of execution within a process• It shares other threads belonging to the same process its code

section, data section, and files and signals.• A traditional (or heavyweight) process has a single thread of

control.• If a process has multiple threads of control, it can perform

more than one task at a time.

Watch: https://www.youtube.com/watch?v=VvdznvwHMbkhttps://stackoverflow.com/questions/6156636/is-this-a-correct-analogy-for-process-and-threads

Page 4: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

What Am I doing?

Threads overview, Multithreading Models and IssuesCS8493 4

Page 5: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Single and Multithreaded Processes

registers

code data files

stack registers registers registers

code data files

stackstackstack

thread thread

single-threaded process multithreaded process

The idea is to achieve parallelism by dividing a process into multiple threads

Threads overview, Multithreading Models and IssuesCS8493 5

Page 6: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Motivation

Threads overview, Multithreading Models and IssuesCS8493 6

• Most modern applications are multithreaded• Threads run within application• Multiple tasks with the application can be implemented by separate

threadsExample: Web browser

– Update display– Fetch data from the network– Opens multiple tabs– Downloading a video while playing it at the same time.Word Processor:– responding to keystrokes– Spell checking in the backgroundWeb server:Thousands of clients concurrently accessing

Page 7: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Word Processor Example

Threads overview, Multithreading Models and IssuesCS8493 7

Page 8: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Multithreaded Server Architecture

client

(1) request(2) create new

thread to servicethe request

server thread

(3) resume listening for additional client requests

• Process creation is heavy-weight while thread creation is light-weight• Can simplify code, increase efficiency•Kernels are generally multithreaded. One thread manages devices, another handles interrupts

Threads overview, Multithreading Models and IssuesCS8493 8

Page 9: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Benefits

Threads overview, Multithreading Models and IssuesCS8493 9

• Responsiveness – may allow continued execution if part of process is blocked, especially important for user interfaces. Web browser allows interaction while download an image.

• Resource Sharing – threads share resources of process, easier than shared memory or message passing

• Economy – cheaper than process creation, thread switching lower overhead than context switching

• Scalability – process can take advantage of multiprocessor architectures by increasing parallelism

Page 10: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Multicore Programming• Multicore or multiprocessor systems putting pressure on

programmers, challenges include:– Dividing activities– Balance– Data splitting– Data dependency: Ensure synchronization– Testing and debugging

• Parallelism implies a system can perform more than one task simultaneously

• Concurrency supports more than one task making progress– Single processor / core, scheduler providing concurrency

• Due to these challenges, many software developers argue that the advent of multicore systems will require an entirely new

10CS8493approach to designing Tshorefatdwsovaerrveiews, yMuslttitehrmeadsingin the future.Models and Issues

Page 11: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Concurrency vs. Parallelism

■ Concurrent execution on single-core system:

single core

time

■ Parallelism on a multi-core system:

T1 T2 T3 T4 T1 T2 T3 T4 T1 …

core 1

core 2

time

T1 T3 T1 T3 T1 …

Threads overview, Multithreading Models and IssuesCS8493 11

T2 T4 T2 T4 T2 …

Page 12: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Multicore Programming (Cont.)

Threads overview, Multithreading Models and IssuesCS8493 12

• Types of parallelism– Data parallelism – distributes subsets of the same data across

multiple cores, performing same operation on each coreo summing the contents of an array of size N

– Task parallelism – distributing tasks(threads) across cores, each thread performing unique operation

o Image dowloading, interacting web browser• As # of threads grows, so does architectural support for threading

– CPUs have cores as well as hardware threads– Consider Oracle SPARC T4 with 8 cores, and 8 hardware

threads per core

Page 13: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Multithreading Models

Threads overview, Multithreading Models and IssuesCS8493 13

Page 14: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Multithreading Models

Threads overview, Multithreading Models and IssuesCS8493 14

• Threads may be provided either ato User level, for user threads, oro Kernel, for kernel threads.

• User threads are supported above the kernel and are managed without kernel support

• Kernel threads are supported and managed directly by the operating system.

• Virtually all contemporary operating systems—including Windows, Linux, Mac OS X, and Solaris— support kernel threads.

Page 15: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Multithreading Models

Threads overview, Multithreading Models and IssuesCS8493 15

• Ultimately, a relationship must exist between user threads and kernel threads.

• There are 3 common ways of establishing such a relationship:

1) Many-to-One Model

2) One-to-One Model

3) Many-to-Many Model

Page 16: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Many-to-One Model• Many user-level threads mapped to

single kernel thread• Entire process will block if a thread

makes a blocking system call.• Multiple threads may not run in

parallel on multicore system because only one thread can access the kernel at a time

• Few systems currently use this model

• Examples:– Solaris Green Threads– GNU Portable Threads

userthread

kernel threadk

Threads overview, Multithreading Models and IssuesCS8493 16

Page 17: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

One-to-One

• Each user-level thread maps to kernel thread

• Creating a user-level thread creates a kernel thread

• More concurrency than many-to-one by allowing another thread to run when a thread makes a blocking system call.

• Number of threads per process sometimes restricted due to overhead of creating kernel threads

• Examples– Windows 2000, NT– Linux

userthread

kernel threadkkk k

CS8493– Solaris 9 and laterThreads overview, Multithreading

Models and Issues 17

Page 18: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Many-to-Many Model

• Allows many user level threads to be mapped to many kernel threads

• Allows the operating system to create a sufficient number of kernel threads

• When a thread performs a blocking system call, the kernel can schedule another thread for execution.

• Solaris prior to version 9• Windows with the ThreadFiber

package

userthread

kernelthreadkkk

Threads overview, Multithreading Models and IssuesCS8493 18

Page 19: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Two-level Model

• Similar to M:M, but also it allows a user thread to be bound to single kernel- level thread

• Examples– IRIX– HP-UX– Tru64 UNIX– Solaris 8 and earlier

user thread

kernel threadkkk k

Threads overview, Multithreading Models and IssuesCS8493 19

Page 20: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Difference between User Level thread and Kernel Level thread

Threads overview, Multithreading Models and IssuesCS8493 20

Page 21: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Assessment

Threads overview, Multithreading Models and IssuesCS8493 21

1. A thread shares its resources(like data section, code section, open files, signals) with a)other process similar to the one that the thread belongs tob) other threads that belong to similar processesc) other threads that belong to the same processd) all of the mentioned

2. A heavy weight processa) has multiple threads of executionb) has a single thread of executionc) can have multiple or a single thread for executiond) none of the mentioned

Page 22: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Assessment

3. A process having multiple threads of control implies

a) it can do more than one task at a timeb) it can do only one task at a time, but much fasterc) it has to use only one thread per processd) none of the mentioned

4. The kernel is of user threads.a) a part ofb) the creator ofc) unaware ofd) aware of

Threads overview, Multithreading Models and IssuesCS8493 22

Page 23: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Threading Issues

Threads overview, Multithreading Models and IssuesCS8493 23

• Semantics of fork() and exec() system calls• Signal handling

– Synchronous and asynchronous• Thread cancellation of target thread

– Asynchronous or deferred• Thread-local storage• SchedulerActivations

Page 24: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Semantics of fork() and exec()

Threads overview, Multithreading Models and IssuesCS8493 24

• Does fork() duplicate only the calling thread or all threads?– Some UNIXes have two versions offork

• exec() usually works as normal – replace the running process including all threads

Page 25: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Signal Handling

Threads overview, Multithreading Models and IssuesCS8493 25

• Signals are used in UNIX systems to notify a process that a particular event has occurred.

• Signal may be received either synchronously or asynchronously• Examples of synchronous signal include illegal memory access

and division by 0.• Examples of asynchronous signals include terminating a process

with specific keystrokes (such as <control><C>), timer expire• A signal handler is used to process signals

1. Signal is generated by particular event2. Generated signal is delivered to a process3. Signal is handled by one of two signal handlers:

1. Default signal handler2. user-defined signal handler

Page 26: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Signal Handling (Cont.)

Threads overview, Multithreading Models and IssuesCS8493 26

• Every signal has default handler that kernel runs whenhandling signal– User-defined signal handler can override default– Some signals (such as changing the size of a window) are

simply ignored; others (such as an illegal memory access)are handled by terminating the program.

– For single-threaded, signal delivered to process• 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

Page 27: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Thread Cancellation

• Terminating a thread before it has finished• Thread to be canceled is target thread• Two general approaches:

– Asynchronous cancellation terminates the target thread immediately

– Deferred cancellation allows the target thread to periodically check if it should be cancelled

• Pthread code to create and cancel a thread:

CS8493 27

Page 28: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Thread Cancellation (Cont.)

■ Invoking thread cancellation requests cancellation, but actual cancellation depends on thread state

■ If thread has cancellation disabled, cancellation remains pending until thread enables it

■ Default type is deferred● Cancellation only occurs when thread reaches cancellation

pointI.e. pthread_testcancel()Then cleanup handler is invoked

■ On Linux systems, thread cancellation is handled through signalsThreads overview, Multithreading

Models and IssuesCS8493 28

Page 29: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Thread-Local Storage

Threads overview, Multithreading Models and IssuesCS8493 29

• Thread-local storage (TLS) allows each thread to have its own copy of data

• Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

• Different from local variables– Local variables visible only during single function

invocation– TLS visible across function invocations

• Similar to static data– TLS is unique to each thread

Page 30: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

SchedulerActivations

r• Both M:M and Two-level models require

communication to maintain the appropriate numbe of kernel threads allocated to the application

• Typically use an intermediate data structure between user and kernel threads – lightweight process (LWP)– Appears to be a virtual processor on which

process can schedule user thread to run– Each LWP attached to kernel thread– How many LWPs to create?

• Scheduler activations provide upcalls - a communication mechanism from the kernel to the upcall handler in the thread library

• This communication allows an application to maintain theCcSo8r4r9e3ct number kernel threadTshreads overview,Multithreading 30Models and Issues

Page 31: Threads overview,Multithreading CS8493 1CS8493 28 Thread-Local Storage Threads overview,Multithreading Models and Issues CS8493 29 • Thread-local storage (TLS) allows each thread

Source of Information

Threads overview, Multithreading Models and IssuesCS8493 31

• Text book• https://www.tutorialspoint.com/operating_system/

os_multi_threading.htm• https://www.youtube.com/watch?

v=VVX73RUkRLY&list=PLAb-SL0AWW_YXixw-4F0TKCbtofkyI_0n&index=14