View
287
Download
3
Category
Preview:
Citation preview
计算机系•信息处理实验室
Lecture 8 Processes, Threads, and Jobs (2)
xlanchen@04/08/2005
xlanchen@04/08/2005 Understanding the Inside of Windows2000
2计算机系信息处理实验室
Contents
The internal structures of process
How to create a process
The internal structures of thread
How to create a thread
Thread Scheduling
Job Objects
xlanchen@04/08/2005 Understanding the Inside of Windows2000
3计算机系信息处理实验室
Thread structures
a Windows 2000 thread is represented by ETHREAD block
in the system address spaceexcept TEB
How is the thread related to Csrss and Win32k.sys
xlanchen@04/08/2005 Understanding the Inside of Windows2000
4计算机系信息处理实验室
Fibers vs. Threads
Fibers
"lightweight" threads
implemented in user mode in Kernel32.dll
allow an App. to schedule its own "threads" outside the priority-based scheduling mechanism of 2K
ConvertThreadToFiber, win32 function
CreateFiber
SwitchToFiber
xlanchen@04/08/2005 Understanding the Inside of Windows2000
5计算机系信息处理实验室
Structure of the kernel thread block
xlanchen@04/08/2005 Understanding the Inside of Windows2000
6计算机系信息处理实验室
EXPERIMENT
Displaying ETHREAD and KTHREAD Structures
xlanchen@04/08/2005 Understanding the Inside of Windows2000
7计算机系信息处理实验室
Fields of the TEB
xlanchen@04/08/2005 Understanding the Inside of Windows2000
8计算机系信息处理实验室
EXPERIMENT
Examining the TEB
xlanchen@04/08/2005 Understanding the Inside of Windows2000
9计算机系信息处理实验室
Related Kernel Variables
PspCreateThreadNotifyRoutine
PspCreateThreadNotifyRoutineCount
PspCreateProcessNotifyRoutine
PspCreateProcessNotifyRoutineCount
PspCidTable
xlanchen@04/08/2005 Understanding the Inside of Windows2000
10计算机系信息处理实验室
Related Performance Counters
Process:
Priority Base
Thread:
% Privileged Time
% Processor Time
% User Time
Context Switches/Sec
Elapsed Time
ID Process
ID Thread
Priority Base
Priority Current
Start Address
Thread State
Thread Wait Reason
xlanchen@04/08/2005 Understanding the Inside of Windows2000
11计算机系信息处理实验室
Win32 Thread Functions
CreateThread
CreateRemoteThread
ExitThread
TerminateThread
GetExitCodeThread
GetThreadTimes
Get/SetThreadContext
GetThreadSelectorEntry
xlanchen@04/08/2005 Understanding the Inside of Windows2000
12计算机系信息处理实验室
EXPERIMENT
Using the Kernel Debugger !thread Command
xlanchen@04/08/2005 Understanding the Inside of Windows2000
13计算机系信息处理实验室
EXPERIMENT
Viewing Thread Information
xlanchen@04/08/2005 Understanding the Inside of Windows2000
14计算机系信息处理实验室
Create a thread
CreateThread to create a Win32 thread
In Kernel32.dll
1. Creates a user-mode stack
2. Initializes the thread's hardware context
3. Calles NtCreateThread to create the executive thread object in the suspended state
4. Notifies the Win32 subsystem about the new thread
5. Return the thread handle and its ID to the caller
xlanchen@04/08/2005 Understanding the Inside of Windows2000
15计算机系信息处理实验室
the thread starts running
KiThreadStartup
PspUserThreadStartup
LdrInitializeThunk (in Ntdll.dll)
xlanchen@04/08/2005 Understanding the Inside of Windows2000
16计算机系信息处理实验室
Flow of CreateThread
xlanchen@04/08/2005 Understanding the Inside of Windows2000
17计算机系信息处理实验室
2K Scheduling priority-driven, preemptive
the highest-priority runnable (ready) thread always runs,
processor affinity
quantum
the length of time a thread is allowed to run before Windows 2000 interrupts the thread to find out whether another thread at the same priority level or higher is waiting to run or whether the thread's priority needs to be reduced
xlanchen@04/08/2005 Understanding the Inside of Windows2000
18计算机系信息处理实验室
EXPERIMENT
Viewing Ready Threads
xlanchen@04/08/2005 Understanding the Inside of Windows2000
19计算机系信息处理实验室
EXPERIMENT
Thread-Scheduling State Changes
xlanchen@04/08/2005 Understanding the Inside of Windows2000
20计算机系信息处理实验室
Thread priority levels
xlanchen@04/08/2005 Understanding the Inside of Windows2000
21计算机系信息处理实验室
Kernel prioritiesWin32 vs. Windows 2000
xlanchen@04/08/2005 Understanding the Inside of Windows2000
22计算机系信息处理实验室
kernel's dispatcher
dispatching occurs at DPC/dispatch level and is triggered by any of the following events
A thread becomes ready to execute
A thread leaves the running state
because its time quantum ends, it terminates, or it enters a wait state
A thread's priority changes
The processor affinity of a running thread changes.
xlanchen@04/08/2005 Understanding the Inside of Windows2000
23计算机系信息处理实验室
Win32 Scheduling APIs
Suspend/ResumeThread
Suspends or resumes a paused thread from execution.
Get/SetPriorityClass
Returns or sets a process's priority class (base priority).
Get/SetThreadPriority
Returns or sets a thread's priority (relative to its process base priority).
xlanchen@04/08/2005 Understanding the Inside of Windows2000
24计算机系信息处理实验室
Get/SetProcessAffinityMask
Returns or sets a process's affinity mask.
SetThreadAffinityMask
Sets a thread's affinity mask (must be a subset of the process's affinity mask) for a particular set of processors, restricting it to running on those processors.
xlanchen@04/08/2005 Understanding the Inside of Windows2000
25计算机系信息处理实验室
Get/SetThreadPriorityBoost
Returns or sets the ability for Windows 2000 to boost the priority of a thread temporarily (applies only to threads in the dynamic range).
SetThreadIdealProcessor
Establishes a preferred processor for a particular thread but doesn't restrict the thread to that processor.
Get/SetProcessPriorityBoost
Returns or sets the default priority boost control state of the current process. (This function is used to set the thread priority boost control state when a thread is created.)
xlanchen@04/08/2005 Understanding the Inside of Windows2000
26计算机系信息处理实验室
SwitchToThread
Yields execution for one or more quantums.
Sleep
Puts the current thread into a wait state for a specified time interval (figured in milliseconds [msec]). A zero value relinquishes the rest of the thread's quantum.
SleepEx
Causes the current thread to go into a wait state until either an I/O completion callback is completed, an APC is queued to the thread, or the specified time interval ends.
xlanchen@04/08/2005 Understanding the Inside of Windows2000
27计算机系信息处理实验室
EXPERIMENT
Examining and Specifying Process and Thread Priorities
xlanchen@04/08/2005 Understanding the Inside of Windows2000
28计算机系信息处理实验室
Real-Time Priorities
Increase scheduling priority privilege
2K is not a true real-time operating system
xlanchen@04/08/2005 Understanding the Inside of Windows2000
29计算机系信息处理实验室
Interrupt Levels vs. Priority Levels
xlanchen@04/08/2005 Understanding the Inside of Windows2000
30计算机系信息处理实验室
Thread states
xlanchen@04/08/2005 Understanding the Inside of Windows2000
31计算机系信息处理实验室
Quantum
Quantum Accounting
Default: 6
Per clock interrupt: -3 ???
The clock interval is determined by hardware platform
Controlling the Quantum
xlanchen@04/08/2005 Understanding the Inside of Windows2000
32计算机系信息处理实验室
EXPERIMENT
Determining the Clock Interval Frequency
xlanchen@04/08/2005 Understanding the Inside of Windows2000
33计算机系信息处理实验室
Scheduling Data Structures
xlanchen@04/08/2005 Understanding the Inside of Windows2000
34计算机系信息处理实验室
Scheduling Scenarios
Voluntary Switch
Preemption
Quantum End
Termination
xlanchen@04/08/2005 Understanding the Inside of Windows2000
35计算机系信息处理实验室
Voluntary switching
xlanchen@04/08/2005 Understanding the Inside of Windows2000
36计算机系信息处理实验室
Preemptive thread scheduling
xlanchen@04/08/2005 Understanding the Inside of Windows2000
37计算机系信息处理实验室
Quantum End
xlanchen@04/08/2005 Understanding the Inside of Windows2000
38计算机系信息处理实验室
Context Switching
A thread's context and the procedure for context switching vary depending on the processor's architecture
A typical context switch requires saving and reloading the following data:
Program counter
Processor status register
Other register contents
User and kernel stack pointers
A pointer to the address space in which the thread runs (the process's page table directory)
xlanchen@04/08/2005 Understanding the Inside of Windows2000
39计算机系信息处理实验室
Idle Thread Dispatched when no runnable thread exists on a CPU
Each CPU is allotted one idle thread
the basic flow
Enables and disables interrupts (allowing any pending interrupts to be delivered).
Checks whether any DPCs are pending on the processor. If DPCs are pending, clears the pending software interrupt and delivers them.
Checks whether a thread has been selected to run next on the processor, and if so, dispatches that thread.
Calls the HAL processor idle routine (in case any power management functions need to be performed).
xlanchen@04/08/2005 Understanding the Inside of Windows2000
40计算机系信息处理实验室
Priority Boosts
In five cases, Windows 2000 can boost (increase) the current priority value of threads
On completion of I/O operations
After waiting on executive events or semaphores
After threads in the foreground process complete a wait operation
When GUI threads wake up because of windowing activity
When a thread that's ready to run hasn't been running for some time (CPU starvation)
xlanchen@04/08/2005 Understanding the Inside of Windows2000
41计算机系信息处理实验室
Priority boosting and decay
xlanchen@04/08/2005 Understanding the Inside of Windows2000
42计算机系信息处理实验室
EXPERIMENT
Watching Foreground Priority Boosts and Decays
xlanchen@04/08/2005 Understanding the Inside of Windows2000
43计算机系信息处理实验室
EXPERIMENT
Watching Priority Boosts on GUI Threads
xlanchen@04/08/2005 Understanding the Inside of Windows2000
44计算机系信息处理实验室
EXPERIMENT
Watching Priority Boosts for CPU Starvation
xlanchen@04/08/2005 Understanding the Inside of Windows2000
45计算机系信息处理实验室
Scheduling on SMP Systems
Affinity
Ideal and Last Processor
Choosing a Processor for a Ready Thread
Selecting a Thread to Run on a Specific CPU
When the Highest-Priority Ready Threads Are Not Running ?
xlanchen@04/08/2005 Understanding the Inside of Windows2000
46计算机系信息处理实验室
Job Objects
A job object is a nameable, securable, shareable kernel object that allows control of one or more processes as a group.
basic function
to allow groups of processes to be managed and manipulated as a unit
xlanchen@04/08/2005 Understanding the Inside of Windows2000
47计算机系信息处理实验室
A process can be a member of only one job object
Win32 API Functions for Jobs
CreateJobObject
OpenJobObject
AssignProcessToJobObject
TerminateJobObject
SetInformationJobObject
QueryInformationJobObject
xlanchen@04/08/2005 Understanding the Inside of Windows2000
48计算机系信息处理实验室
EXPERIMENT
Viewing the Job Object
Recommended