Linux編程多線程.ppt_第1頁
Linux編程多線程.ppt_第2頁
Linux編程多線程.ppt_第3頁
Linux編程多線程.ppt_第4頁
Linux編程多線程.ppt_第5頁
已閱讀5頁,還剩89頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Linux Programming Pthread,www.rt- ,Topics,Threads Pthread Pthread Mutex Pthread Condition variables Pthread Main API,Linux Programming Threads,Threads,Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads,Single vs. Multithreaded Processes,Process: ev

2、erything weve seen up to now Thread: like a process, only shares memory, global variables, files, PID with other threads within the same process,use threads or processes?,Why use threads instead of processes? Faster context switch Easier to share data Uses less memory and resources Thread creation f

3、aster than process creation Less things to set-up Why use processes instead of threads? Different code Running on different machines Different owners Little communication Sharing memory can lead to obscure bugs,User-level threads,Threads can be provided at the user or kernel level User level: kernel

4、 knows nothing about threads Implemented in a library by somebody without touching the kernel User library handles Thread creation Thread deletion Thread scheduling Benefits: Faster creation and scheduling Drawbacks: One thread blocking during I/O blocks all threads in process (even ready-to-run thr

5、eads),User-level threads (contd),Three primary thread libraries: POSIX Pthreads Win32 threads Java threads,Kernel-level threads,Kernel knows about threads Kernel handles thread creation, deletion, scheduling Benefits: Kernel can schedule another thread if current one does blocking I/O Kernel can sch

6、edule multiple threads on different CPUs on SMP multiprocessor Drawbacks: Slower to schedule, create, delete than user-level Most modern OSes support kernel-level threads Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X,Multithreading Models,How to map kernel-level threads to user-level threads? Ma

7、ny-to-One One-to-One Many-to-Many,Many-to-One Model,Many-to-One,Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads Any disadvantages? All block when one blocks All run on 1 CPU,One-to-one Model,One-to-One,Each user-level thread maps to a kerne

8、l thread Examples: Windows NT/XP/2000 Linux Solaris 9 and later Any disadvantages? Overhead for creating threads Many operating systems limit number of threads,Many-to-Many Model,Many-to-Many Model,Allows many user level threads to be mapped to smaller or equal number of kernel threads Allows the fl

9、exibility of choosing the number of kernel threads allocated to a process “Best of both worlds” Solaris prior to version 9,Two-level Model,Two-level Model,Similar to many-to-many, but allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier,Threading Iss

10、ues,Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread-specific data,Semantics of fork() and exec(),Does fork() duplicate only the calling thread or all threads? Some UNIX systems have two versions of fork() exec() usually replaces all threads with ne

11、w program,fork1(),fork2(),Thread Cancellation,Terminating a thread before it has finished E.g., two cooperating threads, one discovers an error Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically ch

12、eck if it should be cancelled Why is this an issue? What if a thread is in the middle of Allocating resources Performing I/O Updating a shared data structure,Thread Cancellation (contd),Essentially, deferred cancellation = “target, please cancel yourself” Occurs when target checks for cancellation s

13、ignal Allows cancellation at “safe” points Called cancellation points in Pthreads,Signal Handling,Signals are used in UNIX to notify a process that a particular event has occurred A signal handler is used to process signals Signal is generated by a particular event Signal is delivered to a process S

14、ignal is handled (or ignored/blocked) Options: Deliver the signal to the thread to which the signal applies Applicable with synchronous signals e.g., illegal memory access Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific threat

15、to receive all signals for the process,Thread Pooling,When design situations arise that could benefit by using many short-lived threads, thread pooling is a useful technique. Rather than create a brand new thread for each task, you can have one of the threads from the thread pool pulled out of the p

16、ool and assigned to the task. When the thread is finished with task, it adds itself back to the pool and waits for another assignment,Benefits of Thread Pooling,You can reduce response time because a thread is already constructed and started and is simply waiting for its next task. In the case of an

17、 HTTP server, an available thread in the pool can deliver each new file requested Threads are fixed in size at the time of construction. All the threads are started, and then each goes into a wait state until a task is assigned to it. If all the threads are currently assigned a task, new service req

18、uests will put into a wait state until one of the threads finishes its task and returns itself to the pool.,Thread Pools,Motivating example: a web server running on an SMP machine To handle each connection: Create a new process to handle it too slow, inefficient Create a new thread to handle it Opti

19、on 2 better but still has some problems: Some overhead for thread creation/deletion Thread will only be used for this connection Unbounded number of threads might crash web server Better solution: use a thread pool of (usually) fixed size,Thread Pools (contd),Threads in pool sit idle Request comes i

20、n: Wake up a thread in pool Assign it the request When it completes, return it to pool If no threads in pool available, wait Advantages: Usually slightly faster to wake up an existing thread than create a new one Allows the number of threads in the application to be limited by the size of the pool M

21、ore sophisticated systems dynamically adjust pool size,Thread-specific Data,Allows each thread to have its own copy of data Useful for implementing protection For example, user connects to banks database server Server process responds, has access to all accounts Multiple people may be accessing thei

