1 / 58

Chapter 4: Threads

Chapter 4: Threads. Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Thread: Introduction. Each process has Own Address Space Single thread of control A process model has two concepts: Resource grouping

victor-key
Download Presentation

Chapter 4: Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Threads

  2. Chapter 4: Threads • Overview • Multithreading Models • Threading Issues • Pthreads • Windows XP Threads • Linux Threads • Java Threads

  3. Thread: Introduction • Each process has • Own Address Space • Single thread of control • A process model has two concepts: • Resource grouping • Execution • Sometimes it is useful to separate them

  4. Unit of Resource Ownership • A process has an • Address space • Open files • Child processes • Accounting information • Signal handlers • Etc • If these are put together in a form of a process, can be managed more easily

  5. Unit of Dispatching • Path of execution • Program counter: which instruction is running • Registers: • holds current working variables • Stack: • Contains the execution history, with one entry for each procedure called but not yet returned • State • Processes are used to group resources together • Threads are the entities scheduled for execution on the CPU • Threads are also called lightweight process

  6. Its better to distinguish between the two concepts • Address space/Global Variables • Open files • Child processes • Accounting info • Signal handlers • Program counter • Registers • Stack • State Heavy weight process In case of multiple threads per process Light weight processes Unit of Resource Split • Program counter • Registers • Stack • State • Address space/Global Variables • Open files • Child processes • Accounting info • Signal handlers • Program counter • Registers • Stack • State • Program counter • Registers • Stack • State Unit of Dispatch Share

  7. 65 47 of 65 0 Threads allow you to multiplex which resources? • CPU • Memory • PCBs • Open files • User authentication structures

  8. Thread • A thread is a basic unit of CPU utilization. It consist of • A thread ID • A program Counter • A register Set • A stack • Threads share something with its peer threads (all the other5 threads in this particular task) the things that it share are • Its code section • Its data section • Any OS resources, available for the task. • The first thread starts execution with int main(int argc, char *argv[]) • The threads appear to the Scheduling part of an OS just like any other process • Allow multiple execution paths in the same process environment

  9. Threads vs. Processes • Processes • A process has code/data/heap & other segments • There must be at least one thread in a process • Threads within a process share code/data/heap, share I/O, but each has its own stack & registers • Expensive creation • Expensive context switching • If a process dies, its resources are reclaimed & all threads die • Inter-process communication via OS and data copying. Threads • A thread has no data segment or heap • A thread cannot live on its own, it must live within a process • There can be more than one thread in a process, the first thread calls main & has the process’s stack • Inexpensive creation • Inexpensive context switching • If a thread dies, its stack is reclaimed • Inter-thread communication via memory.

  10. Process Vs. Threads (a) Three threads, each running in a separate address space (b) Three threads, sharing the same address space

  11. 65 47 Context switch time for which entity is greater? • Process • Thread

  12. The Thread Model Each thread has its own stack

  13. Single and Multithreaded Processes • A traditional heavy weight process is same as task with one thread. It has a single thread of control. • If a process is multi thread, then that means more than one part of the thread is executing at one time. • Multi threading can be useful in programs such as web browsers where you can wish to download a file , view an animation and print something at the same time.

  14. Single and Multithreaded Processes

  15. TCB for Thread1 PC SP State Registers … TCB for Thread2 Stack – thread2 PC SP State Registers … Stack – thread1 Implementing Threads Process’s address space • Processes define an address space; threads share the address space • Process Control Block (PCB) contains process-specific information • Owner, PID, heap pointer, priority, active thread, and pointers to thread information • Thread Control Block (TCB) contains thread-specific information • Stack pointer, PC, thread state (running, …), register values, a pointer to PCB, … mapped segments DLL’s Heap Initialized data Code

  16. Threads’ Life Cycle • Threads (just like processes) go through a sequence of start, ready, running, waiting, and done states Done Start Ready Running Waiting

  17. Benefits Of Multi-Threading • Responsiveness Multithreading increase the responsiveness .As the process consist of more than one thread, if one thread block or busy in lengthy calculation, some other thread still executing of the process. So the user get response from executing process. • Resource Sharing All threads, which belongs to one process, share the memory and resources of that process. Secondly it allow the application to have several different threads within the same address space.

  18. Benefits Of Multi-Threading • Economy Allocation of memory and resources or process creation is costly. All threads of a process share the resources of that process so it is more economical to create and context switch the thread. • Utilization of Multi processor Architectures MP architecture allows the facility of parallel processing, which is most efficient way of processing. A single process can run on one CPU even if we have more processors. Multi-threading on MP system increase the concurrency. If a process is dividing into multiple threads , these threads can execute simultaneously on different processors.

  19. Types of Threads • There are two types of threads • Kernel Threads • User Threads

  20. User Threads • User level threads are not seen by operating system and also very fast (switching from one thread to another thread in a single process does not require context switch since same process is still executing). However, if the thread that is currently executing blocks, the rest of the process may also blocked( if OS is using only one single kernel thread for this process i.e. the thread that kernel sees is same as blocked thread , hence kernel assume that whole process is blocked). • Thread management done by user-level threads library • Three primary thread libraries: • POSIX Pthreads • Win32 threads • Java threads

  21. Kernel Threads • Kernel Supported threads are seen by operating system and must be scheduled by the operating system. One multi thread may have multiple kernel threads. • Examples • Windows XP/2000 • Solaris • Linux • Tru64 UNIX • Mac OS X

  22. User-Level Managed by application Kernel not aware of thread Context switching cheap Create as many as needed Must be used with care Kernel-Level Managed by kernel Consumes kernel resources Context switching expensive Number limited by kernel resources Simpler to use User-Level vs. Kernel Threads Key issue: kernel threads provide virtual processors to user-level threads, but if all of kthreads block, then all user-level threads will block even if the program logic allows them to proceed

  23. Thread Libraries • Thread library provides programmer with API for creating and managing threads • Two primary ways of implementing • Library entirely in user space with no kernel support. All the code and data structure exists in user space. This means that invoking of a function in a the library results in a local function call in user space and not a system call. • Kernel-level library supported by the OS. In this case code and data structures for the library exits in kernel space. Invoking a function in the API of library typically results in a system call to a kernel.

  24. Thread Libraries • Three main thread libraries are used in today • POSIX Pthreads • Win32 • Java

  25. Multithreading Models • Many-to-One • One-to-One • Many-to-Many

  26. Many-to-One • Many user-level threads mapped to single kernel thread. • It is efficient because it is implemented in user space. A process using this model blocked entirely if a thread makes a blocking system call. • Only one thread can access the kernel at a time so it can not be run in parallel on multiprocessor. • Examples: • Solaris Green Threads • GNU Portable Threads("Genuinely Not Unix" ; GNU is an operating system composed of free software)

  27. Many-to-One Model

  28. One-to-One • Each user-level thread maps to kernel thread. • It provides more concurrency because it allows another thread to execute when threads invoke the blocking system call. • It facilitates the parallelism in multiprocessor systems. • Each user thread requires a kernel thread, which may affect the performance of the system. • Creation of threads in this model is restricted to certain number. • Examples • Windows NT/XP/2000 • Linux • Solaris 9 and later

  29. One-to-one Model

  30. Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads • Allows the operating system to create a sufficient number of kernel threads • Number of kernel threads may be specific to a either a particular application or a particular machine. • The user can create any number of threads and corresponding kernel level threads can run in parallel on multiprocessor. • When a thread makes a blocking system call, the kernel can execute another thread. • Solaris prior to version 9 • Windows NT/2000 with the ThreadFiber package

  31. Many-to-Many Model

  32. Multithreading Models: Comparison • The many-to-one model allows the developer to create as many user threads as he/she wishes, but true concurrency can not be achieved because only one kernel thread can be scheduled for execution at a time • The one-to-one model allows more concurrence, but the developer has to be careful not to create too many threads within an application • The many-to-many model does not have these disadvantages and limitations: developers can create as many user threads as necessary, and the corresponding kernel threads can run in parallel on a multiprocessor

  33. Two-level Model • Similar to M:M, except that it allows a user thread to be bound to kernel thread • Examples • IRIX • HP-UX • Tru64 UNIX • Solaris 8 and earlier

  34. Two-level Model user-level threads LWP LWP LWP LWP • Combination of one-to-one + “strict” many-to-many models • Supports both bound and unbound threads • Bound threads - permanently mapped to a single, dedicated LWP • Unbound threads - may move among LWPs in set • Thread creation, scheduling, synchronization done in user space • Flexible approach, “best of both worlds” • Used in Solaris implementation of Pthreads and several other Unix implementations (IRIX, HP-UX)

  35. Two-level Model

  36. 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 • Common in UNIX operating systems (Solaris, Linux, Mac OS X)

  37. Java Threads • Java threads are managed by the JVM • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface

  38. Java Thread States

  39. Threading Issues • Semantics of fork() and exec() system calls • Thread cancellation • Signal handling • Thread pools • Thread specific data • Scheduler activations

  40. Semantics of fork() and exec() • As the fork() system call is used to create a separate, duplicate process. • The semantics of the fork() and exec() system calls change in a multithreaded program. • If one thread in a program calls the fork(), does the new process duplicate all the threads or is the new process single threaded?

  41. Semantics of fork() and exec() • Does fork() duplicate only the calling thread or all threads? • Two versions of fork() in UNIX:, one that duplicates all threads and 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. • If exec() is called immediately after forking, then duplicating all threads is unnecessary, as the program specified in the parameters to exec() will replace the process. In this instance, duplicating only the calling thread is appropriate. • If the separate process does not call exec() after forking, the separate process should duplicate all threads.

  42. Thread Cancellation • Thread cancellation is the task of terminating a thread before it has finished. For example, if multiple threads are concurrently searching through a database and one thread return the result, the remaining threads might be canceled. • A thread that is to be often canceled is referred as the target thread. • Two general approaches: • Asynchronous cancellation terminates the target thread immediately • Deferred cancellation allows the target thread to periodically check if it should be cancelled, allowing it an opportunity to terminate itself in an orderly fashion.

  43. Problem in Thread Cancellation • The difficulty with cancellation occurs in a situations • where resources have been allocated to a cancel thread • Where thread has been cancelled in the midst of updating data it share with other threads. • This becomes especially difficult with asynchronous cancellation. Often operating system will reclaim system resources from the canceled thread but will not reclaim all the resources. Therefore, canceling a thread asynchronously may not free a necessary system-wide resource.

  44. Signal Handling • A signal is used in a UNIX system to notify a process that a particular event has occurred. Signal may be received either asynchronously or synchronously. All Signal follow the same pattern. • A signal handler is used to process signals • Signal is generated by particular event • Signal is delivered to a process • Signal is handled

  45. Signal Handling • Examples of synchronous signals include illegal memory access and division by 0. if a running program perform either of these operations a signal is generated. Synchronous signals are delivered to the same process that performed the operation that caused the signals. • When a signal is generated by some event external to a running process, that process receive the signal asynchronously. Examples of such signals include terminating a process with specific key strokes (such as <control><C>) and having a timer expire. Typically , asynchronous signal is sent to another process.

  46. Signal Handling in UNIX Every signal may be handled by one of two possible handlers: A default signal handler that is run by the kernel when handling that signal. A user-defined signal handler that is called to handle a signal.

  47. Signal Handling • Delivering the signal in multithreaded programs is more complicated, where a process may have several threads. Following Options exist: • 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.

  48. Thread Pools • The general idea is to create a number of threads at process startup and place them into a pool where they sit and wait for work • Advantages: • Usually slightly faster to service a request with an existing thread than create a new thread • Allows the number of threads in the application(s) to be bound to the size of the pool

  49. Thread Specific Data • Allows each thread to have its own copy of data • Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

  50. Scheduler Activations • A final issue to be considered with multithreaded programs concerns with communication between kernel level and user level thread library. • Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application. • Many system implementing either M:M or Two level model place an intermediate data structure between user and kernel threads. This data structure is typically known as light weight process or (LWP).

More Related