Chapter 4.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Chapter 4: Threads Operating System Concepts Essentials – 2nd Edition Silberschatz, Galvin and Gagne ©2013 Chapter 4: Threads n Overview n Multicore Programming n Multithreading Models...

Chapter 4: Threads Operating System Concepts Essentials – 2nd Edition Silberschatz, Galvin and Gagne ©2013 Chapter 4: Threads n Overview n Multicore Programming n Multithreading Models n Thread Libraries n Implicit Threading n Threading Issues n Operating System Examples Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Objectives n To introduce the notion of a thread—a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems n To discuss the APIs for the Pthreads, Windows, and Java thread libraries n To explore several strategies that provide implicit threading n To examine issues related to multithreaded programming n To cover operating system support for threads in Windows and Linux Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Motivation n Most modern applications are multithreaded to make computers run fast n Threads run within application n Multiple tasks with the application can be implemented by separate threads Eg Microsoftword l Update display l Fetch data l Spell checking l Answer a network request block n Process creation is frocess controlwhile heavy-weight thread creation is light-weight n Can simplify code, increase efficiency n Kernels are generally multithreaded to handle multiple Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Multithreaded Server Architecture like TCP Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Benefits n Responsiveness – may allow continued execution if part of process is blocked, especially important for user interfaces n Resource Sharing – threads share resources of process, easier than shared memory or message passing n Economy – cheaper than process creation, thread switching lower overhead than context switching n Scalability – process can take advantage of multiprocessor architectures Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Multicore Programming CPULedinfores CPU n Finding Multicore or multiprocessor systems putting pressure on programmers, challenges include: l Dividing activities l Balance l Data splitting l Data dependency l Testing and debugging n Parallelism implies a system can perform more than one task simultaneously n Concurrency supports more than one task making progress l Single processor / core, scheduler providing concurrency Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Multicore Programming (Cont.) Types of parallelism th.EE fiIdiametask n l Data parallelism – distributes subsets of the same data across multiple cores, same operation on each l Task parallelism – distributing threads across cores, each thread performing unique operation each thread does diff task n As # of threads grows, so does architectural support for threading l CPUs have cores as well as hardware threads l Consider Oracle SPARC T4 with 8 cores, and 8 hardware threads per core Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Concurrency vs. Parallelism n Concurrent execution on single-core system: n Parallelism on a multi-core system: Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Single and Multithreaded Processes whybreakintothread to speed up lessmemoryconsumption Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 neverasks Amdahl’s Law n Identifies performance gains from adding additional cores to an application that has both serial and parallel components n S is serial portion serialcomponents moreimp for n N processing cores performance gain n That is, if application is 75% parallel / 25% serial, moving from 1 to 2 cores results in speedup of 1.6 times n As N approaches infinity, speedup approaches 1 / S Serial portion of an application has disproportionate effect on performance gained by adding additional cores n But does the law take into account contemporary multicore systems? Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 User Threads and Kernel Threads n User threads - management done by user-level threads library n Three primary thread libraries: l POSIX Pthreads l API Windows threads l Java threads n Kernel threads - Supported by the Kernel n Examples – virtually all general purpose operating systems, including: l Windows l Solaris l Linux thread l Tru64 UNIX L Library l Mac OS X system call kernel thread level library Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Multithreading Models n Many-to-One n One-to-One n Many-to-Many Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Many-to-One n Many user-level threads mapped to single kernel thread n One thread blocking causes all to block n Multiple threads may not run in parallel on muticore system because only one may be in kernel at a time because only l offue n Few systems currently use this model n Examples: l Solaris Green Threads l GNU Portable Threads mobile.ve f Eat Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 One-to-One n Each user-level thread maps to kernel thread n Creating a user-level thread creates a kernel thread n More concurrency than many-to-one mikanthis ing n Number of threads per process sometimes restricted due to overhead n Examples l Windows l Linux l Solaris 9 and later Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads n Allows the operating system to create a sufficient number of kernel threads n Solaris prior to version 9 n Windows with the ThreadFiber package whenreleasedused Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Two-level Model n Similar to M:M, except that it allows a user thread to be bound to kernel thread n Examples Ei l IRIX l HP-UX l Tru64 UNIX l Solaris 8 and earlier why ES Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Thread Libraries n Thread library provides programmer with API for creating and managing threads n Two primary ways of implementing l Library entirely in user space l Kernel-level library supported by the OS Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Pthreads n May be provided either as user-level or kernel-level n A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization n Specification, not implementation 1itstandardwaytoI Proietti n API specifies behavior of the thread library, implementation is life up to development of the library n Common in UNIX operating systems (Solaris, Linux, Mac OS X) Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Pthreads Example Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 PthreadsExample Pthreads Example (Cont.) (Cont.) Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Pthreads Code for Joining 10 Threads Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Windows Multithreaded C Program Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Windows Multithreaded C Program (Cont.) Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Java Threads n Java threads are managed by the JVM n Typically implemented using the threads model provided by underlying OS n Java threads may be created by: l Extending Thread class l Implementing the Runnable interface Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Java Multithreaded Program as Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Java Multithreaded Program (Cont.) Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Implicit Threading n Growing in popularity as numbers of threads increase, program correctness more difficult with explicit threads n Creation and management of threads done why by compilers and run-time libraries rather than programmers n Three methods explored cover major 0s l Thread Pools l OpenMP l Grand Central Dispatch n Other methods include Microsoft Threading Building Blocks (TBB), java.util.concurrent package Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 Thread Pools n Create a number of threads in a pool where they await work n Advantages: l Usually slightly faster to service a request with an existing thread than create a new thread l Allows the number of threads in the application(s) to be bound to the size of the pool setting the limit l Separating task to be performed from mechanics of creating task allows different strategies for running task 4 i.e.Tasks could be scheduled to run periodically n Windows API supports thread pools: Operating System Concepts Essentials – 2nd Edition 4.0 Silberschatz, Galvin and Gagne ©2013 compare contrast OpenMP Threadpool OpenMp n Set of compiler directives and an API for C, C++, FORTRAN COBAL n Provides support for parallel programming in shared-memory environments n Identifies parallel regions – blocks of code that can run in parallel #pragma omp parallel Create as many threads as there are cores #pragma omp parallel for for(i=0;i

Use Quizgecko on...
Browser
Browser