0% found this document useful (0 votes)
17 views

6 Windows Internals m6 Slides

This document discusses threads and thread scheduling in Windows. It covers the following key points: 1) Every user mode thread has two stacks - one in kernel space and one in user space, which can be configured differently. 2) Thread priorities range from 1 to 31, with priorities also affected by the process priority class. Higher priority threads preempt lower priority threads. 3) The scheduler uses priority-based preemptive time-slicing to determine which ready thread runs on the CPU. The time slice or "quantum" is typically 30ms for clients and 180ms for servers.

Uploaded by

Ron Tabibian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

6 Windows Internals m6 Slides

This document discusses threads and thread scheduling in Windows. It covers the following key points: 1) Every user mode thread has two stacks - one in kernel space and one in user space, which can be configured differently. 2) Thread priorities range from 1 to 31, with priorities also affected by the process priority class. Higher priority threads preempt lower priority threads. 3) The scheduler uses priority-based preemptive time-slicing to determine which ready thread runs on the CPU. The time slice or "quantum" is typically 30ms for clients and 180ms for servers.

Uploaded by

Ron Tabibian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Windows Internals

Module 6: Processes & Threads (Part 2)


Pavel Yosifovich
CTO, CodeValue
[email protected]
http://blogs.Microsoft.co.il/blogs/pavely
Thread Stacks

 Every user mode thread has two stacks


 In kernel space (12K (x86), 24K (x64))
 Resides in physical memory (most of the time)
 In user space (may be large)
 By default 1MB is reserved, 64KB committed
 A guard page is placed just below the last committed page, so that the stack can
grow
 Can change the initial size
 Using linker settings as new defaults
 On a thread by thread basis in the call to CreateThread / CreateRemoteThread(Ex)
 Can specify a new committed or reserved size, but not both
 Committed is assumed, unless the flag STACK_SIZE_PARAM_IS_A_RESERVATION is
used
Demo

Thread stacks
Thread Priorities

 Thread priorities are between 1 and 31 (31 being the highest)


 Priority 0 is reserved for the zero page thread
 The Windows API mandates thread priority be based on a process
priority class (base priority)
 A thread’s priority can be changed around the base priority
 APIs (Win32)
 SetPriorityClass – changing process base priority
 SetThreadPriority – change the thread priority offset from the parent’s
base priority
 API (kernel)
 KeSetPriorityThread – change thread priority to some absolute value
Thread Priorities (Win32 View)
Normal Priorities Realtime Priorities

Realtime Priority Class

High Priority Class

Above Normal Priority Class

Normal Priority Class

Below Normal Priority Class

Idle Priority Class

1 4 6 8 10 13 15 16 24 31
Priority
Demo

Thread Priorities
CPU
Ready
Running
priority
5
31

16

8 6 4

4 7

Waiting

888 2 1 3
Thread Scheduling (single processor)

 Priority based, preemptive, time-sliced


 Highest priority thread runs first
 If time slice (quantum) elapses, and there is another thread with the same
priority in the Ready state – it runs
 Otherwise, the same thread runs again
 If thread A runs, and thread B (with a higher priority) receives something it
waited upon (message, kernel object signaling, etc.), thread A is preempted
and thread B becomes the Running thread
 Voluntary switch
 A thread entering a wait state is dropped from the scheduler’s Ready list
 Typical time slice is 30 msec on client, 180 msec on server
 On an MP system with n logical processors, n concurrent threads
may be running
Thread States

Init (0)
Preemption,
Preempt quantum end

Ready, (1)
Deferred Standby (3) Running (2)
Ready (7)

Voluntary
switch
Transition (6)

Waiting (5)
Kernel
Terminate (4)
stack outswap
The Scheduler

 Scheduling routines are called when scheduling events occur


 Interval Timer interrupts checks for quantum end and timed wait
completion
 I/O Completion calls
 Changes in thread priority
 Changing state of waitable object other threads are waiting on
 Entering a wait on one or more objects
 Entering Sleep
Demo

Thread Scheduling
The Quantum

 Scheduler clock tick is typically


 10 msec (uniprocessor)
 15 msec (multiprocessor)
 Can determine with clockres.exe utility from SysInternals
 Default client quantum is 2 clock ticks
 Default server quantum is 12 clock ticks
 Quantum can be modified by using the registry or a Job
 Quantum boosting
 On a system configured for short, variable quantum
 The foreground process gets triple quantum
 For any process with a priority class above Idle
Quantum Control

 Registry key: HKLM\SYSTEM\CCS\Control\PriorityControl


 Value: Win32PrioritySeparation
4 2 0
Short vs. Long Variable vs. Fixed Foreground Priority Boost

 Short vs. Long


 1=long, 2=short
 0, 3=deafult (long for Server, short for Client)
 Variable vs. Fixed
 1=boost priority of foreground process, 2=don’t boost
 0, 3=default (boost for Client, don’t boost for Server)
 Foreground quantum boost
 Index into a table
Short Long
Variable 6 12 18 12 24 36
Fixed 18 18 18 36 36 36
Demo

Thread Quantum

You might also like