Tasks, Threads, and Processes

Tasks , Threads, and Processes

A thread is one of potentially several tasks that run within the context of a single process. A process is one of potentially several programs that run on a platform whose operating system can take advantage of memory management unit (MMU). It is important to note the need for an MMU. An MMU is a hardware extension in the more powerful CPUs that allows a program to run in protected memory space.

Actually, a task and a thread are essentially the same thing. We usually think of tasks in embedded systems and threads in larger machines.

The protection afforded by the MMU is what distinguishes a process from a thread. Unlike a thread, no matter how confused a process gets, it cannot corrupt the memory space of another process. Processes are safe from each other because of the protection provided by the MMU. If the currently running process has a stray pointer that starts trashing memory, it can trash its own space, but it causes some type of MMU- related OS exception before it is able to access the memory space of another process.

You might be wondering why multi-threading (or multi-tasking) is even necessary in an MMU-based multi-processing environment. With the protection provided by the MMU comes a lot of overhead at context-switch time. Recall that I said before that the context of the currently running task is the state of the CPU. The context includes the register set and a few other task-specific variables . With a process, the context is all of that plus the overhead of reconfiguring the MMU and possibly pulling the incoming process back into RAM from secondary storage.

This overhead can impose a surprisingly high cost, because seemingly innocuous transactions can cause a full process switch. For example, because one process cannot touch the memory space of another process, the only way for one process to talk to another process is through some type of message-passing system call a call that also causes a context switch.

Because of the extra overhead associated with full processes, threads are the preferred multi-tasking choice for the real-time environment. The context switch overhead is minimal, and data sharing between threads is much less complex. Running within a thread, however, has its dangers also. With multiple threads running within the same block of memory space, a bad pointer or stack overflow in one thread has the ability to trash memory in any of the other threads. As we have discussed in an earlier chapter, finding such a bug can sometimes be very challenging.



Embedded Systems Firmware Demystified
Embedded Systems Firmware Demystified (With CD-ROM)
ISBN: 1578200997
EAN: 2147483647
Year: 2002
Pages: 118
Authors: Ed Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net