140 likes | 286 Vues
Threads. An Introduction to Multithreaded Programming. Bryce Tompkins, tompkins_bryce@emc.com. First There Was The Process . (from Wikipedia) a process is a running instance of a program, including all variables and other state.
E N D
Threads An Introduction to Multithreaded Programming Bryce Tompkins, tompkins_bryce@emc.com
First There Was The Process • (from Wikipedia) a process is a running instance of a program, including all variables and other state. • The “state” includes open file descriptors/handles, address space, context (registers), etc. • UNIX and (modern) Windows processes exhibit similar characteristics. • Processes are scheduled preemptively by the kernel. Only one process can execute at a time on a single-processor/core computer. Multiple processes can execute simultaneously on multi-processor/core computers.
Process Scheduling • UNIX and Windows are examples of Multitasking operating systems in which the OS is responsible for scheduling CPU time for each running process. • Multitasking gives the illusion of concurrency on a single processor machine. • The CPU-time allocated to a process and the frequency in which it is scheduled to run, are dependent on several factors: scheduling priority, policy (round-robin, FIFO), I/O. • A Context Switch occurs each time the kernel swaps one process for another off the CPU.
The Context Switch • The kernel maintains the context of the process in registers on the CPU. The context includes: the Stack Pointer, the Program Counter, the Status Register and General Purpose Registers. • The context is stored in kernel memory and is retrieved and loaded at the process’s next scheduling. • The Context Switch allows the execution of the process to pick up where it left off when it is scheduled next.
Memory Swapping • Virtual memory allows operating systems to map an address space much greater than the physical memory available. • Memory segments are “swapped” in and out of physical memory, to and from disk (swap space), on demand. Memory segments not used in a while are swapped out in favor of segments in demand.
Then There Were Threads • (from Wikipedia) A thread is short for a thread of execution or a sequence of instructions. • Every process has at least one thread of execution. A process with more than one thread is referred to as a multithreaded process.
Thread Characteristics • Threads are scheduled by the kernel. • Each thread maintains its own stack. • All threads within a process share the same address space and file descriptors/handles.
Thread Benefits • A shared address space has benefits: • Shorter context switches since process memory is less likely to be swapped out. • Inter-thread communication is less cumbersome than inter-process communication • Asynchronous programming. Computation bound threads can run while I/O bound threads are blocking. • Multiple threads can run on multiple processors/cores.
The Difficultly with Multithreaded Programming • A shared address space requires programmers to synchronize access to global and heap allocated data to prevent “race conditions”. A race condition occurs when two or more threads are in contention for a shared resource. • A race condition can cause corruption of data resulting in unexpected results or segmentation violations. • Synchronization methods may lead to deadlocks between multiple threads. • A delinquent thread can bring down (crash) the entire process.
Threads in NetWorker 7.3 • Authentication requests in nsrexecd. • Message passing in NMC/gstd. • The Legato Portmapper in nsrexecd. • Concurrent recovers in nsrmmd.
Thread Aware Tools • Solaris • ps –efL, GNU top, prstat, gdb • Windows • Task Manager • Mac OS X • Activity Monitor