The Usual Approaches to Multitasking
There are four main ways to implement multitasking:
- Multiple processes
- Kernel threads
- User threads
- Cooperative multitasking
Multiple Processes
In an operating system such as UNIX or VMS, an application can be
divided up into multiple processes. The operating system rapidly
switches back and forth among the processes so that they seem to run
simultaneously. These processes may need to communicate with each
other to coordinate their activities, using sockets, shared memory
segments, semaphores, or whatever other facilities are available.
Kernel Threads
Kernel threads are somewhat like multiple processes. They run
concurrently, as scheduled by the operating system. (The term "kernel"
refers to the operating system, especially in Unix and Unix-like
systems.)
However, each thread is only a subdivision of a process. The operating
system runs multiple processes, each of which may consist of multiple
threads. Within a process, all threads reside within the same memory
space. They can communicate with each other through that shared memory,
without relying on the operating system to provide any form of
interprocess communication.
As with separate processes, the operating system may interrupt a
given thread at any time in order to run another thread for a while.
When the first thread receives control again, it resumes as if nothing
had happened -- except that the contents of memory are likely to have
been changed in the meanwhile by another thread. It may take
careful programming to keep different threads from interfering with
each other through these asynchronous side effects.
User Threads
User threads are similar to kernel threads, except that they are
invisible to the operating system. The process in which they reside
schedules CPU time for them and manages the resulting interruptions.
This approach requires support from the language, such as Java, or
from a multithreading library.
Cooperative Multitasking
Instead of interrupting threads in the middle of execution, cooperative
multitasking lets each thread interrupt itself voluntarily at intervals.
This approach minimizes the chance that threads will interfere with
each other unexpectedly. It also requires no special support from the
operating system or the language.
This is the approach taken by Cheap Threads.
Some people prefer not to use the term "threads" for cooperative
multitasking. To them, multithreading is asynchronous by definition.
I prefer to use a different definition -- concurrency within a single
address space, whether synchronous or asynchronous. In any case the
choice of terminology is not important. What's important is that
each option offers a different combination of advantages and
disadvantages.
Home