Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Copland (operating system) wikipedia , lookup
Unix security wikipedia , lookup
Library (computing) wikipedia , lookup
Spring (operating system) wikipedia , lookup
Burroughs MCP wikipedia , lookup
Distributed operating system wikipedia , lookup
Security-focused operating system wikipedia , lookup
OPERATING SYSTEM CONCEPTS Lecture Contents Motivation for threads Thread Concept Similarity and Difference between Process and Thread Advantages of threads Implementation of Multithreading Multithreading Models Thread Library Concept Multithreading Issues Case Study *Please follow Silberchatz, Galvin and Gagne (Chapter 4), 7th edition Issues with Processes • So far we have discussed Process, Creation of new process concept and its execution; However there are some issues with processes. Processes are EXPENSIVE ! (E.g. resources that are to be allocated with every process) Hardware resources (memory), kernel resources (PCB) Link establishment for processes/related processes) IPC (unrelated Concurrency Motivation ? .. A mechanism that provides an alternative of a process (so that a new process is not needed) THREAD Thread- A piece of code in a process which runs concurrently with execution of other piece of codes within the same process (same address space) No need to create a new process • Resources that thread need to update/allocate privately: Thread ID CPU Context (Program Counter (PC), register set) Stack Priority Kernel maintained external variable errno, (in order to tell the programmer that an error has occurred) Threads within a same process share… • Multiple and concurrent threads within a process share: Code Threads are in the same process address space Data Open File descriptor table PCB (Process Control Block) For a thread creation, only need to allocate CPU context and Stack !! Single and Multithreaded Processes How processes are similar to threads or different ? • A thread can be in a state, similar to process (new, ready, running, waiting, terminate) • Threads can also create other threads. • Difference • Processes execute only within their own address space, whereas threads of same process execute within address space of one major process • Automatic protection mechanism for processes but what about threads… ? • Synchronization issues ? Advantages of thread Responsiveness - allow a program to continue running even if part of it is blocked (e.g. In a browser, multiple threads are working simultaneously to increase responsiveness) Resource sharing - threads share the memory and resources of the process (allows an application to have several threads of activity within the same address space) Economy - it is more economical to create and context-switch threads (because they share resources of the process to which they belong) In Solaris a process is thirty times slower to create and five times slower to context-switch Scheduling a thread is easy (just save the CPU context) Scalability (utilizing multiprocessor architectures) Some other things.. The main reason for having threads is that in many applications, multiple activities are going on at once. Some of these may block from time to time. By decomposing such an application into multiple sequential threads that run in parallel, the programming model becomes simpler. How multithreading is implemented ? There are two techniques for implementing multithreading User-Level threads Thread creation and management is done by the application using a thread library Any application can be programmed to be multithreaded by using a thread library which is a package of application level functions. The kernel is not aware of existence of threads Kernel Level threads Thread management and implementation is done by operating system Kernel provides a way for user processes to create multiple threads Difference between the two techniques… Advantages of User level threads User-level threads can be created in a system that doesn’t provide multithreading facility Management and scheduling of thread is done by threading library in user space, so user level threads don’t invoke kernel for doing these tasks User level threads can run on any OS, no changes are required to do on underlying kernel (Portability) Disadvantages Using User Level threading strategy, a multithreaded application cannot take advantage of multiprocessor system. (different threads of a same process cannot run on different CPU’s because OS considers a process a single threaded program) If a thread is blocked, all the threads within a process gets blocked. Advantages of Kernel level threads Since kernel threads use the kernel scheduler, different kernel threads can run on different CPUs (taking advantage of multiprocessor system) If a thread within a process gets blocked (e.g. waiting for an I/O), the scheduler can schedule another thread Disadvantages Frequent invoking of kernel for management of threads More expensive to create than user threads Examples User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Multithreading Models Based on the number of kernel threads allocated correspondingly to the user level threads, we have three multithreading models Many-to-one Model One-to-one Model Many-to-Many Model Many-to-One Model Many user-level threads mapped to single kernel thread Thread management is done by the thread library in user space, so it is efficient. Drawback - the entire process will block if a thread makes a blocking system call. Examples: Solaris Green Threads GNU Portable Threads One-to-One Model Each user-level thread maps to kernel thread True concurrency - compared to many-to-one model since another thread will be able to run when a thread makes a blocking system call. Allows multiple threads to run in parallel on multiprocessors. Drawback: Creating a user thread requires creating a corresponding kernel thread. (Overhead) Examples Windows NT/XP/2000 Linux Solaris 9 and later Many-to-Many Model (Hybrid Model) Multiple user level threads multiplexed over a smaller or equal number of kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris 2 Windows NT/2000 with the ThreadFiber package Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS -> invoking a function in the library results with a system call Pthreads 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 (i.e. the OS designers) This standard has been defined to make the writing of PORTABLE threaded program possible This standard defines over 60 function calls May be provided either as user-level or kernel-level. Common in UNIX operating systems (Solaris, Linux, Mac OS X) Threading Issues Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations Thread Cancellation Terminating a thread before it has finished Example: multiple threads concurrently searching through a database and one thread returns the result Stopping a webpage from loading any further Two general approaches: Asynchronous cancellation terminates the target thread immediately – troublesome if it’s in middle of updating some data Deferred cancellation allows the target thread to periodically check if it should be cancelled – checking a flag. Can only be cancelled at defined points called cancellation points Signal Handling • • • • Signals are used in UNIX systems to notify a process that a particular event has occurred Example: illegal memory access, division by zero A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled In a multithreaded process, where should a signal be delivered? 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 Thread Pools Reason for the need of thread pool: Time consumed to create a new thread and destroy the previous ones – leads to slow performance Unlimited threads could exhaust system resources, such as CPU time or memory Solution: Thread Pools • Create a number of threads at process startup and place them in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Also allows an application to specify the number of threads it requires Thread Specific Data Allows each thread to have its own copy of specific data Example, in a transaction-processing system, service each transaction in a separate thread. Furthermore, each transaction might be assigned a unique identifier. To associate each thread with its unique identifier, use thread-specific data. Scheduler activation User-Level Threads: If a single user-level thread blocks, the operating system blocks the entire multithreaded process. Another limitation to a many-to-one thread mapping is that a process's threads cannot simultaneously execute on multiple processors. Scheduler activations attempt to address these limitations to userlevel threads. A scheduler activation is a kernel thread that can notify a userlevel threading library of events (e.g., a thread has blocked or a processor is available). This type of kernel thread is called a "scheduler activation,“ because the user-level threading library can perform thread-scheduling operations when "activated" by an event notification, sometimes called an upcall. Case Study • Windows XP • Linux Windows XP Windows XP Threads Linux Threads • Linux refers to them as tasks rather than threads • Thread creation is done through clone() system call • clone() accepts arguments that specify which resources to share with the child process