Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

Similar presentations


Presentation on theme: "1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads."— Presentation transcript:

1

2 1 Chapter 5 Threads

3 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads

4 3 Thread  A thread, sometimes called a lightweight process, is a basic unit of CPU utilization  Comprises  A thread ID  A program counter  A register set  A stack  Shares with other threads belonging to the same process:  Code section  Data section  Other resources § 5.1

5 4 Thread  A traditional, or heavyweight, process has a single thread of control.

6 5 Thread Examples  Busy web server has many concurrently accessing clients.  If the web server ran as a traditional single- threaded process, it would be able to service only one client at a time…slow!  Solution: one process contains multiple threads to serve each requests… multithread the web- service process.

7 6 Multithreaded RPC  Typically, RPC servers are multithreaded.  When a server receives a message, it services the message using a separate thread.  This allow the server to service several concurrent requests.

8 7 Benefits  Responsiveness – continue running even if part of it is blocked  Resource sharing – allows an application to have several different threads of activity all within the same address space  Economy – it is more economical to create and context switch threads than process  Utilization of multiprocessor architectures – in single-processor architecture, multithread is only an illusion § 5.1.2

9 8 User Threads  User threads are supported by user-level threads library without the need for kernel intervention.  Fast to create and manage.  Examples - POSIX Pthreads - Mach C-threads - Solaris threads § 5.3

10 9 Kernel Threads  Kernel threads are supported directly by the OS in kernel space  Slower to create and manage.  Examples - Windows 95/98/NT - Solaris - Digital UNIX § 5.3.2

11 10 Multithreading Models  Many-to-One Model  One-to-One Model  Many-to-Many Model § 5.2

12 11 Many-to-One Model  Many user-level threads mapped to single kernel thread.  Used on systems that do not support multiple kernel threads.  Efficient, but the entire process will be blocked if a thread makes a blocking system call  Since only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors

13 12 Many-to-One Model

14 13 One-to-One Model  Each user thread maps to a kernel thread.  Provides more concurrency than the many-to-one  Drawback: creating a user thread requires creating the corresponding kernel thread … burden the performance … most system restrict the number of threads supported.  Examples - Windows 95/98/NT - OS/2

15 14 One-to-One Model

16 15 Many-to-Many Model  Multiplex many user threads to a smaller or equal number of kernel threads.  Developers can create as many user threads as necessary and the corresponding kernel threads can run in parallel on a multiprocessor.  Examples:  Solaris  IRIX  Digital UNIX

17 16 Many-to-Many Model

18 17 Threading Issues  Thread calls fork :  New process duplicate all threads  New process duplicate only the thread that invoked the fork system call.  Usage of the two versions of fork depends upon the application.  Thread invokes the exec system call:  The program specified in the parameter to exec will replace the entire process – including all threads and LWPs. § 5.3

19 18 Thread Cancellation  Thread cancellation is the task of terminating a thread before it has completed, example:  One of the threads searching a database complete.  User stops a web page from loading further.  Two scenarios to cancel a thread (called target thread):  Asynchronous calcellation: One thread immediately terminates the target thread.  Deferred cancellation: The target thread can periodically check if it should terminate, allowing the target thread an opportunity to terminate itself in an orderly fashion. § 5.3.2

20 19 Asynchronous Calcellation  Difficulties:  Resources have been allocated to a cancelled thread  A thread was cancelled while in the middle of updating data it is sharing with other threads. The OS will often reclaim system resources from a cancelled thread, but often will not reclaim all resources. Cancelling a thread asynchronously may not free a necessary system-wide resource.

21 20 Deferred Cancellation  Works by one thread indicating that a target thread is to be cancelled.  Allow a thread to check if it should be cancelled at a point when it can safely be cancelled.  Pthreads refers to such points as cancellation points.

22 21 Singnal 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 upon the source and the reason for the event being signaled.  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. § 5.3.3

23 22 Synchronous Signal  Synchronous signal are delivered to the same process that performed the operation causing the signal. Examples:  Illegal memory access  Division by zero.

24 23 Asynchronous Signal  When a signal is generated by an event external to a running process, that process receives the signal asynchronously. Example:  Terminating a process with specific keystrokes (such as Ctrl-C)  Timer expire  Typically, an asynchronous signal is sent to another process.

25 24 Signal Handling  Every signal may be handled by one of two handlers:  A default signal handler Run by the kernel when handling the signal.  A user-defined signal handler May override the default signal handler.

26 25 Signal Delivering  A signal should be delivered to which of the threads owned by a process?  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.

27 26 Windows 2000  Although Windows 2000 does not explicitly provide support for signals, they can be emulated using aysnchronous procedure calls (APCs).  APC is roughly equivalent to an asynchronous signal in UNIX.  Whereas UNIX must contend with how to deal with signals in a multithreaded environment, the APC is more straightforward as an APC is delivered to a particular thread rather than process.

