Beruflich Dokumente
Kultur Dokumente
Introduction Definition of Thread Motivation for Threads Thread States: Life Cycle of a Thread Thread Operations Threading Models
User-Level Threads Kernel-Level Threads Combining User- and Kernel-Level Threads
4.7
4.7.1 4.7.2
Objectives After reading this chapter, you should understand: the motivation for creating threads. the similarities and differences between processes and threads. the various levels of support for threads. the life cycle of a thread. thread signaling and cancellation. the basics of POSIX, Linux, Windows XP and Java threads.
Recent Developments in Processors 1. 32-bit processors are replaced by 64-bit processors. Software lags behind hardware by 2-4 years. 2. Multi-core processors will be dominant. It is much easier to increase the number of cores than to increase processor clock frequencies. 3. Netflix uses about 30% of Internet traffic at night.
2004 Deitel & Associates, Inc. All rights reserved.
Instruction TLB
Although threads seem to be a small step from sequential computation, in fact, they represent a huge step. They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism. Threads, as a model of computation, are wildly non-deterministic, and the job of the programmer becomes one of pruning that non-determinism.
2004 Deitel & Associates, Inc. All rights reserved.
Single CPU
10
11
Observations: 1. To transmit a 2-hour MPEG movie, or 2 Gbyte, requires transmissions by software servers of more than 1 million packets, with maximum data of 1,500 bytes. 2. Higher-definition movies require 40 Gbytes or more. 3. In late 2013, Federal Health Exchange website limited 50,000 users to be logged on at any time.
2004 Deitel & Associates, Inc. All rights reserved.
12
4.1 Introduction
General-purpose languages such as Java, C#, Visual C++ .NET, Visual Basic .NET and Python have made concurrency primitives available to applications programmer Multithreading
Programmer specifies applications contain threads of execution Each thread designate a portion of a program that may execute concurrently with other threads
13
Word processor
Kernel 1. Accept inputs from a keyboard or mouse, 2. Display text and graphics on the video monitor, 3. Send outputs to a printer
2004 Deitel & Associates, Inc. All rights reserved.
14
15
Three-thread Word Processor Thread 1 interacts with a user. Thread 2 reformats the document when commanded by Thread 1.
Thread 3 outputs to a printer when commanded by Thread 1.
It should be clear that three processes would not work here because they all need to have access to the same document (in the memory). By having 3 threads, which share a common memory, all threads have access to the document being edited.
16
Kernel
Network connection
2004 Deitel & Associates, Inc. All rights reserved.
17
18
19
Worker thread
while(TRUE) { wait_for_work(&buf); look_for_page_in_cache(&buf, &page); if( page_not_in_cache(&page)) read_page_from_disk(&page); return_page(&page); }
20
Threads may be managed by the operating system or by a user application Examples: Win32 threads, C-threads, Pthreads
21
22
4.3 Motivation for Threads Threads have become prominent due to trends in Software design More naturally expresses inherently parallel tasks Performance Scales better to multiprocessor systems (each thread can be executed by a processor) Cooperation Shared address space incurs less overhead than IPC
2004 Deitel & Associates, Inc. All rights reserved.
23
Benefits of Threads 1. Responsive to users inputs: a multi-thread process will continue to run even if part of it (a thread) is blocked or it is performing a lengthy operation. 2. Resource Sharing: Threads share the memory and the resources of the process 3. Economy: it takes less processor time to create and manage threads than processes. 4. Scalability: threads can run concurrently on different processing cores, while a process with a single thread can run on only one core.
2004 Deitel & Associates, Inc. All rights reserved.
24
4.3 Motivation for Threads Each thread transitions among a series of discrete thread states Threads and processes have many operations in common (e.g. create, exit, resume, and suspend) Thread creation does not require operating system to initialize resources that are shared between parent processes and its threads
Reduces overhead of thread creation and termination compared to process creation and termination
25
4.4 Thread States: Life Cycle of a Thread Thread states Born state Ready state (runnable state) Running state Dead state Blocked state Waiting state Sleeping state
Sleep interval specifies for how long a thread will sleep
26
27
12
19
18
15
sort thread 0
3 7 12 18 19 2 4 6
sort thread 1
8 15
merge thread
2 3 4 6 7 8 12 15 18 19
28
Example 1: Multi-threaded sorting application Assume that an array a[n] with n entries is to be sorted. It is stored in a global array to be accessed by all threads. The Sort Thread 0 sorts the first half of the array, a[0] to a[n/2 -1], The Sort Thread 1 sorts the second half of the array, a[n/2] to a[n -1], The merge thread combines the two sorted sub-arrays into one array, b[n], which is another global array.
29
void merge(int a[], int n, int b[]) { //purpose: to merge two sorted arrays, a[0 to n/2-1] //and a[n/2 to n-1] into array b.
for( int i=0; i<n; i++) {
int i0, i1; i0=0; i1=n/2; if( a[i0] < a[i1]) { b[i] = a[i0]; i0++; } else { b[i] = a[i1]; i1++; }
}
2004 Deitel & Associates, Inc. All rights reserved.
30
31
bool checkDigit(int a[][9], int rowStart, int rowEnd, int columnStart, int columnEnd) {//purpose: to check that a given Row, column or //3*3 block contains 1 to 9. int count[9]; for( int i=0; i<9; i++) count[i] = 0; for( int row=rowStart; row<rowEnd; row++)
for( int column=columnStart; column<columnEnd; column++)
count[ a[row][column]-1]++;
return true; }
2004 Deitel & Associates, Inc. All rights reserved.
32
33
34
Example-3 MPEG 8*8 Block Direct-Cosine-Transform A 640*480 image is divided into 80*60 blocks of 8 rows* 8 pixels/row. Each 8*8 block must perform one DCT independently. Thus each 8*8 DCT can be performed by one thread.
X(k1, k2) =
7 =0
, cos[
+0.5 2 8
+0.5 1 7 cos [ ] =0 8
35
Amdahls Law
If a N-core system runs an application with S portion of serial component, Speedup <= 1/[ S + (1-S)/N] Example: an application with 40% serial component,
2-core: 4-core:
36
Example of Parallel Code for(int i=0; i< n; i++) a[i] = b[i] + c[i];
for(int i=0; i< n/2; i++) a[i] = b[i] + c[i]; for(int i=n/2; i< n; i++) a[i] = b[i] + c[i];
37
38
Matrix Multiplication A = B * C int A[N][N], B[N][N], C[N][N]; for(int i=0; i<n; i++){ for(int j=0; j<m; j++) { A[i][j]=0; for( int k=0; k<p; k++) A[i][j] += B[i][k] * C[k][j]; }}
39
Parallel Code for Matrix Multiplication A = B * C int A[N][N], B[N][N], C[N][N]; for(int i=0; i<n/2; i++){ for(int j=0; j<m; j++) { A[i][j]=0; for( int k=0; k<p; k++) A[i][j] += B[i][k] * C[k][j]; }} for(int i=n/2; i<n; i++){ for(int j=0; j<m; j++) { A[i][j]=0; for( int k=0; k<p; k++) A[i][j] += B[i][k] * C[k][j]; }}
40
41
4.5 Thread Operations Thread operations do not correspond precisely to process operations
Cancel
Indicates that a thread should be terminated, but does not guarantee that the thread will be terminated Threads can mask the cancellation signal
Join
A primary thread can wait for all other threads to exit by joining them The joining thread blocks until the thread it joined exits
42
43
44
45
4.6.2 Kernel-level Threads Kernel-level threads attempt to address the limitations of user-level threads by mapping each thread to its own execution context
Kernel-level threads provide a one-to-one thread mapping
Advantages: Increased scalability, interactivity, and throughput Disadvantages: Overhead due to context switching and reduced portability due to OS-specific APIs
Kernel-level threads are not always the optimal solution for multithreaded applications
46
47
Worker threads
Persistent kernel threads that occupy the thread pool Improves performance in environments where threads are frequently created and destroyed Each new thread is executed by a worker thread
Scheduler activation
Technique that enables user-level library to schedule its threads Occurs when the operating system calls a user-level threading library that determines if any of its threads need rescheduling
48
49
Asynchronous
Occur due to an event typically unrelated to the current instruction Threading library must determine each signals recipient so that asynchronous signals are delivered properly
Each thread is usually associated with a set of pending signals that are delivered when it executes Thread can mask all signals except those that it wishes to receive
2004 Deitel & Associates, Inc. All rights reserved.
50
51
52
4.8 POSIX and Pthreads Threads that use the POSIX threading API are called Pthreads
POSIX states that processor registers, stack and signal mask are maintained individually for each thread POSIX specifies how operating systems should deliver signals to Pthreads in addition to specifying several thread-cancellation modes
53
4.9 Linux Threads Linux allocates the same type of process descriptor to processes and threads (tasks) Linux uses the UNIX-based system call fork to spawn child tasks To enable threading, Linux provides a modified version named clone
Clone
accepts arguments that specify which resources to share with the child task
54
55
56
Windows XP provides each process with a thread pool that consists of a number of worker threads, which are kernel threads that execute functions specified by user threads
57
58
Java allows the application programmer to create threads that can port to many computing platforms Threads
Created by class Thread Execute code specified in a Runnable objects run method
59
60
61
62
63
Reference Andrew Tanenbaum, Moden Operating Systems, 2nd Edition, Prentice-Hall, 2001.