Operating Operating Systems I (1D (1DT044) Threads (Chapter 4) Tuesday february 1 Uppsala University 2011 karl.marklund@it.uu.se
PCB state In brief, the PCB serves as the repository for any process id/number information that may vary program counter from process to process. registers CPU scheduling information (chapter 5) memory management information (chapter 8) accounting information memory limits I/O status information
Chapter 4: Threads Chapter objectives: ★ To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems. ★ To discuss the APIs for the Pthreads, Win32, and Java thread libraries. ★ To examine issues related to multithreaded programming. The text book used in the course
4.1) Single and Multithreaded Processes
4.1.1) Multithreaded Server Architecture An example of how an server can use threads. One thread listens for client requests. When a request is made, the server creates a new thread to service the request and resume listening fro additional requests. Why not fork a new process for each request? Creating a new process is time consuming and resource intensive compared to creating a new thread.
4.1.2) Benefits What’s so great about threads? Multithreading an interactive application may allow a program to continue running Responsiveness even if part of it is blocking or performing a lengthy operation, thereby increasing responsiveness to the user. Processes may only share resources through techniques such as shared memory or Resource Sharing message passing. Threads (by default) share memory and resources which allows an application to have several threads of execution in the same address space. Allocating memory and resources for process creation is costly. Because threads Economy share the resources of the process to which they belong, it is (at least in theory) more economical to create and context-switch threads. Scalability The benefits of multithreading can be greatly increased in a multiprocessor architecture, where threads may be running in parallel on different processors.
4.1.3) Single-Core CPUs The core is the part of the processor that actually performs the reading and executing of instructions. Processors were originally developed with only one core . A Single-core processor can process only one instruction at a time. To improve efficiency, processors commonly utilize pipelines internally, which allow several instructions to be processed together; however, they are still consumed into the pipeline one at a time
4.1.3) Multi-Core CPUs A multi-core processor is composed of two or more independent cores. One can describe it as an integrated circuit which has two or more individual processors (called cores in this sense).
4.1.3) Multicore Programming Multi-core give us more computing power - do programmers utilize this? Multicore systems putting pressure on programmers... Challenges: ★ Dividing activities ★ Balance ★ Data splitting ★ Data dependency ★ Testing and debugging intel quad core
4.1.3) Multicore Programming - challenges Dividing activities Finding areas in an application that can be divided into separate, concurrent tasks and thus can run in parallel on individual cores. If tasks can be found to run in parallel, programmers also must Balance ensure the tasks to perform equal work of equal value. Using a separate execution core for a task that don’t contribute much value to the overall process may not be worth the cost. Just as applications are divided into separate tasks, the data Data splitting accesses and manipulated by the tasks must be divided to run on separate cores. Data dependency The data accessed by the tasks must be examined for dependencies between two or more tasks. In instances where one task depends on data from another, programmers must ensure that the execution of the tasks is synchronized to accommodate the data dependency. We examine such strategies in Chapter 6. Testing and debugging When a program is running in parallel on multiple cores, there are many different execution paths. Testing and debugging such concurrent programs is inherently more difficult than testing and debugging single-threaded applications.
4.1.3) Multicore Programming - debugging and execution paths If the threads interleave in this order, thread 2 will withdraw money from the account although there are not enough money in the account at the time of the withdraw. You have a data race in your program!
4.1.3) Multicore Programming - debugging and execution paths If the threads interleave in this order, thread 2 will detect that there are not enough money in the account at the time of the withdraw. You have a data race in your program! Depending on the interleaving of the threads, the program will work as expected sometimes and not as expected other times.
4.1.2 & 4.1.3) Concurrent Execution of Threads ”Concurrent” execution of threads on a single core CPU Concurrent execution of threads on a dual core CPU
4.3) Thread Libraries A Thread library provides programmers with an API for creating and managing threads. Two primary ways of implementing a thread library exists: User Threads Library entirely in user space Three primary thread libraries: User Mode ★ POSIX Pthreads How are user level threads implemented? ★ Win32 threads mode bit = 1 ★ Java threads How does user level threads relate to kernel threads? Kernel Mode Kernel Threads Kernel-level library supported by the OS mode bit = 0 Examples: ★ Tru64 UNIX ★ Windows XP/2000 ★ Mac OS X ★ Solaris ★ Linux
4.2) Multithreading Models Many-to-One One-to-One Many-to-Many Two-level-model Many user-level Each user-level Allows many user Similar to many-to- threads mapped to thread maps to one level threads to be many, except that it a single kernel kernel thread. mapped to many allows a user thread thread. kernel threads. to be bound to a kernel thread. Thread Allows the management is operating system to done by the thread create a sufficient library in user number of kernel space. threads.
4.2) Multithreading Models Many-to-One One-to-One Many-to-Many Two-level-model Thread management Provides more Multiplexes many Similar to many-to- is done by the thread concurrency than the user-level threads to a many, except that it library in user space many-to-one model by smaller or equal allows a user thread but entire process will allowing another number of kernel to be bound to a block if a thread thread to run when a threads. kernel thread. makes a blocking thread makes a system call. blocking system call. A compromise between man ý -to-one Only one thread can Allows for threads to and one-to-one. access the kernel at a run in parallel on time, hence threads multiprocessors. This models makes thread creation ”expensive” and cannot run in parallel ! most implementations restrict the number of threads on multiprocessors. supported by the system.
4.2) Multithreading Models Many-to-One One-to-One Many-to-Many Two-level-model Examples: Examples: Examples: Examples: ★ Solaris Green ★ Windows NT/ ★ Solaris prior to ★ IRIX Threads XP/2000 version 9 ★ HP-UX ★ GNU Portable ★ Linux ★ Windows NT/ ★ Tru64 UNIX Threads 2000 with the ★ Solaris 9 and ★ Solaris 8 and ThreadFiber later earlier package
4.3.1) Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. May be provided either as user-level or kernel-level. API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX operating systems (Solaris, Linux, Mac OS X).
4.3.3) Java Threads Java threads are managed by the JVM. Typically implemented using the threads model provided by underlying OS. Java threads may be created by: ★ Extending Thread class. ★ Implementing the Runnable interface.
4.4) Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread ★ Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations
4.4.2) Thread Cancellation Often, a web page is loaded using several threads - each image is loaded in a separate thread etc. If a user presses the stop button in the browser, how can we cancel all the threads? Two general approaches: ★ Asynchronous cancellation terminates the target thread immediately. ★ Deferred cancellation allows the target thread to periodically check if it should be cancelled.
4.4.2) Thread Cancellation Hmm... Do we need to be careful when cancel threads? The difficulty with cancellation occurs in situations where resources have been allocated to a canceled thread or where a thread is canceled while in the midst of updating data it is sharing with other threads. ★ Asynchronous cancellation terminates the target thread immediately. Often the OS will reclaim system resources from a canceled thread but will not reclaim all resources. Therefore, canceling a thread a asynchronous may not free a necessary system-wide resource. ★ Deferred cancellation allows the target thread to periodically check if it should be cancelled. The thread can perform the check for cancelation at a point at which it can be canceled safely. Pthreads refers to such poitns as cancellation points .
Recommend
More recommend