28 27 Thread Pools  Multithreaded server has potential problems  Need time to create the thread prior to servicing the request.  Unlimited threads could exhaust system resources…use thread pools. § 5.3.4 Create a number of threads at process startup and place them into a pool, where they sit and wait for work.

29 28 Benefits of Thread Pools 1. Usually faster to service a request with an existing thread than waiting to create a thread. 2. A thread pool limits the number of threads that exist at any one point.  The number of threads in the pool can be set heuristically based upon system factors.  More sophisticated thread-pool architectures can dynamically adjust the number of threads in the pool according to usage patterns.

30 29 Thread-Specific Data  Although the data of a process can be shared by its threads, a thread might need its own copy of certain data, called thread-specific data.  For example: transaction-processing.  Most thread libraries, including Win32, Pthreads, and Java, provides this support.

31 30 Pthreads  Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and implementation.  This is a specification for thread behavior, not an implementation.  OS designers may implement the specification any way they wish.  Generally restricted to UNIX-based system such as Solaris 2. Windows generally not support Pthreads. § 5.4

32 31 Solaris 2 Threads  Solaris 2 is a version of UNIX  Supported only traditional heavy-weight processes within a single thread of control until 1992  Intermediate level thread: lightweight processes (LWP)  Each process contains at least one LWP  Only user-level threads currently connected to an LWP accomplish work § 5.5

33 32 Solaris 2 Threads

34 33 Solaris Process ( 略 )

35 34 Windows 2000 Threads  A Windows application runs as a separate process where each process may contain one or more threads.  Windows 2000 uses one-to-one and many-to-many mapping.  General components of a Windows 2000 thread:  A thread ID  A register set representing the status of the processor.  A user stack used when the thread is running its user mode. (kernel stack when kernel mode.)  A private storage area used by various run-time libraries and dynamic link libraries. § 5.6

36 35 Windows 2000 Threads  The primary data structures of a thread include:  ETHREAD (executive thread block) pointer to belonging process, address of thread routine, pointer to KTHREAD  KTHREAD (kernel thread block) scheduling and synchronization information for the thread, kernel stack, pointer to TEB  TEB (thread environment block) user stack Kernel space User space

37 36 Linux Threads  Fork system call duplicates a process.  Clone system call creates a “thread” analogously by creates a separate process sharing the address space of the calling process.  When fork is invoked, a new process is created along with a copy of all the associated data structures of the parent process.  When clone is invoked, a new process is created which points to the data structure of the parent process for sharing it. § 5.7

38 37 Linux Threads  Linux does not distinguish between processes and threads.  Linux usually uses the term task rather then process or thread.

39 38 Java Threads  Java is unique because it provides support for creation and management of threads at the language level, it provides commands that allows the programmer to create and manipulate threads of control within the program.  All Java program comprise at least a single thread of control. § 5.8

40 39 Java Thread Management  Java APIs for managing threads:  suspend() – suspends execution of the currently running thread.  sleep() – puts the currently running thread to sleep for a specified amount of time.  resume() – resumes execution of a suspended thread.  stop() – stops execution of a thread. § 5.6.2

41 40 Applets with threads  Applets are natural examples for multithreading because they commonly have graphics, animation, and audio – all good candidates as threads.  start() method of an applet is called when an applet is first displayed. If the user leaves the web page or the applet scrolls off the screen, the applet’s stop() method is called.  The destroy() method of an applet is called when the applet is removed from the browser’s cache.

42 41 Java Threads Creation  One way to create a thread explicitly is to create a new class that is derived from the Thread class, and to override the run method of the Thread class.  Example: (using old example of the book) § 5.8.1

43 42 Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } public class First { public static void main(String args[]) { Worker1 runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); } Worker1 Thread Worker1 runner run start

44 43 Extending the Thread Class  An object of this derived class will run as a separate thread of control in the JVM.  Calling the start method for the new object does two things: 1. It allocates memory and initializes a new thread in the JVM. 2. It calls the run method, making the thread eligible to be run by the JVM.  Two threads are created by the JVM: 1. The thread associated with the application – the thread that starts execution at the main() method. 2. The runner thread that is created explicitly with the start() method.

45 44 Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } public class First { public static void main(String args[]) { Worker1 runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); } 1 2

46 45 JVM and Host OS  The typical implementation of the JVM is on top of a host OS. This allows the JVM to hide the implementation details of the underlying OS and to provide a consistent, abstract environment that allows Java programs to operate on any platform that supports a JVM.  Windows 95/98/NT/2000: one-to-one, each Java thread for a JVM running on NT maps to a kernel thread  Solaris 2: many-to-one  Solaris 2.6: many-to-many § 5.8.2


Download ppt "1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads."

Similar presentations


Ads by Google