Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Threads “Most modern applications are multithreaded”

Similar presentations


Presentation on theme: "Chapter 4 Threads “Most modern applications are multithreaded”"— Presentation transcript:

1 Chapter 4 Threads “Most modern applications are multithreaded”
Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues Operating-System Examples “Most modern applications are multithreaded”

2 4.1 Overview A web browser might have one thread display images or texts, another retrieves data from the network A server creates one thread for each request

3 Thread = light-weight process (LWP)
4.1 Overview Thread = light-weight process (LWP) Basic unit of CPU utilization Consists of a thread ID, a program counter, a register set, and a stack space Shares with peer threads its code section, data section, and OS resources (open files, signals), collectively known as a task Threads reside in the same address space and have access to the same data (one alters data; others see) One thread opens a file for reading; others can also read from that file Traditional or heavy-weight process is equal to a task with one single thread

4 Threads play a vital role in remote procedure call
4.1 Overview Threads play a vital role in remote procedure call RPC servers are multithreaded When a server receives a message, it services the message using a separate thread

5 Utilization of multiprocessor architecture
4.1 Overview: Benefits Responsiveness A program continues running even if part of it is blocking or is performing a lengthy operation Resource sharing Code sharing Economy More economical to create and context switch between threads (compared with processes) Utilization of multiprocessor architecture Each thread may run on different processors concurrently

6 4.2 Multicore Programming
Multithreaded programming provides a mechanism for more efficient use of multiple cores and improves concurrency Consider an application with four threads Challenges? Read pages 165—166 of the textbook.

7 4.2 Multicore Programming:二個名詞
Parallelism vs. concurrency 平行(parallelism):表示系統可以同時執行一項以上的工作 並行(concurrency):支援一個以上的工作/任務保持進展 單處理器/核心,排班器(scheduler)提供並行 Possible to have concurrency without parallelism

8 4.2 Multicore Programming:關於平行
資料平行(data parallelism) 分配同一份資料的子集合到多個運算核心,並在每一個核心執行相同的操作 Distributes subsets of the same data across multiple cores and perform the same operation on each core 任務平行(task parallelism) 分配執行緒到多個運算核心,每一個執行緒執行一個獨一無二的操作 硬體架構對於執行緒的支援:CPU有更多的核心和硬體執行緒的支援 例:Oracle SPARC T4有8核,每個核心有8個硬體執行緒 Oracle SPARC T4 有 8個核心,每個核心有8個硬體執行緒

9 4.3 Overview: User Threads
User-level threads are implemented in user-level libraries, rather than via system calls The library supports thread creation, scheduling, and management without kernel intervention Thread switching does not need to involve the OS Switching between user-level threads can be done independently of the OS and, hence, very fast But, if the kernel is single-threaded A system call can cause the entire process to wait until the system call returns A blocked process cannot get CPU time Scheduling can be unfair

10 4.1 Overview: Kernel Threads
Kernel threads are supported directly by OS Thread creation, scheduling, and management are done by the kernel in kernel space Code and data structures for the thread library exist in kernel space Invoking a function in the API for the library results in a system call to the kernel While one thread is blocked, other threads of the same task can still get CPU time Scheduling is fair Disadvantage Transfer of control from one thread to another within the same process requires mode switch

11 User Threads vs. Kernel Threads

12 4.3 Multithreading Models: Many-to-One Model
Many user-level threads mapped to one kernel thread Thread management is done in user space The entire process will block if a thread makes a blocking system call Only one thread can access the kernel at a time; multiple threads cannot run in parallel on multiprocessors Employed when user-level thread libraries are implemented on an OS that does not support kernel threads

13 4.3 Multithreading Models: One-to-One Model
Each user-level thread mapped to a kernel thread Allow another thread to run when a thread makes a blocking system call Drawback Creating a user thread requires creating the corresponding kernel thread  overhead 例: Windows NT/XP/2000, Linux

14 4.3 Multithreading Models: Many-to-Many Model
Many user level threads mapped to many kernel threads Allows the OS to create a sufficient number of kernel threads (can run in parallel on a multiprocessor) Free from shortcomings of N-to-1 and 1-to-1 models When a thread performs a blocking system call, the kernel can schedule another thread for execution

15 Many-to-Many Model: Two-level Model
Similar to M:M, but allows a user thread to be bound to kernel thread

16 Two primary ways of implementing
4.4 Thread Libraries Thread library provides programmers with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS

17 4.4 Thread Libraries: Pthread
POSIX (Portable Operating System Interface) standard (IEEE c) API (Application Program Ingterface) for thread creation and synchronization POSIX = set of standard UNIX-based interfaces API specifies behavior of the thread library; implementation is up to the library development Common in UNIX operating systems

18 Pthread Example #include <pthread.h> #include <stdio.h> int sum; /* shared by the threads */ void *runner(void *param) main(int argc, char *argv[]) { pthread_t tid; /* thread id */ pthread_attr attr; /* set of thread attributes as stack size, scheduling */ …. pthread_attr_init(&attr); /* get the default attributes */ pthread_create(&tid,&attr, runner, argv[1]); /* now wait for the thread to exit */ pthread_join(tid,NULL); printf("sum= %d\n",sum); } void *runner(void *param) { sum = …. pthread_exit(0); } 應用:多執行緒程式,計算1+2+…+100總和,Thread1計算1+2+…+20, Thread2計算21+22+…+40,…,Thread5計算81+82+…+100