22、r accounts at the same time Thread assigned to manipulate or view users bank account Using thread-specific data limits (unintentional or erroneous) access by other threads,Pthreads,A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization NOT an implementation Implementation in dif

23、ferent OSes may be using user or kernel threads Common in UNIX operating systems (Solaris, Linux, Mac OS X) Fairly portable Available on pyrite Has man pages on Linux (check “man pthread_create”) To use: #include Link with pthread library: g+ prog.cc -lpthread,Windows XP Threads,Implements the one-t

24、o-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads,Linux Threads,Linux refers to them as tasks rather than threads Thread creation is done thr

25、ough the clone() system call clone() allows a child task to share different things with the parent task such as: the address space the table of open files the signal handlers If nothing is shared: same as fork(), creates essentially a process If everything is shared: creates a thread as we know it,L

26、inux Threads (contd),int clone(int (*fn)(void *), void *child_stack, int flags, void *arg); clone() is used to create kernel-level threads,files,memory space,To the user: PCB1 and PBC2 are “kernel-level threads within the same process” To the kernel: PCB1 and PCB 2 are processes (tasks),Java Threads

27、,Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface Details in textbook,Linux Programming Pthread,Thread Basic,Thread operations include thread creation, termination, synchronization, data management Threads in the same proc

28、ess share: Process address space, instructions and most data Opened files Signals and signal handles Each thread has a unique: Thread ID A register set Stack Local variables and return address,Pthreads: POSIX Threads,Pthreads is a standard set of C library functions for multithreaded programming IEE

29、E Portable Operating System Interface, POSIX, section 1003.1 standard, 1995 Pthread Library (60+ functions) Thread management: create, exit, detach, join, . . . Thread cancellation Mutex locks: init, destroy, lock, unlock, . . . Condition variables: init, destroy, wait, timed wait, . . . . . . Progr

30、ams must include the file pthread.h Programs must be linked with the pthread library (-lpthread),Pthreads Naming Convention,Types: pthread_object_t Functions: pthread_object_action Constants/Macros: PTHREAD_PURPOSE Examples: pthread_t: the type of a thread pthread_create(): creates a thread pthread_

31、mutex_t: the type of a mutex lock pthread_mutex_lock(): lock a mutex PTHREAD_CREATE_DETACHED,Pthread POSIX thread,Include pthread header file # include Compile and execution C compiler: gcc o foo foo.c -lpthread C+ compiler: g+ -o foo foo.c lpthread Execution: ./foo,Example,Pthread POSIX thread,Basi

32、c pthread functions pthread_create create child thread pthread_exit thread termination pthread_join wait for thread termination phtread_mutex_lock lock critical section pthread_mutex_unlock unlock critical section pthread_cond_wait wait for a condition signal phtread_cond_signal wake up one waiting

33、thread pthread_cond_broadcast wake up all waiting threads,pthread_self(),Returns the thread identifier for the calling thread At any point in its instruction stream a thread can figure out which thread it is Convenient to be able to write code that says: “If youre thread 1 do this, otherwise do that

34、” However, the thread identifier is an opaque object (just a pthread_t value) you must use pthread_equal() to test equality pthread_t pthread_self(void); int pthread_equal(pthread_t id1, pthread_t id2);,pthread_create(),Creates a new thread int pthread_create ( pthread_t *thread, pthread_attr_t *att

35、r, void * (*start_routine) (void *), void *arg); Returns 0 to indicate success, otherwise returns error code thread: output argument for the id of the new thread attr: input argument that specifies the attributes of the thread to be created (NULL = default attributes) start_routine: function to use

36、as the start of the new thread must have prototype: void * foo(void*) arg: argument to pass to the new thread routine If the thread routine requires multiple arguments, they must be passed bundled up in an array or a structure,pthread_create() example,Want to create a thread to compute the sum of th

37、e elements of an array void *do_work(void *arg); Needs three arguments the array, its size, where to store the sum we need to bundle them in a structure struct arguments double *array; int size; double *sum; ,pthread_create() example,int main(int argc, char *argv) double array100; double sum; pthrea

38、d_t worker_thread; struct arguments *arg; arg = (struct arguments *)calloc(1, sizeof(struct arguments); arg-array = array; arg-size=100; arg-sum = . ,pthread_create() example,void *do_work(void *arg) struct arguments *argument; int i, size; double *array; double *sum; argument = (struct arguments*)a

39、rg; size = argument-size; array = argument-array; sum = argument-sum; *sum = 0; for (i=0;isize;i+) *sum += arrayi; return NULL; ,Comments about the example,The “main thread” continues its normal execution after creating the “child thread” IMPORTANT: If the main thread terminates, then all threads ar

40、e killed! We will see that there is a join() function Of course, memory is shared by the parent and the child (the array, the location of the sum) nothing prevents the parent from doing something to it while the child is still executing which may lead to a wrong computation we will see that Pthreads

41、 provide locking mechanisms The bundling and unbundling of arguments is a bit tedious,Memory Management of Args,The parent thread allocates memory for the arguments Warning #1: you dont want to free that memory before the child thread has a chance to read it That would be a race condition Better to

42、let the child do the freeing Warning #2: if you create multiple threads you want to be careful there is no sharing of arguments, or that the sharing is safe For instance, if you reuse the same data structure for all threads and modify its fields before each call to pthread_create(), some threads may

43、 not be able to read the arguments destined to them Safest way: have a separate arg structure for each thread,pthread_exit(),Terminates the calling thread void pthread_exit(void *retval); The return value is made available to another thread calling a pthread_join() (see next slide) My previous examp

44、le had the thread just return from function do_work() In this case the call to pthread_exit() is implicit The return value of the function serves as the argument to the (implicitly called) pthread_exit().,pthread_join(),Causes the calling thread to wait for another thread to terminate int pthread_jo

45、in( pthread_t thread, void *value_ptr); thread: input parameter, id of the thread to wait on value_ptr: output parameter, value given to pthread_exit() by the terminating thread (which happens to always be a void *) returns 0 to indicate success, error code otherwise multiple simultaneous calls for

46、the same thread are not allowed,pthread_kill(),Causes the termination of a thread int pthread_kill( pthread_t thread, int sig); thread: input parameter, id of the thread to terminate sig: signal number returns 0 to indicate success, error code otherwise,pthread_join() example,int main(int argc, char

47、 *argv) double array100; double sum; pthread_t worker_thread; struct arguments *arg; void *return_value; arg = (struct arguments *)calloc(1,sizeof(struct arguments); arg-array = array; arg-size=100; arg-sum = ,pthread_join() Warning,This is a common “bug” that first-time pthread programmers encounte

48、r Without the call to pthread_join() the previous program may end immediately, with the main thread reaching the end of main() and exiting, thus killing all other threads perhaps even before they have had a chance to execute,pthread_join() Warning,When creating multiple threads be careful to store t

49、he handle of each thread in a separate variable Typically one has an array of thread handles That way youll be able to call pthread_join() for each thread Also, note that the following code is sequential! for (i=0; i num_threads; i+) pthread_create( Return 0 to indicate success, error code otherwise

50、 attr: pointer to a thread attribute,Detached Thread,One option when creating a thread is whether it is joinable or detached Joinable: another thread can call join on it By default a thread is joinable Detached: no thread can call join on it Lets look at the function that allows to set the “detached

51、 state”,pthread_attr_setdetachstate(),Sets the detach state attribute int pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate); returns 0 to indicate success, error code otherwise attr: input parameter, thread attribute detachstate: can be either PTHREAD_CREATE_DETACHED PTHREAD_CREATE

52、_JOINABLE (default),Detach State,Detached threads have all resources freed when they terminate Joinable threads have state information about the thread kept even after they finish To allow for a thread to join a finished thread So-called “no rush to join” So, if you know that you will not need to jo

53、in a thread, create it in a detached state so that you save resources This is lean-and-mean C, as opposed to hand-holding Java, and every little saving is important,Creating a Detached Thread,#include #define NUM_THREAD 25 void *thread_routine (void *arg) printf(“Thread %d, my TID is %un”, (int)arg,

54、 pthread_self(); pthread_exit(0); ,Creating a Detached Thread,int main() pthread_attr_t attr; pthread_t tidsNUM_THREADS; int x; pthread_attr_init( . . . / should take a while otherwise . . . / the child may not have time to run ,Mutual Exclusion and Pthreads,Pthreads provide a simple mutual exclusio

55、n lock Lock creation int pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); returns 0 on success, an error code otherwise mutex: output parameter, lock attr: input, lock attributes NULL: default There are functions to set the attribute (look at the man pages if youre inter

56、ested),Linux Programming pthread mutex,Pthread: Locking,Locking a lock If the lock is already locked, then the calling thread is blocked If the lock is not locked, then the calling thread acquires it int pthread_mutex_lock( pthread_mutex_t *mutex); returns 0 on success, an error code otherwise mutex

57、: input parameter, lock,Pthread: Locking,Just checking Returns instead of locking int pthread_mutex_trylock( pthread_mutex_t *mutex); returns 0 on success, EBUSY if the lock is locked, an error code otherwise mutex: input parameter, lock,Synchronizing pthreads,Releasing a lock int pthread_mutex_unlo

58、ck( pthread_mutex_t *mutex); returns 0 on success, an error code otherwise mutex: input parameter, lock Pthreads implement exactly the concept of locks as it was described in the previous lecture notes,Mutex example,Cleaning up memory,Releasing memory for a mutex attribute int pthread_mutex_destroy(

59、 pthread_mutex_t *mutex); Releasing memory for a mutex int pthread_mutexattr_destroy( pthread_mutexattr_t *mutex);,Linux Programming pthread Condition variables,Condition variables Condition variable is used with the appropriate functions for waiting and later continuation Creating/Destroying: pthread_cond_init() pthread_cond_t cond = PTHREAD_COND_INITIALIZER; Pthread_cond_destroy() Waiting on condition: pthread_cond_wait() pthread_cond_timewait(timeout) Waking thread based on condition: pthread_cond_signal() wake any one thread phread_con

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論