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
Security-focused operating system wikipedia , lookup
Spring (operating system) wikipedia , lookup
Library (computing) wikipedia , lookup
Unix security wikipedia , lookup
Burroughs MCP wikipedia , lookup
Distributed 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) Analogy • Active Entity • Works simultaneously with others (many threads executing) • Requires coordination (if many parts of process are executing in parallel, then coordination among resources is required) • Resources: I/O devices,CPU etc Process VS Thread • Process are represented in two ways: • In RAM ? Physical representation.. • In Execution context ? CPU registers.. • OS represents all this information by using PCB • Separate PCB for every process.. • PCB in case of multi-threading.. Threads within a same process share… • Multiple and concurrent threads within a process share: Code Threads are in the same process address space Data 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 • 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 (lower main memory requirement) Economy - it is more economical to create and context-switch threads (because they share resources of the process to which they belong) Parallelization (computation speedup) Specialization (repeatedly executing same code) IPC not required! Scalability (utilizing multiprocessor architectures) Some other things.. 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. Any benefit to OS ? OS can assist multiple threads if it’s multithreaded Making OS multithreaded is helpful because they can support multiple apps at same time and execute them on different CPUs. Benefits of multithreading (Single CPU).. Are threads useful in Single CPU architecture or when num of threads > num of CPU ? If t_idle (disk reading e.g.) > t_cxt_switch time, then it makes sense that switching among threads is useful t_cxt_switch < p_cxt_switch Benefits of multithreading (Multi CPU system).. 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 Basic thread mechanism • How to support threads? • Must have data structure that allows to distinguish between process and thread Goals • To identify threads and keep record of them like PCB • Mechanisms to create and manage threads • Mechanisms to allow threads to communicate with eachother Thread Creation • Thread type data structure that contains all information specific to that thread e.g. Program counter, registers, stack etc • Fork (proc,args) {new type thread data structure is created} • Join(thread) {parent thread blocked until all executed in case of bunch of threads processing on some input array} How multithreading is implemented ? (with respect to benefits to applications and OS) • OS level threads are visible to OS scheduler • OS scheduler decides the execution/mapping of these OS level threads • Some of the OS (kernel) level threads are linked to user level threads (in a process) and some are made to run their own code. • For a user level thread to be executed, it must be RECOGNIZED (mapped to a kernel level thread and then OS scheduler will schedule it on underlying hardware). Multithreading Models • Depending on this concept, we can have three types of multithreading models: A. One to One model: (for each user level thread, one kernel level thread) One-to-One Model Each user-level thread maps to kernel thread OS sees/understands that app is multithreaded Benefits Since OS already provide mechanisms to support its threads, user threads can directly benefit from this. True concurrency - Another thread will be able to run when a thread makes a blocking system call. Drawback: Creating a user thread requires creating a corresponding kernel thread. (Overhead) Portability issues: User level threads is relying support from kernel level thread management so an app running in one environment may not be able to run on other Many to one Model • Many user level threads mapped to one OS level thread • There’s a thread management library on user level that • • • • decides which user level thread should be mapped to THAT ONE OS level thread. That user level thread will run only when its scheduled by OS scheduler Totally portable (everything done at user level, no need of support from OS level) OS doesn’t know whether the app is multithreaded or not, it just sees kernel level thread Whole process can get blocked ! 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 • Allow hybrid multithreading ! Difference between the two techniques… Advantages of User level threads (process scope) 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) Process wide thread management (only limited to one process) 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 (system scope) 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 If a process has large number of threads, it will receive large fraction of underlying hardware CPU. Disadvantages Frequent invoking of kernel for management of 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 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 Examples of threading Libraries Pthreads Win32 Java 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 May be provided either as user-level or kernel-level. Common in UNIX operating systems (Solaris, Linux, Mac OS X) Threading Issues Thread cancellation 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