19 書Fig. 4.9範例程式

20 於樹莓派平台編譯Fig. 4.9範例程式

21 擴充Fig. 4.9範例程式

22

23 使用Windows thread library創建多執行緒的範例

24 使用Java API創建多執行緒的範例

25 Java threads may be created by
Java supports (at the language level) for creation and management of threads i.e., managed by JVM, not by user-level library or kernel Java threads may be created by Extending Thread class Implementing the runnable interface Java threads are managed by the JVM

26 4.4 Java Threads: 使用 Extending Thread Class 之範例*
class Summation extends Thread { public Summation(int n) { upper = n; } public void run() { int sum = 0; …. public class ThreadTester { public static void main(…) { Summation thrd = new Summation(…); thrd.start(); } 2nd thread Creates the thread Allocate memory and initialize a new thread in JVM Call run() method

27 4.4 Java Threads: The Runnable Interface*
public interface Runnable { public abstract void run(); } Implement the Runnable interface class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker Thread”); }

28 4.4 Java Threads: Creating the Thread*
public class Second { public static void main(String args[]) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); System.out.println(“I am the main thread”); }

29 4.4 Java Threads: Java Thread Management*
suspend() – suspends execution of the currently running thread sleep() – puts the currently running thread to sleep for a specified amount of time resume() – resumes execution of a suspended thread stop() – stops execution of a thread

30 4.4 Java Threads: JVM and the Host OS*
JVM specification does not indicate how Java threads are mapped to the underlying OS Leaving decision to a particular implementation Winows NT/2000: one-to-one model One java thread  one kernel thread Solaris 2 Initially, many-to-one model Solaris 2.6: many-to-many model

31 Semantics of fork() and exec() system calls Thread cancellation
4.6 Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Associate each thread with its unique identifier

32 4.6 Threading Issues: fork() and exec()
Semantics of fork() and exec() system calls Thread fork() Duplicate the process (i.e. all threads) Or the new process is single-threaded UNIX systems have two different forks Thread exec() The program replace the entire process – all threads and LWPs fork()  exec() : duplicating all threads is unnecessary fork()  No exec(): duplicate all threads (i.e. the process)

33 4.6 Threading Issues: Thread Cancellation
Thread cancellation (cancelling the target thread) 例: Multiple threads search a database and one returns the result, the remaining might be cancelled Asynchronous cancellation One thread immediately terminates the target thread Deferred cancellation The target thread can periodically (at a cancellation point) check if it should terminate safely

34 4.6 Threading Issues: Thread Cancellation
Thread cancellation (cancelling the target thread) Difficulties Resources have been allocated to a cancelled thread Thread cancelled in the middle of updating shared data (especially troublesome with asynchronous cancellation) OS reclaims the system resources from a cancelled thread (but OS will not reclaim ALL RESOURCES) Most OS’ provide asynchronous cancellation Pthread provides deferred cancellation

35 4.6 Threading Issues: Signal Handling
Used in UNIX to notify a process that a particular event has occurred Receive types Synchronous (internal to the process) 例: division by zero, illegal memory access Asynchronous (external to the process) 例: <Ctrl>+<C>, timer expire Signal patterns Occurrence of a particular event triggers a signal Generated signal is delivered to the process Process must handle the signal

36 4.6 Threading Issues: Signal Handling
Every signal may be handled by A default signal handler Every signal has a default signal handler that is run by the kernel A user-defined signal handler The default signal handler may be overridden by a user-defined signal handler

37 4.6 Threading Issues: Signal Handling in Multithreaded Programs
Options Deliver the signal to the thread to which the signal applies Synchronous signals to the thread that generated the signal Deliver the signal every thread in the process 例: <Ctrl>+<C> asynchronous signal Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process (eg. Solaris)

38 4.5 Implicit Threading: Thread Pools
Thread creation per service request Overhead of creation Unlimiting the number of threads  exhausting the system resources Solution: thread pools Create a number of threads at process startup, and place them into a pool They wait for work No free thread in the pool, the service has to wait

39 Linux refers to them as tasks rather than threads
4.7 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call fork() duplicates a process clone() allows a child task to share the address space of the parent task (process) Parameters to specify the degree of sharing clone() without flag set = fork()

40 4.7 Linux Threads: clone() vs. fork()
Memory Fork() Clone() P1 P1 P1, P2 Copy task_struct fields P2

41 4.7 Linux Threads: Linux Process Table
Task table P1 Task_struct P2 P3 Task_struct P2 is a clone of P1 P3 is fork of P1

42 4.7 Linux Threads: Fields of task_struct
State: executing, ready, suspended, stopped, zombie Scheduling information: normal or real-time, a priority Identifiers: unique process ID, user and group IDs Interprocess communication: Links: link to parent; links to siblings; links to its children Times and timers: process creation time, the amount of processor time consumed File system: pointers to opened files (by this process) Virtual memory: defines the virtual memory assigned to this process Processor-specific context registers and stack


Download ppt "Chapter 4 Threads “Most modern applications are multithreaded”"

Similar presentations


Ads by Google