* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Chapter 4
Survey
Document related concepts
Berkeley Software Distribution wikipedia , lookup
Plan 9 from Bell Labs wikipedia , lookup
Copland (operating system) wikipedia , lookup
Mobile operating system wikipedia , lookup
Burroughs MCP wikipedia , lookup
Unix security wikipedia , lookup
Distributed operating system wikipedia , lookup
Spring (operating system) wikipedia , lookup
Library (computing) wikipedia , lookup
Process management (computing) wikipedia , lookup
Transcript
Chapter 4: Threads Overview Multithreading Models Thread Libraries Pthreads Windows Thread Library Java Threads Threading Issues Implicit Threading Open MP Kernel level threads Windows threads Linux threads Operating System Concepts 4.1 Silberschatz, Galvin and Gagne ©2005 Single and Multithreaded Processes Operating System Concepts 4.2 Silberschatz, Galvin and Gagne ©2005 Benefits Responsiveness: Multithreading an interactive application increases responsiveness to the user. Resource Sharing: Threads share memory and resources of the processes to which they belong. Economy: Process creation is expensive; In Solaris 2, creating a process is 30 times slower than creating a thread and context switching is five times slower Utilization of MP Architectures: Benefits of multithreading increase in multiprocessor systems because different threads can be scheduled to different processors Operating System Concepts 4.3 Silberschatz, Galvin and Gagne ©2005 Multicore programming Multithreaded programming provides a mechanism for more efficient use of the multiple cores in current day desktops. Concurrent systems: A concurrent system supports more than one task by allowing all the tasks to make progress Parallel systems: A parallel system allows more than one task to be executing simultaneously. So, if a system is parallel it has to be multiprocessor system. Programming challenges in multicore systems: Operating system designers must write scheduling algorithms that use multiple cores to allow parallel execution. Application programmers need to modify existing programs as well as design new multithreaded programs to exploit the multiple cores. Operating System Concepts 4.4 Silberschatz, Galvin and Gagne ©2005 Multithreading Models Many systems provide support for both user-level and kernel level threads. This provides different multithreading models Many-to-One: maps many user level threads into one kernel level thread. Thread creation, synchronization done in user space – Fast, no system call required, portable. The entire process will block if a thread makes a blocking system call Since only one thread can access kernel at a time, multiple threads cannot run concurrently and thus cannot make use of multiprocessors One-to-One: maps each user level thread to a kernel level thread Thread creation, synchronization require system calls and hence expensive Creating a user level thread results in creating a kernel thread More overhead but allows parallelism Because of the overhead most implementations limit the number of kernel threads created Used in Linux, Windows NT, Windows 2000, etc. Many-to-Many: Multiplexes many user level threads to a smaller or equal number of kernel threads Has the advantages of both the many-to-one and one-to-one model A user level thread is not bound to any particular kernel level thread. Solaris2, IRIS, HP-UX takes this approach Operating System Concepts 4.5 Silberschatz, Galvin and Gagne ©2005 Many-to-One Model Operating System Concepts 4.6 Silberschatz, Galvin and Gagne ©2005 One-to-one Model Operating System Concepts 4.7 Silberschatz, Galvin and Gagne ©2005 Many-to-Many Model Operating System Concepts 4.8 Silberschatz, Galvin and Gagne ©2005 Thread Libraries A thread library provides the programmer with an API for creating and managing threads. User level threads: Thread library lies entirely in the users’ space with no kernel support. This means, invoking a function in the library results in a local function call in the user space, and not a system call. Kernel-level threads: In this case, code and data structures for the library exist in the kernel space. Three main thread libraries available today are: POSIX Pthreads – visit https://computing.llnl.gov/tutorials/pthreads. Windows Threads - visit mdsn.microsoft.com Java Threads. - docs.oracle.com/en/java Operating System Concepts 4.9 Silberschatz, Galvin and Gagne ©2005 Pthreads – Programming for Multi-core PCs 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 (Solaris, Linux, Mac OS X) Refer to the link on “Posix Thread Programming” in the class website for all the pthread library routines and how to use them. Compiling and linking program fun.c with pthread library Use the command “gcc –o fun –pthread fun.c” Operating System Concepts 4.10 Silberschatz, Galvin and Gagne ©2005 An example C Program using Pthreads API. #include <pthread.h> #include <stdio.h> Int sum; void *runner(void *param) { int i; upper=atoi(param); sum=0; for (i=1; i<=upper; i++) sum +=i; pthread_exit(0); } Operating System Concepts Int main(int argc, char *argv[]) { pthread_t tid; //thread identifier pthread_attr_t attr; // set of thread attributes if (argc != 2){ fprintf(stderr, “usage: a.out <integer>\n”); return -1; } if (atoi(argv[1]) < 0){ fprintf (stderr, “%d must be >=0\n”, atoi(argv[1])); return -1; } /*get default attributes*/ pthread_attr_init(&attr); /*create a thread*/ pthread_create(&tid,&attr, runner, argv[1]); /*wait for the thread to exit */ pthread_join(tid,NULL); printf(“sum=%d”, sum); } 4.11 Silberschatz, Galvin and Gagne ©2005 Thread Libraries … Windows Thread Library Similar to Pthreads library in many ways. Read the example in the book or visit mdsn.microsoft.com for learning more about windows threads. Java Threads Java language and its API provide a rich set of features for creating and managing threads. Java threads are available on any system that provides a JVM including Windows, Linux and Mac OS X. Java threads API is also available for Android applications. Operating System Concepts 4.12 Silberschatz, Galvin and Gagne ©2005 Implicit threading Implicit threading libraries take care of much of the work needed to create, manage, and (to some extent) synchronize threads. Two such libraries are : (i) OpenMP and (ii) Intel Thread building blocks OpenMP: is a set of compiler directives, library routines, and environment variables that specify shared memory concurrency in FORTRAN, C and C++ programs The rationale behind OpenMP was to create a portable and unified standard for shared memory parallelism. OpenMP was introduced in November 1997 and its version 4.0 has been released in July 2013. All major compilers support OpenMP language. This includes Microsoft visual C/C++ .NET for Windows, and GNU GCC compiler for Linux Operating System Concepts 4.13 Silberschatz, Galvin and Gagne ©2005 OpenMP OpenMP compiler directives demarcate code that can be executed in parallel (called “parallel regions”) and control how code is assigned to threads. The threads in an OpenMP code operate under the fork-join model. When the main thread encounters a parallel region while executing the application, a team of threads is forked off, and these threads begin executing the code within the parallel region. At the end of the parallel region, the threads within the team wait until all other threads in the team have finished before being joined. The main thread resumes serial execution with the statement following the parallel region. Operating System Concepts 4.14 Silberschatz, Galvin and Gagne ©2005 Pragmas For C/C++ , OpenMP uses “pragmas” as compiler directives All OpenMP pragmas have the same prefix of “ #pragma omp”. This is followed by an OpenMP construct and one or more optional clauses to modify the construct. For example, to define a parallel region within an application, use the parallel construct as in #pragma omp parallel This pragma will be followed by a single statement or a block of code enclosed within curly braces. When the application encounters this statement during execution, it will create a team of threads, execute all the statements within the parallel region on each thread and join the threads after the last statement in the region. Operating System Concepts 4.15 Silberschatz, Galvin and Gagne ©2005 Pragmas… In many applications, large number of independent operations are found in loops. Using the loop work sharing construct in OpenMP, you can split these loop iterations and assign them to threads for concurrent execution. The “parallel for” construct will initiate a new parallel region around the single for loop following the pragma and divide the loop iterations among the threads of the team. Upon completion of the assigned iterations, threads sit at the implicit barrier at the end of the parallel region waiting to join with other threads. For More information on OpenMP, visit openmp.org Operating System Concepts 4.16 Silberschatz, Galvin and Gagne ©2005 Threading Issues Semantics of fork() and exec() system calls. In a multithreaded program, when a fork() system call is executed by a thread, does the new process duplicate all the threads or is the new process single threaded. The exec () system call works the same way we saw earlier – i.e, it replaces the the entire process including all threads Thread cancellation: deals with termination of a thread before it has completed. Depends on the implementation E.g. If a database search is performed by several threads concurrently, all threads can be terminated once one thread returns the result. Cancellation of a thread can occur in two different scenarios: Asynchronous cancellation: One thread immediately terminates the target thread Deferred cancellation: The target thread can periodically check if it should terminate, allowing it to terminate itself in an orderly fashion. Most Operating Systems allow a process or thread to be cancelled asynchronously Operating System Concepts 4.17 Silberschatz, Galvin and Gagne ©2005 Threading Issues… Signal handling: Signal is used in UNIX systems to notify a process a particular event has occurred. Signals follow the following pattern: A signal is generated by the occurrence of a particular event A generated signal is delivered to a process. Once delivered, the signal must be handled Synchronous signals: e.g. signals delivered to the same process that performed the operation causing the signal. For example, division by zero Asynchronous signals: signals generated by an event external to the process are received by the process asynchronously. For example, <control> <C>. Signal handlers: Default signal handlers provided by the OS kernel User-defined signal handlers Issues in handling signals: In single-threaded programs, signals are always delivered to the process. In multithreaded programs who should be delivered the signal? Thread specific data Operating System Concepts 4.18 Silberschatz, Galvin and Gagne ©2005 Threading Issues… Thread pools Reduce the overhead involved in thread creation Instead of creating a thread for each service request, a set of threads are created by the server and placed in a pool When a request for a service arrives, the service is passed to one of the threads waiting in the pool. After the request is serviced, the thread is returned to the pool awaiting more work If a request arrives and there are no threads awaiting in the queue, the server waits until a thread becomes free Benefits of thread pooling: Operating System Concepts Faster servicing of the requests because no overhead involved in thread creation Limits the total number of threads that exist at any one point in the system 4.19 Silberschatz, Galvin and Gagne ©2005 Scheduler Activations Many-to-many, many-to-one and one-to-one models discussed earlier require communication between the kernel and the thread library to maintain the appropriate number of kernel threads allocated to the application to help ensure best performance. Many systems accomplish this communication through scheduler activation. Scheduler activation provides upcalls - a communication mechanism from the kernel to the thread library Operating System Concepts 4.20 Silberschatz, Galvin and Gagne ©2005 Windows XP Threads Implements the one-to-one mapping, as I mentioned earlier Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) Operating System Concepts 4.21 Silberschatz, Galvin and Gagne ©2005 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 4.22 Silberschatz, Galvin and Gagne ©2005