1 / 41

Chapter 5: Threads

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads(POSIX thread) Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads. Multithreading vs. Single threading. Multithreading: OS supports multiple threads of execution within a single process

blue
Télécharger la présentation

Chapter 5: Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5: Threads • Overview • Multithreading Models • Threading Issues • Pthreads(POSIX thread) • Solaris 2 Threads • Windows 2000 Threads • Linux Threads • Java Threads Operating System Concepts

  2. Multithreading vs. Single threading • Multithreading: OS supports multiple threads of execution within a single process • Single threading: OS does not recognize the concept of thread • MS-DOS supports a single user process and a single thread • Traditional UNIX supports multiple user processes but only supports one thread per process • Modern OS such as Solaris, Windows, supports multiple threads Operating System Concepts

  3. Threads • Thread, sometimes called lightweight process(LWP) • Traditional process,sometimes called heavyweight process(HWP) • A thread is an execution path within a process (线程是“进程中的一条执行路径或线索”,或“进程中的一个可调度实体”) • Has an execution state (running, ready, etc.) • Saves thread context when not running • Has an execution stack and some per-thread static storage for local variables • Has access to the memory address space and resources of its process • all threads of a process share this • when one thread alters a (non-private) memory item, all other threads (of the process) sees that • a file opened with one thread, is available to others Operating System Concepts

  4. Single and Multithreaded Processes Operating System Concepts

  5. Benefits • Responsiveness:可以获得快速的用户响应,如在C/S模式下,web server为每个用户连接运行一个线程;RPC服务器中,RPC服务进程会开启多个线程服务于每个RPC request • Resource Sharing:进程是拥有资源的基本单位(CPU,地址空间,I/O资源),进程中的线程可以共享这些资源 • Economy: 创建线程比创建进程更快,进程内的线程切换(context switch)比进程更快,solaris中创建线程比进程快30倍,线程切换比进程切换快5倍 • Utilization of SMP Architectures:可以充分利用多处理器体系结构,使得一个进程中的线程在不同的处理器上运行,提高进程执行的并行度 Operating System Concepts

  6. Application benefits of threads • Consider an application that consists of several independent parts that do not need to run in sequence(一些应用程序可以分成若干相对独立的部分) • Each part can be implemented as a thread(每一部分由一个线程来实现) • Whenever one thread is blocked waiting for an I/O, execution could possibly switch to another thread of the same application (instead of switching to another process) Operating System Concepts

  7. Application benefits of Threads • Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel (线程间通信无需内核干预) • Therefore necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data(需要进行线程间同步) Operating System Concepts

  8. Example of inconsistent view • 3 variables: A, B, C which are shared by thread T1 and thread T2 • T1 computes C = A+B • T2 transfers amount X from A to B(转帐) • T2 must do: A = A -X and B = B+X (so that A+B is unchanged) • If T1 computes A+B after T2 has done A = A-X but before B = B+X, then T1 will not obtain the correct result for C = A + B Operating System Concepts

  9. Threads States • Three key states: running, ready, blocked • They have no suspend state because all threads within the same process share the same address space • Indeed: suspending (ie: swapping) a single thread involves suspending all threads of the same process • Termination of a process, will terminates all threads within the process Operating System Concepts

  10. User Level Threads(ULT) • The kernel is not aware of the existence of threads • All thread management is done by the application by using a user-level thread library • Thread switching does not require kernel mode privileges (no mode switch) • Scheduling is application specific(but may depend on OS support) • Examples - POSIX Pthreads - Mach C-threads - Solaris threads Operating System Concepts

  11. User Level Threads library • Contains codes for: • creating and destroying threads • passing messages and data between threads • scheduling thread execution • saving and restoring thread contexts Operating System Concepts

  12. Kernel activity for User Threads • The kernel is not aware of thread activity but it is still managing process activity • When a thread makes a system call, the whole process will be blocked • but that thread is still perceived as in the running state by the thread library • So thread states are independent of process states Operating System Concepts

  13. Advantages and inconveniences of ULT • Advantages • Thread switching does not involve the kernel: no mode switching • Scheduling can be application specific: choose the best algorithm. • ULTs can run on any OS. Only needs a thread library • Inconveniences • Most system calls are blocking and the kernel blocks processes. So all threads within the process will be blocked • The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors Operating System Concepts

  14. Kernel Level Threads(KLT) • All thread management is done by kernel • No thread library but an API to the kernel thread facility • Kernel maintains context information for the process and the threads • Switching between threads requires the kernel • Scheduling on a thread basis • Examples - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS Operating System Concepts

  15. Advantages and inconveniences of KLT • Advantages • the kernel can simultaneously schedule many threads of the same process on many processors • blocking is done on a thread level • kernel routines can be multithreaded • Inconveniences • thread switching within the same process involves the kernel. We have 2 mode switches per thread switch • this results in a significant performance slowing down Operating System Concepts

  16. Multithreading Models • Many-to-One • One-to-One • Many-to-Many Operating System Concepts

  17. Many-to-One • Many user-level threads mapped to single kernel thread.(纯用户级线程) • Usually used on systems that do not support kernel threads. Operating System Concepts

  18. Many-to-One Model Operating System Concepts

  19. One-to-One • Each user-level thread maps to kernel thread.(纯核心级线程) • Examples - Windows 95/98/NT/2000 - OS/2 Operating System Concepts

  20. One-to-one Model Operating System Concepts

  21. Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads.(混合式线程) • Takes advantage of many-to-one and one-to-one models • Allows the operating system to create a sufficient number of kernel threads. • Solaris 2 • Windows NT/2000 with the ThreadFiber package Operating System Concepts

  22. Many-to-Many Model Operating System Concepts

  23. Threading Issues • Semantics of fork() and exec() system calls. - exec will make the specified program to replace the entire process - if fork in a thread is immediately followed by exec, it will duplicate only the calling thread, otherwise, all threads in the process will be duplicated. • Thread cancellation. - terminate a thread before its completion - thread to be terminated is referred to as target thread - asynchronous:immediately terminated by another thread. - deffered:thread check itself for cancellation and terminate. Operating System Concepts

  24. Signal handling - signal is used in UNIX to notify a process that a particular event has occurred. - signal may be synchronous or asynchronous: * synchronous: signals are delivered to the same process that caused the signal(e.g.illegal memory access, divide by zero); * asynchronous:notified by an event external to the process. - signal must be handled - method of handling a signal * ignore(some signals must not be ignored,e.g. SIGKILL); * default signal handler * user defined signal handler - in a multithreaded program, signals may be sent to different threads - windows2000 uses asynchronous procedure call(APC) to simulate signal. Operating System Concepts

  25. Thread pools - avoid too many threads from exhausting system resources(CPU,memory,etc.) - a pool of threads is created in advance to sit and wait for work - faster to service a request with an existing thread than waiting to create one. - number of threads is limited. - example: multi-threaded server architecture in database management systems. • Thread specific data - a thread might need to own its private data Operating System Concepts

  26. Pthreads(POSIX thread) • a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. • API specifies behavior of the thread library, implementation is up to development of the library. • Common in UNIX operating systems. Operating System Concepts

  27. Threads on linux(LinuxThreads) • Linux refers to them as tasks rather than threads. • Thread creation is done through clone() system call. • Clone() allows a child task (lightweight process) to share the resources of the parent process( address space, files, signal-handling, etc.) • Pthread implementation on linux (pthread_create) makes use of clone,hence do_fork, to create threads(lightweight processes) • Hence pthread in linux is implemented as lightweight process • But it does not completely conform to POSIX thread specifications. e.g. thread created by pthread_create has pid different from the calling thread(process) • Kernel 2.6 NPTL(Native Posix Thread Library),by redhat Operating System Concepts

  28. #include <pthread.h> #include <stdio.h> int sum; void *runner(void *param); main(int argc, char *argv[]) { ptread_t id; pthread_attr_t attr; pthread_attr_init(&attr); //初始化线程属性为缺省属性 pthread_create(&tid,&attr,runner,argv[1]); //创建线程 pthread_join(tid,NULL); //等待线程tid结束 printf(“sum=%d\n”,sum); } void *runner(void *param) { int upper=atoi(param); int i; sum = 0; if (upper >0) for ( i = 1; i <=upper; i++) sum +=i; pthread_exit(0); } Operating System Concepts

  29. Solaris 2 Threads Operating System Concepts

  30. Solaris Process Operating System Concepts

  31. Solaris: versatility • Implements the many-to-many mapping. • We can use ULTs when logical parallelism does not need to be supported by hardware parallelism (we save mode switching) • e.g.: Multiple windows but only one is active at any one time • If threads may block then we can specify two or more LWPs to avoid blocking the whole application • Type of ULTs: • Bounded: bounded to a particular LWP • Unbounded: may transit within a pool of LWPs Operating System Concepts

  32. Solaris: Lightweight Process States LWP states are independent of ULT states (except for bound ULTs) Operating System Concepts

  33. Solaris: user-level thread states (attached to a LWP) Operating System Concepts

  34. Windows 2000 Threads • Windows 2000 processes and threads are all implemented as objects. • Implements the one-to-one mapping. • basic thread components - a thread id - register set - separate user and kernel stacks - private data storage area(local variables) • A thread can be one of six states: ready(就绪), standby(备用), running(运行), waiting(等待), transition(转换), and terminated(终止). Operating System Concepts

  35. Windows 2000 thread states Runnable Standby Pick to Run Switch Preempted Ready Running Unblock/Resume Resource Available Resource Available Terminate Block/ Suspend Transition Waiting Terminated Unblock Resource Not Available Not Runnable Operating System Concepts

  36. Process ID Security Descriptor Base priority Default processor affinity Quota limits Execution time I/O counters VM operation counters Exception/debugging ports Exit status Windows 2000 Process Object Attributes Operating System Concepts

  37. Thread ID Thread context Dynamic priority Base priority Thread processor affinity Thread execution time Alert status Suspension count Impersonation token Termination port Thread exit status Windows 2000 Thread Object Attributes Operating System Concepts

  38. 3 main data structures of a thread • ETHREAD(executive thread block,执行体线程块) - in kernel space • KTHREAD(kernel thread block,内核线程块) - in kernel space • TEB(thread environment block,线程环境块) - in user process space Operating System Concepts

  39. Windows NT/2000 fibers(构造) • Also called light-weight thread • Invisible to OS, similar to user-level thread • Win32 APIs for fiber: CreateFiber, SwitchToFiber, ConverThreadToFiber Operating System Concepts

  40. Java Threads • Java threads may be created by: • Creating(extending) Thread class -thread is actually created when the start method in the class is invoked. • Java threads are managed by the JVM. Operating System Concepts

  41. Java Thread States Operating System Concepts

More Related