1 / 30

Operating Systems

Operating Systems. Threads. Overview Multithreading Models Threading Issues. Overview. A thread is a basic unit of CPU utilization A traditional (or heavyweight) process has a single thread of control Single-threaded applications Simple implementations e.g. assignments

tieve
Télécharger la présentation

Operating Systems

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. Operating Systems NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk

  2. Threads • Overview • Multithreading Models • Threading Issues

  3. Overview • A thread is a basic unit of CPU utilization • A traditional (or heavyweight) process has a single thread of control • Single-threaded applications • Simple implementations e.g. assignments • Multi-threaded applications • Web browser • Web server

  4. Single and Multithreaded Processes ThreadID Program counter Registers Stack

  5. Relationship between threads and processes • The operating system creates a process for the purpose of running a program. • Every process has at least one thread. • On some operating systems, a process can have more than one thread. • Some programs like word processors are designed to have only one instance of themselves running at the same time. • Sometimes, such programs just open up more windows to accommodate multiple simultaneous use. • After all, you can go back and forth between five documents, but you can only edit one of them at a given instance. • Command line interpreters and multiple users ~ processes • Access rights • Protection of other users from failures • Graphical User Interface ~ threads • Multiple aspects at one instance, user input and painting

  6. Processes and Threads • The concept of a process and thread are interrelated by a sense of ownership and of containment • Process • A process is the "heaviest" unit of kernel scheduling • Processes own resources allocated by the operating system • Resources include memory, • file handles, • sockets, • device handles, and user interfaces. • Processes do not share address spaces or file resources except through explicit methods • Processes are typically pre-emptively multitasked. • However, Windows 3.1 and older versions of Mac OS used co-operative or non-preemptive multitasking.

  7. Processes and Threads • Thread • A thread is the "lightest" unit of kernel scheduling. • At least one thread exists within each process. • If multiple threads can exist within a process, then they share the same memory and file resources. • Threads are pre-emptively multitasked if the operating system's process scheduler is pre-emptive. • Threads do not own resources except for a stack and a copy of the registers including the program counter.

  8. Benefits • Responsiveness • blocking and non-blocking requests • Resource Sharing • same address space • Economy • Allocating resources to processes as compared to sharing resources via threads • Utilization of MP Architectures • Multi-threaded applications can utilize multiprocessor platforms by enhancing concurrency

  9. Multi-threading and reality • In an ideal world, five processors would do five times the work of one processor • But we live in a world of contention for shared resources, of disk and memory bottlenecks, single-threaded applications, multithreaded applications that require synchronous processing, and poorly coordinated processors

  10. Kernel threads User level threads Types of threads NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk

  11. Kernel threads • A kernel thread is a kernel entity, like processes and interrupt handlers • It is the entity handled by the system scheduler. • Kernel threads consist of a set of registers, a stack, and a few corresponding kernel data structures. • The user structure contains process-related information • The uthread structure contains thread-related information. • Unlike processes, all threads within a process share the same address space. • Similar to processes, when a kernel thread makes a blocking call, only that thread blocks. • All modern machines support kernel threads, most often via the POSIX threads interface ``pthreads''. • Some dedicated parallel machines support kernel threads poorly or not at all. For example, the Blue Gene/L microkernel does not support pthreads.

  12. Kernel threads • The advantage of kernel threads over processes is faster creation and context switching compared with processes. • Parallel programming • Kernel threads cannot be accessed from the user mode environment, except through the threads library.

  13. Kernel Threads • Supported by the Kernel • Examples • Windows XP/2000 • Solaris • Linux • Tru64 UNIX • Mac OS X

  14. User level threads • Like a kernel thread, a user-level thread includes a set of registers and a stack, and shares the entire address space with the other threads in the enclosing process • Unlike a kernel thread, however, a user-level thread is handled entirely in user code • OS is unaware of a user-level thread's existence • The primary advantages of user-level threads are efficiency and flexibility • Because the operating system is not involved, user-level threads can be made to use very little memory • User-level threads are also more flexible because the thread scheduler is in user code, • for example, the application's priority structure can be directly used by the thread scheduler

  15. User level threads • The primary disadvantage of user-level threads compared to kernel threads is the lack of operating system support. • For example, when a user-level thread makes a blocking call, the kernel does not start running another user-level thread. Instead, the kernel suspends the entire calling kernel thread or process, even though another user-level thread might be ready to run.

  16. User Threads • User threads are mapped to kernel threads by the threads library, in an implementation dependent manner. • The threads library uses a proprietary interface to handle kernel threads. • Three primary thread libraries: • POSIX Pthreads • Win32 threads • Java threads

  17. Questions? NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk

  18. Multithreading Models • Many-to-One • One-to-One • Many-to-Many

  19. Many user-level threads mapped to single kernel thread Entire process will block if a thread makes a blocking call Examples: Solaris Green Threads GNU Portable Threads Many-to-One

  20. One-to-One • Each user-level thread maps to kernel thread • Examples • Windows NT/XP/2000 • Linux • Solaris 9 and later

  21. Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads • Allows the operating system to create a sufficient number of kernel threads • Solaris prior to version 9 • Windows NT/2000 with the ThreadFiber package

  22. Many-to-Many Model

  23. Two-level Model • Similar to M:M, except that it allows a user thread to be bound to kernel thread • Examples • IRIX • HP-UX • Tru64 UNIX • Solaris 8 and earlier

  24. Two-level Model

  25. Threading Issues • Semantics of fork() and exec() system calls • Thread cancellation • Signal handling • Thread pools • Thread specific data • Scheduler activations

  26. Semantics of fork() and exec() • Does fork() duplicate only the calling thread or all threads?

  27. Thread Cancellation • Terminating a thread before it has finished • E.g. Multiple threads searching in a DB, if one thread finds the result, cancel the others • Two general approaches: • Asynchronous cancellation terminates the target thread immediately • Cancellation during data update ~ consistency issues • Deferred cancellation allows the target thread to periodically check if it should be cancelled • Cancellation points

  28. Signal Handling • Signals are used in UNIX systems to notify a process that a particular event has occurred • Synchronous ~ same process, divide by zero • Asynchronous ~ external event, terminate process • A signal handler is used to process signals • Signal is generated by particular event • Signal is delivered to a process • Signal is handled • Handlers • Default • User defined • Options • Deliver the signal to the thread to which the signal applies • Deliver the signal to every thread in the process • Deliver the signal to certain threads in the process • Assign a specific thread to receive all signals for the process

  29. Thread Pools • Create a number of threads in a pool where they await work • Web server • Advantages: • Usually slightly faster to service a request with an existing thread than create a new thread • Allows the number of threads in the application(s) to be bound to the size of the pool

  30. Recommended Reading: Book ~ 143 - 146 OSRC http://www.nondot.org/sabre/os/articles Reading list @ http://www.niit.edu.pk/~umarkalim/courses/fall2006/os.html Questions? NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk

More Related