Java Reference
In-Depth Information
Note that a process is also a unit of execution. Therefore, the two sets of instructions can be run as two processes
to achieve concurrency in their execution. So far, we have assumed that the two sets of instructions are independent of
each other. Suppose this assumption still holds true. What if the two sets of instructions access a shared memory; or,
when both sets of instructions finish running, you need to combine the results from both to compute the final result?
Processes are generally not allowed to access another process's address space. They must communicate using inter-process
communication facilities such as sockets, pipes, etc. The very nature of a process—that it runs independent of another
process—may pose problems when multiple processes need to communicate or share resources. All modern operating
systems let you solve this problem by allowing you to create multiple units of execution within a process, where all units
of execution can share address space and resources allocated to the process. Each unit of execution within a process is
called a thread .
Every process has at least one thread. A process can create multiple threads, if needed. The resources available
to the operating system and its implementation determine the maximum number of threads a process can create. All
threads within a process share all resources including the address space; they can also communicate with each other
easily because they operate within the same process and they share the same memory. Each thread within a process
operates independent of the other threads within the same process.
A thread maintains two things: a program counter and a stack. The program counter lets a thread keep track of
the instruction that is currently executed by it. It is necessary to maintain a separate program counter for each thread
because each thread within a process may be executing different instructions at the same time. Each thread maintains
its own stack to store the values of the local variables. A thread can also maintain its private memory, which cannot be
shared with other threads, even if they are in the same process. The private memory maintained by a thread is called
thread-local storage (TLS) . Figure 6-2 depicts threads represented within a process.
An operating system
Process
Process
Process
A thread within a process
Figure 6-2. Processes and threads
In all modern operating systems, threads are scheduled on the CPU for execution, not the processes. Therefore,
the CPU context switch occurs between the threads. The context switch between threads is less expensive compared
to the context switch between processes. Because of the ease of communication, sharing resources among threads
within a process, and a cheaper context switch, it is preferred to split a program into multiple threads, rather than
multiple processes. Sometimes a thread is also called a lightweight process . The program with six instructions as
discussed above can also be split into two threads within a process as depicted in Figure 6-3 . On a multi-processor
machine, multiple threads of a process may be scheduled on different processors, thus providing true concurrent
executions of a program. A program that uses multiple threads is called a multi-threaded program .
 
 
Search WWH ::




Custom Search