11-Threads Introduction
11-Threads Introduction
11-Threads Introduction
Thread: Introduction
1. 2.
1. 2.
A process has an
Address space Open files Child processes Accounting information Signal handlers
If these are put together in a form of a process, can be managed more easily
Unit of Dispatching
Path of execution
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
Unit of Resource
Address space/Global Variables Open files Child processes Accounting Info Signal Handlers
Split
Share
Feb 19, 2009
Program Counter Registers Program Counter Stack Registers State Stack Program Counter State Registers Stack State
Threads
Allow multiple execution paths in the same process environment Threads share address space, open files etc But have own set of Program counter, Stack etc The first thread starts execution with
The threads appear to the Scheduling part of an OS just like any other process
Thread Usage
the newly created thread uses the current process address space no resources attached to them
Less time to terminate a thread than a process. Less time to switch between two threads within the same process, because the newly created thread uses the current process address space. Less communication overheads
threads share everything: address space, in particular. So, data produced by one thread is immediately available to all the other threads Substantial Computing and Substantial Input/Output
Performance gain
Problems
Unfair for quick requests, occurring behind lengthy request Complex and error prone Heavy IPC required
Feb 19, 2009
2. Background Processing
Consider writing a GUI-based application that uses:
Mouse Keyboard input Handles various clock-based events
In a single threaded application, if the application is busy with one activity, it cannot respond (quickly enough) to other events, such as mouse or keyboard input. Handling such concurrency is difficult and complex But simple in a multithreaded process
Sort some data items on a shared-memory parallel computer. Our task is merely to implement a multithreaded sorting algorithm.
Divide the data into four pieces Have each processor sort a different piece. Two processors each merge two pieces One processor merges the final two combined pieces.