1 / 31

Chapter 5: Threads

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads. Thread definition. A thread, sometimes called LWP (Light Weight Process) is a basic unit of CPU utilization

gtoussaint
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 • Solaris 2 Threads • Windows 2000 Threads • Linux Threads • Java Threads Operating System Concepts

  2. Thread definition • A thread, sometimes called LWP (Light Weight Process) is a basic unit of CPU utilization • A traditional (Heavy Weight) process has a single thread • Multithreading applications : web browsers, text editors • Threads Share: • Code • Data • Files • Threads have their own: • Thread ID • Program counter • Register set • Stack Operating System Concepts

  3. Single and Multithreaded Processes Operating System Concepts

  4. Benefits • Responsiveness: Allow a program to continue running even if part of it is blocked or performing lengthy operations • Resource Sharing: By default, threads share memory resources of the process they belong to. • Economy: Allocating memory and other resources is costly. Threads share resources and are more economical to create, share memory among them and context switch. • Utilization of MP Architectures: Best utilization of Multiprocessors architectures (I.e. Parallel processors run each thread). Operating System Concepts

  5. Thread types • Two types of threads: • User threads • Kernel thread • The main difference is in who performs: • Thread creation • Thread scheduling • Thread management OS or User! Operating System Concepts

  6. User Threads • Thread management done by user-level threads library • Kernel is unaware of multithreading at the process level since all is handled by the User(typically via user libraries) • Examples of available User libraries for handling threads: - POSIX Pthreads - Mach C-threads - Solaris threads Operating System Concepts

  7. Kernel Threads • Supported by the Kernel • Examples (most contemporary OS supports Kernel threads): - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux Operating System Concepts

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

  9. Many-to-One • Many user-level threads mapped to single kernel thread. • Used on systems that do not support kernel threads. • Thread management is done is user space. • Advantage: • Thread management is efficient since all done in User space • Disadvantages: • Entire process will block if a thread makes a blocking system call • No support for parallel processors Operating System Concepts

  10. Many-to-One Model Operating System Concepts

  11. One-to-One • Each user-level thread maps to kernel thread. • Examples - Windows 95/98/NT/2000 - OS/2 • Advantages: • Higher level of concurrency • Supports parallel processors • Disadvantages: • Creating a user thread requires creating the corresponding Kernel thread (much overhead) • Must restrict the number of threads to reduce kernel burdens Operating System Concepts

  12. One-to-one Model Operating System Concepts

  13. Many-to-Many Model • Hybrid of Many-to-one and One-to-One models • Allows many user level threads to be mapped to many kernel threads. • Allows the operating system to create a sufficient number of kernel threads. • OS that use Many-to-Many Model: • Solaris 2 • Windows NT/2000 with the ThreadFiber package • IRIX • HP-UX • Tru64UNIX Operating System Concepts

  14. Many-to-Many Model Operating System Concepts

  15. Threading Issues • Semantics of fork() and exec() system calls. • Thread cancellation. • Signal handling • Thread pools • Thread specific data Operating System Concepts

  16. Thread Issues: Semantics of fork() and exec() system calls. • Question: If one thread in a program calls fork, does the new process duplicate all threads or is the new process single-threaded? • Answer: Two versions of fork is provided by some UNIX versions: • One that duplicates all threads • One that duplicates only the thread that invokes the fork • exec typically works similar to a single process environment. That is: if a thread invokes the exec system call, all threads are also created. Operating System Concepts

  17. Thread Issues: Thread cancellation • Thread cancellation is the task of terminating a thread before it has completed. • Example: if multiple threads are concurrently searching through a database and one thread returns result, the remaining threads might be cancelled. Or pressing the stop button on the web browser (often a web page is loaded in a separate thread) • The tread that is to be canalled is referred to as the Target thread. • Two different types of canceling threads: • Asynchronous cancellation: Immediately cancel target thread • Deferred cancellation: target thread periodically checks to see if should be cancelled. Operating System Concepts

  18. Thread cancellation: Problems • Difficulty arises when resources have been allocated to a cancelled thread, or • If a thread was cancelled while in the middle of updating data it is sharing with other threads (problems specially sever with Asynchronous cancellation). • Deferred cancellation allows for better clean up upon termination since it has time to update the required fields. • The safe states to terminate threads sometimes referred to as “Cancellation points” Operating System Concepts

  19. Thread Issues: Signal handling • Signal is used in UNIX to notify a process that a particular event has occurred. Signal may be received Synchronously or Asynchronously. • Regardless of the type of signal, following will take place: • A signal is generated by the occurrences of an event • A generated signal is delivered to a process • Once delivered, the signal must be handled • Synchronous signals are delivered to the same process that performed the operation causing the signal (Example illegal memory access or div by zero) • Asynchronous signals are typically sent to another process (e.g. when CTRL-C is pressed or Time is expired) Operating System Concepts

  20. Thread Issues: Signal handling • Handling signals in single-threaded programs is straightforward, signals are always delivered to a process. • How about handling signals in a multithreaded program, which thread should get it? In general, the following options exit: • Deliver the signal to the tread to which the signal applies (specially true for synchronous signals) • Deliver the signal to every thread in the process • Deliver the signal to certain thread in the process • Assign a specific thread to receive all signals for the process. (Solaris 2 implements this option) • Windows uses Asynchronous Procedure Calls (APCs). The APC facility allows a user thread to specify a function that is to be called when user thread receives an event. Operating System Concepts

  21. Thread issues: Thread pools • Unlimited threads could exhaust system resources, such as CPU time or memory. Consider creating a treat for every action while browsing the web! • Thread pools: Create a number of threads at process start up and place them into a pool, where they sit and wait for work. • When server receives a request, it awakens a thread from this pool, if one available, and passes it the request. • Once thread competes its service, it returns to the pool awaiting for more work. If pool contains no available thread, waits for a free one. • Benefits of thread pooling: • Faster to service a request with existing thread than waiting to create one • A thread pool limits the number OS threads that exist at any one point. Operating System Concepts

  22. Thread issues: Thread-specific Data • Threads belonging to a process share the data of the process. (a great benefit of multithreading) • However, each thread might need its own copy of certain data in some circumstances (I.e. thread-specific data) • Most thread libraries (e.g. Win32 and Pthreads) provide some form of support for thread-specific data. Java provides support as well. Operating System Concepts

  23. Optional reading • The following sections of the text (and corresponding slides) are specific to certain Operating Systems and are NOT Required reading for the course: • Section 5.4  Pthreads • Section 5.5 Solaris 2 Threads • Section 5.6 Windows 200 Threads • Section 5.7 Linux Threads • Section 5.8 Java Threads Operating System Concepts

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

  25. Pthread Example #include <pthread> #include <stdio> Main () { pthread_id tid; pthread_att attr; /* perform whaterver code you are designed to do */ pthread_attr_init(&attr); /* get thread attributes, e.g. stack size, scheduling info */ pthread_create(&tid, &attr, RunFunc, parm1, parm2); /* create a new thread /* perform other tasks */ pthread_join(tid, NULL); /* now wait for the thread to exist */ } RunFunc(Parm1, Parm 2) { /* do whatever you intended to do */ pthread_exit(0); } Operating System Concepts

  26. Solaris 2 Threads Operating System Concepts

  27. Solaris Process Operating System Concepts

  28. Windows 2000 Threads • Implements the one-to-one mapping. • Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area Operating System Concepts

  29. Linux Threads • Linux refers to them as tasks rather than threads. • Thread creation is done through clone() system call. • Clone() allows a child task to share the address space of the parent task (process) Operating System Concepts

  30. Java Threads • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface • Java threads are managed by the JVM. Operating System Concepts

  31. Java Thread States Operating System Concepts

More Related