. . .
UNIX and Win32 threads will. This is not a disaster, but it does make extra work for the library
Threads and LWPs
In a typical, traditional, multitasking operating system, a process comprises memory, the CPU
register state, and some system state (file descriptors, user ID, working directory, etc., all stored in
the process structure). When it's time to context-switch two processes, the kernel saves the
registers in the process structure, changes some virtual memory pointers, loads the CPU registers
with data from the other process structure, and continues.
When context-switching two threads, the registers are saved as before, but the memory map and
the "current process" pointer remain the same. The idea is that you have a single program, in one
memory space, with many virtual CPUs running different parts of the program concurrently.
What actually makes up a thread are (see Figure 3-2) its own stack and stack pointer; a program
counter; some thread information, such as scheduling priority, and signal mask, stored in the
thread structure; and the CPU registers (the stack pointer and program counter are actually just
Figure 3-2. Contents of a Thread
Everything else comes from either the process or (in a few cases) the LWP. The stack is just
memory drawn from the program's heap. A POSIX thread could look into and even alter the
contents of another thread's stack if it so desired. (Although you, being a good programmer, would
never do this, your bugs might.)
Putting all this together, we end up with a picture such as Figure 3-3. The threads, their stacks, the
code they run, and the global data that they share are all in user space, directly under user control.
The thread structures are also in user space, but completely under the control of the threads library.
There is no legal way for a user program to access those structures directly. The library itself, like
every other system library, is just regular user code that you could have written yourself.
Figure 3-3. How the Threads Library Fits into a Process
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks