Having many threads within one address space is a better solution than having multiple, heavy-weight processes cooperating with each other:
-
Different processes cannot easily share common data structures and other, Operating System dependent resources.
-
Switching the CPU between processes is a costly operation which requires not only the swap of machine registers but also OS specifics like the kernel stack, process stack, page tables and signal vectors. Often cache purging has to be done too. A context switch for threads can be very efficient.
-
Since there is no common address space for different processes, interaction between them (IPC) has to be carried out by the system supplied mechanisms: semaphores, shared memory, sockets, pipes etc. These interactions are much more expensive than accessing common data structures.
|