Threads An Introduction to Multithreaded Programming Bryce Tompkins
Threads An Introduction to Multithreaded Programming Bryce Tompkins, tompkins_bryce@emc. com © 2004 EMC Corporation. All rights reserved. 1
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 singleprocessor/core computer. Multiple processes can execute simultaneously on multi-processor/core computers. © 2004 EMC Corporation. All rights reserved. 2
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. © 2004 EMC Corporation. All rights reserved. 3
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. © 2004 EMC Corporation. All rights reserved. 4
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. © 2004 EMC Corporation. All rights reserved. 5
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. © 2004 EMC Corporation. All rights reserved. 6
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. © 2004 EMC Corporation. All rights reserved. 7
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. © 2004 EMC Corporation. All rights reserved. 8
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 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. © 2004 EMC Corporation. All rights reserved. 9
Threads in Net. Worker 7. 3 Authentication requests in nsrexecd. Message passing in NMC/gstd. The Legato Portmapper in nsrexecd. Concurrent recovers in nsrmmd. © 2004 EMC Corporation. All rights reserved. 10
Thread Aware Tools Solaris – ps –ef. L, GNU top, prstat, gdb Windows – Task Manager Mac OS X – Activity Monitor © 2004 EMC Corporation. All rights reserved. 11
Demo © 2004 EMC Corporation. All rights reserved. 12
Q&A © 2004 EMC Corporation. All rights reserved. 13
© 2004 EMC Corporation. All rights reserved. 14
- Slides: 14