26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The kernel’s role<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

The CPU has a number of registers (the exact number depends on the processor<br />

family, e.g., x86 versus MIPS, and the specific family member, e.g., 80486 versus<br />

Pentium). When the thread is running, information is stored in those registers (e.g., the<br />

current program location).<br />

When the kernel decides that another thread should run, it needs to:<br />

1 save the currently running thread’s registers and other context information<br />

2 load the new thread’s registers and context into the CPU<br />

But how does the kernel decide that another thread should run? It looks at whether or<br />

not a particular thread is capable of using the CPU at this point. When we talked about<br />

mutexes, for example, we introduced a blocking state (this occurred when one thread<br />

owned the mutex, and another thread wanted to acquire it as well; the second thread<br />

would be blocked).<br />

From the kernel’s perspective, therefore, we have one thread that can consume CPU,<br />

and one that can’t, because it’s blocked, waiting for a mutex. In this case, the kernel<br />

lets the thread that can run consume CPU, and puts the other thread into an internal list<br />

(so that the kernel can track its request for the mutex).<br />

Obviously, that’s not a very interesting situation. Suppose that a number of threads can<br />

use the CPU. Remember that we delegated access to the mutex based on priority and<br />

length of wait? The kernel uses a similar scheme to determine which thread is going to<br />

run next. There are two factors: priority and scheduling algorithm, evaluated in that<br />

order.<br />

Prioritization<br />

Consider two threads capable of using the CPU. If these threads have different<br />

priorities, then the answer is really quite simple — the kernel gives the CPU to the<br />

highest priority thread. <strong>Neutrino</strong>’s priorities go from one (the lowest usable) and up, as<br />

we mentioned when we talked about obtaining mutexes. Note that priority zero is<br />

reserved for the idle thread — you can’t use it. (If you want to know the minimum and<br />

maximum values for your system, use the functions sched_get_priority_min() and<br />

sched_get_priority_max() — they’re prototyped in . In this book, we’ll<br />

assume one as the lowest usable, and 255 as the highest.)<br />

If another thread <strong>with</strong> a higher priority suddenly becomes able to use the CPU, the<br />

kernel will immediately context-switch to the higher priority thread. We call this<br />

preemption — the higher-priority thread preempted the lower-priority thread. When<br />

the higher-priority thread is done, and the kernel context-switches back to the<br />

lower-priority thread that was running before, we call this resumption — the kernel<br />

resumes running the previous thread.<br />

Now, suppose that two threads are capable of using the CPU and have the exact same<br />

priority.<br />

20 Chapter 1 • Processes and Threads April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!