Introduction to Threads and Processes

Programs consist of a number of processes, each of which contains one or more conceptually concurrent threads of execution.

A thread is the unit of execution within a process. Every time a process is initialized, a primary thread is created. For many applications the primary thread is the only one that the application requires; however, processes can create additional threads.

Each user process has its own private address space, i.e. a collection of memory regions which that process can access. A user process cannot directly address memory areas in the address space of another process. There is also a special process, the Kernel process, whose threads run at supervisor privilege level. This process normally contains two threads:

  • the Kernel server thread, which is the initial thread whose execution begins at the reset vector, and which is used to implement all Kernel functions requiring allocation or de-allocation on the Kernel heap. This is the highest priority thread in the system.

  • the null thread, which runs only when no other threads are ready to run. The null thread places the processor into idle mode to save power.

Threads execute individually and are unaware of other threads in a process. The scheduling of threads is pre-emptive, i.e. a currently executing thread may be suspended at any time to allow another thread to run.

Each thread is assigned a priority; at any time, the thread running is the highest priority thread which is ready to run. Threads with equal priority are time-sliced on a round-robin basis. Context switching between threads involves saving and restoring the state of threads. This state includes not only the processor registers (the thread context) but also the address space accessible to the thread (the process context). The process context only needs switching if a reschedule is between threads in different processes.

Compare this with active objects which allow non pre-emptive multi-tasking within a single thread.

A thread can suspend, resume, panic and kill another thread.

When a thread is created it is put into a suspended state, it does not begin to run until that thread’s Resume() member function is called.

When a thread is created, it is given the priority EPriorityNormal by default. The fact that a thread is initially put into a suspended state means that the thread priority can be changed (RThread::SetPriority()) before the thread is started (RThread::Resume()).