Understanding the Differences Between Processes and Threads
There are various concepts of the term task in real-time multi-tasking systems. Two of those concepts, processes and threads are addressed in this article.
Thread Example
The code fragment below shows a Tics example of two simple cooperative threads, taskA and taskB.
void taskA(void)
{
while (TRUE) {
printf("Hello from taskA... ");
yield();
}
}
void taskB(void)
{
while (TRUE) {
printf("Hello from taskB... ");
yield();
}
}
void main()
{
startTics(makeTics(MsgSpace, NUM_MSGS));
startTask(makeTask(taskA, 0));
startTask(makeTask(taskB, 0));
suspend();
}
Note the following:
- The tasks, taskA and taskB, are C functions.
- Each task is started in main by making kernel calls.
- The program above is compiled and linked to create a single executable file. When the executable file is loaded and run, taskA and taskB run as concurrent tasks.
- The tasks share the same program space. For example, the tasks can share global data.
Process Example
A process is like an MS-DOS EXE file; it is a complete application. A multi-tasking system that allows for multiple processes therefore allows for multiple applications. Such a system might have a word processor, a database, and a spreadsheet operating as three different processes in three different windows. Note the following:
- Each process has its own separate program space.
- Process A cannot read or write into process B's program space.
- Each process carries the same overhead in terms of bulk that an EXE requires.
Advantages and Disadvantages
Thread Advantages
- Threads are memory efficient. Many threads can be efficiently contained within a single EXE, while each process can incur the overhead of an entire EXE.
- Threads share a common program space, which among other things, means that messages can be passed by queuing only a pointer to the message. Since processes do not share a common program space, the kernel must either copy the entire message from process A's program space to process B's program space - a tremendous disadvantage for large messages, or provide some mechanism by which process B can access the message.
- Thread task switching time is faster, since a thread has less context to save than a process.
- With threads the kernel is linked in with the user code to create a single EXE. This means that all the kernel data structures like the ready queue are available for viewing with a debugger. This is not the case with a process, since the process is an autonomous application and the kernel is separate, which makes for a less flexible environment.
Thread Disadvantages
- Threads are typically not loadable. That is, to add a new thread, you must add the new thread to the source code, then compile and link to create the new executable. Processes are loadable, thus allowing a multi-tasking system to be characterized dynamically. For example, depending upon system conditions, certain processes can be loaded and run to characterize the system. However, the same can be accomplished with threads by linking in all the possible threads required by the system, but only activating those that are needed, given the conditions. The really big advantage of loadability is that the process concept allows processes (applications) to be developed by different companies and offered as tools to be loaded and used by others in their multi-tasking applications.
- Threads can walk over the data space of other threads. This cannot happen with processes. If an attempt is made to walk on another process an exception error will occur.
Threads are Best for Real-time Systems
For real-time systems, threads are generally the best choice for the following reasons:
- Threads are faster and more memory efficient.
- A thread based real-time kernel is smaller and more efficient.
- Real-time systems are typically single applications that require concurrency and robustness. This is consistent with the concept of threads.
- The fact that threads are typically not loadable is generally not an issue, nor is the sharing of program space between threads, in fact it is a decided advantage.
We welcome comments. Let us know what subjects you would like written up.
Send comments to Mike@TicsRealtime.com
Copyright © 2000, Tics Realtime
|