Foreground Application Handling in NT 4.0
Microsoft's new approach to prioritizing threads in NT 4.0 affects the performance of applications running in the background and the foreground.
May 31, 1997
Microsoft stretches quantums to balance performance
In the February issue, Trip Stiles passed along a rumor that unlike Windows NT Workstation 3.51,NT Workstation 4.0's responsiveness can appear to diminish when you run several concurrent tasks.The rumor pinned this behavior on NT 4.0's inability to adjust the process priority independently ofthe priority you assign to an application. Our alert readers quickly pointed out that NT Workstation4.0 uses a different method from NT Workstation 3.51 to optimize applications.In light of thisrevelation, I'd like to explain how NT Workstation 4.0 dispenses CPU time to foregroundapplications. Let's see how NT Workstation 4.0 schedules applications to run, how priority and CPUquantums fit into NT's process priority model, and what this approach means for foregroundapplications running in either NT Workstation or NT Server.
Processes and Threads 101
NT views applications in terms of processes and threads. A process consists ofan address space, a set of resources, and a set of threads that run in context of the process.Threads are the parts of an application that execute, and applications can have multiple threads.Each process has memory space separate from that of other processes, but all threads belonging to aprocess share the same set of resources.
NT assigns a priority to all processes and threads. Processes fall into one of four priorityclasses: realtime, high, normal, and idle. NT assigns each thread, which the application creates asone of the final steps of process creation, to 1 of 31 priority levels based on the thread's processpriority. (Although the priority levels are numbered from 0 to 31, NT does not use the level 0 forpriority user threads.) This priority might not be the same for all threads within a process. Youcan view the priority of a process and its threads with the Microsoft Windows NT Server 4.0Resource Kit Process Viewer tool, which you see in Screen 1.
Process Viewer divides process priority into three categories (Very High, Normal, Idle), as yousee in the Priority section in the middle right of the dialog box in Screen 1. More important tothis discussion is the thread Dynamic Priority setting, which you see in the Thread Informationsection at the bottom of the dialog box. All threads have two priorities: a starting priority and acurrent (dynamic) priority.
NT allocates CPU time to applications by individual threads within that application's process,making that allocation according to individual thread priority (the higher the priority levelnumber, the greater the thread priority). NT runs threads with the highest priority first. If morethan one thread with the same priority is ready to run at the same time, NT divides CPU time amongthose threads in timeslice or quantum units.
The length of a quantum depends on the system architecture (e.g., Intel systems have adifferent timeslice from Alpha systems) and the version of NT. For example, an NT Server-basedquantum is three times as long as the NT Workstation quantum on a given architecture. Microsoft madethe quantums' lengths different to accommodate the intended use of each platform: NT Workstation'sshorter quantum works better for a single user who's switching between applications, and NT Serverperforms better while providing services and data to an entire network. Microsoft designed NT sothat the CPU is idle as often as possible and designed the length of the quantum to give eachversion of the operating system a chance to finish one operation and begin another.
The Mechanics of Scheduling Threads in NT
NT is a priority-based, preemptive, multitasking operating system (i.e., it allocates CPU timein quantum units and dispenses these quantums to threads according to each thread's priority). Athread can get CPU time in three ways:
The thread that's running relinquishes the processor because it finishes its quantum
The thread that's running relinquishes the processor because it's waiting for another event totake place
A thread that was waiting for an event to take place becomes ready, or a thread with a higherpriority than a thread that's running becomes ready to run and preempts the running thread andinterrupts its quantum
Let's look at each of these scenarios. The first situation is easy to grasp: Thehighest-priority threads take turns using the CPU. When one thread completes its quantum, it movesto the back of the queue and awaits its next turn at the CPU (or if it's finished with its task, itterminates). This orderly process is similar to how a lunch line at a fast-food restaurant wouldwork in a perfect world.
But what happens if a thread isn't satisfied with a hamburger from the warming bin and orderssomething that takes extra time to prepare? In this situation, rather than hold up the line, thewaiting thread steps aside and lets the next thread in the queue place its order and getits lunch. The second thread doesn't preempt the first thread, but because the first threadis waiting for an event to take place (i.e., it's in a wait state), it relinquishes control of theprocessor, and the second thread is free to execute.
What happens when the first thread's special order is ready? At that point, assuming the threadthat's running doesn't have a higher priority than the thread that was in the wait state, thewaiting thread preempts the thread that was running. The waiting thread doesn't restart itstransaction with the cashier (CPU); it just finishes the existing transaction.
Finally, if a thread with a higher priority than a thread that's running is ready to execute,it can preempt the running thread. For example, imagine that Joe Thread is standing in line at thatfast-food restaurant and the President of the United States Thread walks in. The fact that JoeThread was already running doesn't matter because President Thread has a higher priority. WhenPresident Thread receives its order, Joe Thread is free to continue its cycle.
Foreground and Background Performance
So how do threads and quantums relate to program performance? The answer depends on the versionof NT you're running. Both NT 3.5x and NT 4.0 let you give foreground applications more CPU timethan background applications (NT assumes that you're most concerned with your foregroundapplications if you're using the desktop product), but each operating system takes a differentapproach. NT 3.5x Workstation increases the priority of threads running in the foreground so thatthey can bump other threads waiting for CPU time. In contrast, NT 4.0 Workstation increases thenumber of quantums that a foreground thread receives--by default, threads belonging to backgroundapplications get one quantum, and threads belonging to foreground applications get three quantums.The threads don't change priority, but when competing with other threads of the same priority, theyget to run a little longer than the average.
This change in design between NT 3.5x and NT 4.0 means that foreground applications might notrun as well under NT Workstation 4.0 as they did under NT Workstation 3.5x. Microsoft made thechange to improve background application performance, which can suffer when foreground applicationshog the CPU.
You can adjust your system settings so that foreground and background applications are morebalanced in NT Workstation 4.0: Open the System applet in the Control Panel and click thePerformance tab, as you see in Screen 2. For both NT Server 4.0 and NT Workstation 4.0, the defaultsetting, which you see in the Application Performance section of the dialog box in Screen 2, isthree quantums for foreground applications. You can move the slider to the center of the bar andreduce the quantum setting to two. Although NT Server has this control, by doing some research onthe NT scheduling code, I found that adjusting this setting doesn't affect the priority of aforeground application--NT Server threads always run at the same long quantum. Strange, but true.The reason this setting doesn't affect NT Server may be related to the fact that this operatingsystem is a server platform. Therefore, NT Server might assume that all applications (not just thosein the foreground) need the original percentages of CPU time.
If you're interested in finding this setting in the NT Workstation Registry, go toHKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlPriorityControlWin32PrioritySeparation. Thedefault value of this DWORD string is 0*2. As you can see, even though thread priority is no longerincreased for foreground apps, NT Workstation 4.0 includes provisions for increasing theirresponsiveness. Quantum stretching is not something you can easily see, like changing the priority,but it's there.
About the Author
You May Also Like