- Windows Processes and Threads
- Process Creation
- Process Handle Counts
- Process Identities
- Duplicating Handles
- Exiting and Terminating a Process
- Waiting for a Process to Terminate
- Environment Blocks and Strings
- Example: Parallel Pattern Searching
- Processes in a Multiprocessor Environment
- Process Execution Times
- Example: Process Execution Times
- Generating Console Control Events
- Example: Simple Job Management
- Job Objects
A process contains its own independent virtual address space with both code and data, protected from other processes. Each process, in turn, contains one or more independently executing threads. A thread running within a process can create new threads, create new independent processes, and manage communication and synchronization between the objects.
By creating and managing processes, applications can have multiple, concurrent tasks processing files, performing computations, or communicating with other networked systems. It is even possible to exploit multiple processors to speed processing.
This chapter explains the basics of process management and also introduces the basic synchronization operations that will be used throughout the rest of the book.
Windows Processes and Threads
Every process contains one or more threads, and the Windows thread is the basic executable unit. Threads are scheduled on the basis of the usual factors: availability of resources such as CPUs and physical memory, priority, fairness, and so on. Windows has supported symmetric multiprocessing (SMP) since NT4, so threads can be allocated to separate processors within a system.
From the programmer's perspective, each Windows process includes resources such as the following components:
One or more threads.
A virtual address space that is distinct from other processes' address spaces, except where memory is explicitly shared. Note that shared memory-mapped files share physical memory, but the sharing processes will use different virtual addresses to access the mapped file.
One or more code segments, including code in DLLs.
One or more data segments containing global variables.
Environment strings with environment variable information, such as the current search path.
The process heap.
Resources such as open handles and other heaps.
Each thread in a process shares code, global variables, environment strings, and resources. Each thread is independently scheduled, and a thread has the following elements:
A stack for procedure calls, interrupts, exception handlers, and automatic storage.
Thread Local Storage (TLS)arrays of pointers giving each thread the ability to allocate storage to create its own unique data environment.
An argument on the stack, from the creating thread, which is usually unique for each thread.
A context structure, maintained by the kernel, with machine register values.
Figure 6-1 shows a process with several threads. This figure is schematic and does not indicate actual memory addresses, nor is it drawn to scale.
Figure 6-1 A Process and Its Threads
This chapter shows how to work with processes consisting of a single thread. Chapter 7 shows how to use multiple threads.
Note: Figure 6-1 is a high-level overview from the programmer's perspective. There are numerous technical and implementation details, and interested readers can find out more in Inside Windows 2000 (Solomon and Russinovich).
A UNIX process is comparable to a Windows process with a single thread.
Threads, in the form of POSIX Pthreads, are a recent addition to UNIX implementations and are now nearly universally used. Stevens (1992) does not discuss threads; everything is done with processes.
Needless to say, vendors and others have provided various thread implementations for many years; they are not a new concept. Pthreads is, however, the most widely used standard, and proprietary implementations are obsolete.