Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 32 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
32
Dung lượng
747 KB
Nội dung
Chapter Threads 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 Pthreads, Win32, and Java thread libraries • To examine issues related to multithreaded programming 6.1 Overview • A thread is a basic unit of CPU utilization • Thread comprises – A thread ID – A program counter – A register set – A stack • Thread shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files and signals • A traditional (or heavyweight) process has a single thread of control • If a process has multiple threads of control, it can perform more than one task at a time Figure 6.1 Single-threaded and multithreaded processes 6.1.1 Motivation • Many software packages that run on modern desktop PCs are multithreaded • An application typically is implemented as a separate process with several threads of control Example: A web browser might have one thread display images or text while another thread retrieves data from the network • Many operating system kernels are now multithreaded; several threads operate in the kernel, and each thread performs a specific task, such as managing devices or interrupt handling 6.1.2 Benefits • Responsiveness Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user • Resource sharing.The benefit of sharing code and data is that it allows an application to have several different threads of activity within the same address space • Economy Because threads share resources of the process to which they belong, it is more economical to create and context-switch threads • Utilization of multiprocessor architectures In a multiprocessor architecture, where threads may be running in parallel on different processors A singlethreaded process can only run on one CPU Multithreading on a multi-CPU machine increases concurrency 6.2 Multithreading Models • Threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads • User threads are supported above the kernel and are managed without kernel support • Kernel threads are supported and managed directly by the operating system • There must exist a relationship between user threads and kernel threads 6.2.1 Many-to-One Model • Maps many user-level threads to one kernel thread • Thread management is done by the thread library in user space, so it is efficient • But the entire process will block if a thread makes a blocking system call • Because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors • Example Green threads—a thread library available for Solaris—uses this model, as does GNU Portable Threads Figure 6.2 Many-to-one model 6.2.2 One-to-One Model • Maps each user thread to a kernel thread • To provides more concurrency than the many-toone model by allowing another thread to run when a thread makes a blocking system call • To allows multiple threads to run in parallel on multiprocessors • Most implementations of this model restrict the number of threads supported by the system • Example Linux, along with the family of Windows operating systems-including Windows 95, 98, NT, 2000, and XP-implement the one-toone model 6.3.1 Pthreads • Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization • Pthreads, may be provided as either a user- or kernel-level library • Numerous systems implement the Pthreads specification, including Solaris, Linux, Mac OS X, and Tru64 UNIX • Example: design a multithreaded program that performs the summation of a nonn negative integer in a separate thread sum i i 0 6.3.2 Win32 Threads • The technique for creating threads using the Win32 thread library is similar to the Pthreads technique in several ways • Notice – Include the windows h header file – Using the CreateThread() function – The DWORD data type is an unsigned 32-bit integer – Win32 defines as LPVOID: a pointer to a void n • Example: sum i i 0 6.3.3 Java Threads • Java threads are managed by the JVM (Java Virtual Machine) • All Java programs comprise at least a single thread of control • There are two techniques for creating threads in a Java program – One approach is to create a new class that is derived from the Thread class and to override its run() method – An alternative-and more commonly used technique is to define a class that implements the Runnable interface • Creating a Thread object does not specifically create the new thread; rather, it is the start() method that actually creates the new thread Calling the start() method for the new object does two things: − It allocates memory and initializes a new thread in the JVM − It calls the run () method, making the thread eligible to be run by the JVM • The Runnable interface is defined as follows: public interface Runnable { public abstract void run(); } • When a class implements Runnable, it must define a run() method The code implementing the run() method is what runs as a separate thread n • Example: sum i i 0 • Example: class Worker1 extends Thread { public void run() { System.out.println("I Am a Worker Thread"); } } public class ThreadTester { public static void main(String args[]) { Worker1 runner = new Worker1(); runner.start(); System.out.println("I Am The Main Thread"); } } 6.4 Threading Issues 6.4.1 The fork() and exec() System Calls • A new process is created by the fork() system call (See Figure 3.10 –page 92) • The new process consists of a copy of the address space of the original process This mechanism allows the parent process to communicate easily with its child process • Both processes (the parent and the child) continue execution at the instruction after the fork(), with one difference: The return code for the fork() is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned to the parent • The semantics of the fork() and exec() system calls change in a multithreaded program • If one thread in a program calls fork(), UNIX systems have two versions of fork() – one that duplicates all threads – another that duplicates only the thread that invoked the fork() system call • If a thread invokes the exec() system call, the program specified in the parameter to exec() will replace the entire process-including all threads 6.4.2 Cancellation • Thread cancellation is the task of terminating a thread before it has completed • A thread that is to be canceled is often referred to as the target thread Cancellation of a target thread may occur in two different scenarios − Asynchronous cancellation One thread immediately terminates the target thread − Deferred cancellation The target thread periodically checks whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion • 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 • Often, With asynchronous cancellation, the operating system will reclaim system resources from a canceled thread but will not reclaim all resources • Therefore, canceling a thread asynchronously may not free a necessary system-wide resource 6.4.3 Signal Handling • A signal is used in UNIX systems to notify a process that a particular event has occurred • A signal may be received either synchronously or asynchronously, depending on the source of and the reason for the event being signaled − A Synchronous signals are delivered to the same process that performed the operation that caused the signal Example: illegal memory access and division by − An asynchronous signal is generated by an event external to a running process, that process receives the signal asynchronouslysignal is sent to another process • All signals follow the same 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 • Every signal may be handled by one of two possible handlers: − A default signal handler − A user-defined signal handler • Every signal has a default signal handler that is run by the kernel This default action can be overridden by a user-defined signal handler that is called to handle the signal • In general, a signal be delivered following 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 • Signals may be handled in different ways Some signals (such as changing the size of a window) may simply be ignored; others (such as an illegal memory access) may be handled by terminating the program 6.4.4 Thread Pools • The general idea behind a thread pool is to create a number of threads at process startup and place them into a pool, where they sit and wait for work • Thread pools offer these benefits: − Servicing a request with an existing thread is usually faster than waiting to create a thread − A thread pool limits the number of threads that exist at any one point This is particularly important on systems that cannot support a large number of concurrent threads 6.4.5 Thread-Specific Data • Threads belonging to a process share the data of the process Indeed, this sharing of data provides one of the benefits of multithreaded programming • However, in some circumstances, each thread might need its own copy of certain data We will call such data thread-specific data • Useful when we not have control over the thread creation process (i.e., when using a thread pool) Reference: Silberschatz-Galvin-Gagne, Operating System Concepts, USA, 2005 (http://www.os-book.com) ... concurrency 6.2 Multithreading Models • Threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads • User threads are supported above the kernel and... Kernel threads are supported and managed directly by the operating system • There must exist a relationship between user threads and kernel threads 6.2.1 Many-to-One Model • Maps many user-level threads. .. different threads of activity within the same address space • Economy Because threads share resources of the process to which they belong, it is more economical to create and context-switch threads