Windows进程调度相关

 结构体所在环境:

  Windows XP Version 2600 (Service Pack 3) UP Free x86 compatible

EPROCESS:

  1 ntdll!_EPROCESS
  2    +0x000 Pcb              : _KPROCESS
  3    +0x06c ProcessLock      : _EX_PUSH_LOCK
  4    +0x070 CreateTime       : _LARGE_INTEGER
  5    +0x078 ExitTime         : _LARGE_INTEGER
  6    +0x080 RundownProtect   : _EX_RUNDOWN_REF
  7    +0x084 UniqueProcessId  : Ptr32 Void
  8    +0x088 ActiveProcessLinks : _LIST_ENTRY
  9    +0x090 QuotaUsage       : [3] Uint4B
 10    +0x09c QuotaPeak        : [3] Uint4B
 11    +0x0a8 CommitCharge     : Uint4B
 12    +0x0ac PeakVirtualSize  : Uint4B
 13    +0x0b0 VirtualSize      : Uint4B
 14    +0x0b4 SessionProcessLinks : _LIST_ENTRY
 15    +0x0bc DebugPort        : Ptr32 Void
 16    +0x0c0 ExceptionPort    : Ptr32 Void
 17    +0x0c4 ObjectTable      : Ptr32 _HANDLE_TABLE
 18    +0x0c8 Token            : _EX_FAST_REF
 19    +0x0cc WorkingSetLock   : _FAST_MUTEX
 20    +0x0ec WorkingSetPage   : Uint4B
 21    +0x0f0 AddressCreationLock : _FAST_MUTEX
 22    +0x110 HyperSpaceLock   : Uint4B
 23    +0x114 ForkInProgress   : Ptr32 _ETHREAD
 24    +0x118 HardwareTrigger  : Uint4B
 25    +0x11c VadRoot          : Ptr32 Void
 26    +0x120 VadHint          : Ptr32 Void
 27    +0x124 CloneRoot        : Ptr32 Void
 28    +0x128 NumberOfPrivatePages : Uint4B
 29    +0x12c NumberOfLockedPages : Uint4B
 30    +0x130 Win32Process     : Ptr32 Void
 31    +0x134 Job              : Ptr32 _EJOB
 32    +0x138 SectionObject    : Ptr32 Void
 33    +0x13c SectionBaseAddress : Ptr32 Void
 34    +0x140 QuotaBlock       : Ptr32 _EPROCESS_QUOTA_BLOCK
 35    +0x144 WorkingSetWatch  : Ptr32 _PAGEFAULT_HISTORY
 36    +0x148 Win32WindowStation : Ptr32 Void
 37    +0x14c InheritedFromUniqueProcessId : Ptr32 Void
 38    +0x150 LdtInformation   : Ptr32 Void
 39    +0x154 VadFreeHint      : Ptr32 Void
 40    +0x158 VdmObjects       : Ptr32 Void
 41    +0x15c DeviceMap        : Ptr32 Void
 42    +0x160 PhysicalVadList  : _LIST_ENTRY
 43    +0x168 PageDirectoryPte : _HARDWARE_PTE_X86
 44    +0x168 Filler           : Uint8B
 45    +0x170 Session          : Ptr32 Void
 46    +0x174 ImageFileName    : [16] UChar
 47    +0x184 JobLinks         : _LIST_ENTRY
 48    +0x18c LockedPagesList  : Ptr32 Void
 49    +0x190 ThreadListHead   : _LIST_ENTRY
 50    +0x198 SecurityPort     : Ptr32 Void
 51    +0x19c PaeTop           : Ptr32 Void
 52    +0x1a0 ActiveThreads    : Uint4B
 53    +0x1a4 GrantedAccess    : Uint4B
 54    +0x1a8 DefaultHardErrorProcessing : Uint4B
 55    +0x1ac LastThreadExitStatus : Int4B
 56    +0x1b0 Peb              : Ptr32 _PEB
 57    +0x1b4 PrefetchTrace    : _EX_FAST_REF
 58    +0x1b8 ReadOperationCount : _LARGE_INTEGER
 59    +0x1c0 WriteOperationCount : _LARGE_INTEGER
 60    +0x1c8 OtherOperationCount : _LARGE_INTEGER
 61    +0x1d0 ReadTransferCount : _LARGE_INTEGER
 62    +0x1d8 WriteTransferCount : _LARGE_INTEGER
 63    +0x1e0 OtherTransferCount : _LARGE_INTEGER
 64    +0x1e8 CommitChargeLimit : Uint4B
 65    +0x1ec CommitChargePeak : Uint4B
 66    +0x1f0 AweInfo          : Ptr32 Void
 67    +0x1f4 SeAuditProcessCreationInfo : _SE_AUDIT_PROCESS_CREATION_INFO
 68    +0x1f8 Vm               : _MMSUPPORT
 69    +0x238 LastFaultCount   : Uint4B
 70    +0x23c ModifiedPageCount : Uint4B
 71    +0x240 NumberOfVads     : Uint4B
 72    +0x244 JobStatus        : Uint4B
 73    +0x248 Flags            : Uint4B
 74    +0x248 CreateReported   : Pos 0, 1 Bit
 75    +0x248 NoDebugInherit   : Pos 1, 1 Bit
 76    +0x248 ProcessExiting   : Pos 2, 1 Bit
 77    +0x248 ProcessDelete    : Pos 3, 1 Bit
 78    +0x248 Wow64SplitPages  : Pos 4, 1 Bit
 79    +0x248 VmDeleted        : Pos 5, 1 Bit
 80    +0x248 OutswapEnabled   : Pos 6, 1 Bit
 81    +0x248 Outswapped       : Pos 7, 1 Bit
 82    +0x248 ForkFailed       : Pos 8, 1 Bit
 83    +0x248 HasPhysicalVad   : Pos 9, 1 Bit
 84    +0x248 AddressSpaceInitialized : Pos 10, 2 Bits
 85    +0x248 SetTimerResolution : Pos 12, 1 Bit
 86    +0x248 BreakOnTermination : Pos 13, 1 Bit
 87    +0x248 SessionCreationUnderway : Pos 14, 1 Bit
 88    +0x248 WriteWatch       : Pos 15, 1 Bit
 89    +0x248 ProcessInSession : Pos 16, 1 Bit
 90    +0x248 OverrideAddressSpace : Pos 17, 1 Bit
 91    +0x248 HasAddressSpace  : Pos 18, 1 Bit
 92    +0x248 LaunchPrefetched : Pos 19, 1 Bit
 93    +0x248 InjectInpageErrors : Pos 20, 1 Bit
 94    +0x248 VmTopDown        : Pos 21, 1 Bit
 95    +0x248 Unused3          : Pos 22, 1 Bit
 96    +0x248 Unused4          : Pos 23, 1 Bit
 97    +0x248 VdmAllowed       : Pos 24, 1 Bit
 98    +0x248 Unused           : Pos 25, 5 Bits
 99    +0x248 Unused1          : Pos 30, 1 Bit
100    +0x248 Unused2          : Pos 31, 1 Bit
101    +0x24c ExitStatus       : Int4B
102    +0x250 NextPageColor    : Uint2B
103    +0x252 SubSystemMinorVersion : UChar
104    +0x253 SubSystemMajorVersion : UChar
105    +0x252 SubSystemVersion : Uint2B
106    +0x254 PriorityClass    : UChar
107    +0x255 WorkingSetAcquiredUnsafe : UChar
108    +0x258 Cookie           : Uint4B
View Code

KPROCESS:

 1 ntdll!_KPROCESS
 2    +0x000 Header           : _DISPATCHER_HEADER
 3    +0x010 ProfileListHead  : _LIST_ENTRY
 4    +0x018 DirectoryTableBase : [2] Uint4B
 5    +0x020 LdtDescriptor    : _KGDTENTRY
 6    +0x028 Int21Descriptor  : _KIDTENTRY
 7    +0x030 IopmOffset       : Uint2B
 8    +0x032 Iopl             : UChar
 9    +0x033 Unused           : UChar
10    +0x034 ActiveProcessors : Uint4B
11    +0x038 KernelTime       : Uint4B
12    +0x03c UserTime         : Uint4B
13    +0x040 ReadyListHead    : _LIST_ENTRY
14    +0x048 SwapListEntry    : _SINGLE_LIST_ENTRY
15    +0x04c VdmTrapcHandler  : Ptr32 Void
16    +0x050 ThreadListHead   : _LIST_ENTRY
17    +0x058 ProcessLock      : Uint4B
18    +0x05c Affinity         : Uint4B
19    +0x060 StackCount       : Uint2B
20    +0x062 BasePriority     : Char
21    +0x063 ThreadQuantum    : Char
22    +0x064 AutoAlignment    : UChar
23    +0x065 State            : UChar
24    +0x066 ThreadSeed       : UChar
25    +0x067 DisableBoost     : UChar
26    +0x068 PowerState       : UChar
27    +0x069 DisableQuantum   : UChar
28    +0x06a IdealNode        : UChar
29    +0x06b Flags            : _KEXECUTE_OPTIONS
30    +0x06b ExecuteOptions   : UChar
View Code

ETHREAD:

 1 ntdll!_ETHREAD
 2    +0x000 Tcb              : _KTHREAD
 3    +0x1c0 CreateTime       : _LARGE_INTEGER
 4    +0x1c0 NestedFaultCount : Pos 0, 2 Bits
 5    +0x1c0 ApcNeeded        : Pos 2, 1 Bit
 6    +0x1c8 ExitTime         : _LARGE_INTEGER
 7    +0x1c8 LpcReplyChain    : _LIST_ENTRY
 8    +0x1c8 KeyedWaitChain   : _LIST_ENTRY
 9    +0x1d0 ExitStatus       : Int4B
10    +0x1d0 OfsChain         : Ptr32 Void
11    +0x1d4 PostBlockList    : _LIST_ENTRY
12    +0x1dc TerminationPort  : Ptr32 _TERMINATION_PORT
13    +0x1dc ReaperLink       : Ptr32 _ETHREAD
14    +0x1dc KeyedWaitValue   : Ptr32 Void
15    +0x1e0 ActiveTimerListLock : Uint4B
16    +0x1e4 ActiveTimerListHead : _LIST_ENTRY
17    +0x1ec Cid              : _CLIENT_ID
18    +0x1f4 LpcReplySemaphore : _KSEMAPHORE
19    +0x1f4 KeyedWaitSemaphore : _KSEMAPHORE
20    +0x208 LpcReplyMessage  : Ptr32 Void
21    +0x208 LpcWaitingOnPort : Ptr32 Void
22    +0x20c ImpersonationInfo : Ptr32 _PS_IMPERSONATION_INFORMATION
23    +0x210 IrpList          : _LIST_ENTRY
24    +0x218 TopLevelIrp      : Uint4B
25    +0x21c DeviceToVerify   : Ptr32 _DEVICE_OBJECT
26    +0x220 ThreadsProcess   : Ptr32 _EPROCESS
27    +0x224 StartAddress     : Ptr32 Void
28    +0x228 Win32StartAddress : Ptr32 Void
29    +0x228 LpcReceivedMessageId : Uint4B
30    +0x22c ThreadListEntry  : _LIST_ENTRY
31    +0x234 RundownProtect   : _EX_RUNDOWN_REF
32    +0x238 ThreadLock       : _EX_PUSH_LOCK
33    +0x23c LpcReplyMessageId : Uint4B
34    +0x240 ReadClusterSize  : Uint4B
35    +0x244 GrantedAccess    : Uint4B
36    +0x248 CrossThreadFlags : Uint4B
37    +0x248 Terminated       : Pos 0, 1 Bit
38    +0x248 DeadThread       : Pos 1, 1 Bit
39    +0x248 HideFromDebugger : Pos 2, 1 Bit
40    +0x248 ActiveImpersonationInfo : Pos 3, 1 Bit
41    +0x248 SystemThread     : Pos 4, 1 Bit
42    +0x248 HardErrorsAreDisabled : Pos 5, 1 Bit
43    +0x248 BreakOnTermination : Pos 6, 1 Bit
44    +0x248 SkipCreationMsg  : Pos 7, 1 Bit
45    +0x248 SkipTerminationMsg : Pos 8, 1 Bit
46    +0x24c SameThreadPassiveFlags : Uint4B
47    +0x24c ActiveExWorker   : Pos 0, 1 Bit
48    +0x24c ExWorkerCanWaitUser : Pos 1, 1 Bit
49    +0x24c MemoryMaker      : Pos 2, 1 Bit
50    +0x250 SameThreadApcFlags : Uint4B
51    +0x250 LpcReceivedMsgIdValid : Pos 0, 1 Bit
52    +0x250 LpcExitThreadCalled : Pos 1, 1 Bit
53    +0x250 AddressSpaceOwner : Pos 2, 1 Bit
54    +0x254 ForwardClusterOnly : UChar
55    +0x255 DisablePageFaultClustering : UChar
View Code

KTHREAD:

 1 ntdll!_KTHREAD
 2    +0x000 Header           : _DISPATCHER_HEADER
 3    +0x010 MutantListHead   : _LIST_ENTRY
 4    +0x018 InitialStack     : Ptr32 Void
 5    +0x01c StackLimit       : Ptr32 Void
 6    +0x020 Teb              : Ptr32 Void
 7    +0x024 TlsArray         : Ptr32 Void
 8    +0x028 KernelStack      : Ptr32 Void
 9    +0x02c DebugActive      : UChar
10    +0x02d State            : UChar
11    +0x02e Alerted          : [2] UChar
12    +0x030 Iopl             : UChar
13    +0x031 NpxState         : UChar
14    +0x032 Saturation       : Char
15    +0x033 Priority         : Char
16    +0x034 ApcState         : _KAPC_STATE
17    +0x04c ContextSwitches  : Uint4B
18    +0x050 IdleSwapBlock    : UChar
19    +0x051 VdmSafe          : UChar
20    +0x052 Spare0           : [2] UChar
21    +0x054 WaitStatus       : Int4B
22    +0x058 WaitIrql         : UChar
23    +0x059 WaitMode         : Char
24    +0x05a WaitNext         : UChar
25    +0x05b WaitReason       : UChar
26    +0x05c WaitBlockList    : Ptr32 _KWAIT_BLOCK
27    +0x060 WaitListEntry    : _LIST_ENTRY
28    +0x060 SwapListEntry    : _SINGLE_LIST_ENTRY
29    +0x068 WaitTime         : Uint4B
30    +0x06c BasePriority     : Char
31    +0x06d DecrementCount   : UChar
32    +0x06e PriorityDecrement : Char
33    +0x06f Quantum          : Char
34    +0x070 WaitBlock        : [4] _KWAIT_BLOCK
35    +0x0d0 LegoData         : Ptr32 Void
36    +0x0d4 KernelApcDisable : Uint4B
37    +0x0d8 UserAffinity     : Uint4B
38    +0x0dc SystemAffinityActive : UChar
39    +0x0dd PowerState       : UChar
40    +0x0de NpxIrql          : UChar
41    +0x0df InitialNode      : UChar
42    +0x0e0 ServiceTable     : Ptr32 Void
43    +0x0e4 Queue            : Ptr32 _KQUEUE
44    +0x0e8 ApcQueueLock     : Uint4B
45    +0x0f0 Timer            : _KTIMER
46    +0x118 QueueListEntry   : _LIST_ENTRY
47    +0x120 SoftAffinity     : Uint4B
48    +0x124 Affinity         : Uint4B
49    +0x128 Preempted        : UChar
50    +0x129 ProcessReadyQueue : UChar
51    +0x12a KernelStackResident : UChar
52    +0x12b NextProcessor    : UChar
53    +0x12c CallbackStack    : Ptr32 Void
54    +0x130 Win32Thread      : Ptr32 Void
55    +0x134 TrapFrame        : Ptr32 _KTRAP_FRAME
56    +0x138 ApcStatePointer  : [2] Ptr32 _KAPC_STATE
57    +0x140 PreviousMode     : Char
58    +0x141 EnableStackSwap  : UChar
59    +0x142 LargeStack       : UChar
60    +0x143 ResourceIndex    : UChar
61    +0x144 KernelTime       : Uint4B
62    +0x148 UserTime         : Uint4B
63    +0x14c SavedApcState    : _KAPC_STATE
64    +0x164 Alertable        : UChar
65    +0x165 ApcStateIndex    : UChar
66    +0x166 ApcQueueable     : UChar
67    +0x167 AutoAlignment    : UChar
68    +0x168 StackBase        : Ptr32 Void
69    +0x16c SuspendApc       : _KAPC
70    +0x19c SuspendSemaphore : _KSEMAPHORE
71    +0x1b0 ThreadListEntry  : _LIST_ENTRY
72    +0x1b8 FreezeCount      : Char
73    +0x1b9 SuspendCount     : Char
74    +0x1ba IdealProcessor   : UChar
75    +0x1bb DisableBoost     : UChar
View Code

线程优先级:

  Windows 把线程分为32个优先级,

  优先级分3类:

    1:优先级为0,0页优先级,有且仅有一个线程处于当前优先级,当前优先级给内存管理器来释放分页内存。

    2:优先级1-15,动态调整优先级。

    3:16-31,实时优先级。

  每个线程都有两个优先级信息,一个优先级叫做基本优先级,这个基本优先级是从当前线程所在进程继承下来的。

                  KTHREAD 结构的 BasePriority 成员记录了基本优先级。

                另一个优先级叫做当前优先级,当前优先级是线程在每个时间片里运行时的优先级。

                  KTHREAD 结构的 Priority 成员记录了当前优先级。

  如果当前线程运行在动态优先级中,那么它的当前优先级会以基本优先级为基础,上下有一定的浮动调整,但是无论怎么调整,它的值不会超出1-15这个范围。

与进程调度相关的是 KTHREAD 的名叫 State 的成员,当前成员表示线程当前状态,在WRK中定义成如下,分别有不同的含义

 1 typedef enum _KTHREAD_STATE {
 2     Initialized,   //  线程已经初始化完成,但是没有加入进程的线程列表
 3     Ready,      //  线程准备就绪,等待被执行
 4     Running,     //  线程正在运行
 5     Standby,     //  线程处于备用状态,被选中,作为某处理器上下一个要被执行的线程
 6     Terminated,   //  线程已经结束,正在回收资源
 7     Waiting,     //  线程处于等待状态,等待某个条件
 8     Transition,   //  处于转移状态的线程已经准备好了
 9     DeferredReady, //  处于延迟就绪状态的线程已经准备好了,它与就绪的区别是没有选择处理器
10     GateWait     //  线程正在等待一个门对象
11 } KTHREAD_STATE;

相关的状态转换图如下(网上找的)

一般来说,线程处于动态调整优先级的时候,线程的当前优先级都是有可能被调整的,但是大多数情况下,都是优先级被提升,

常见的可直接操作提升线程优先级的方法如下:

  1:当一个IO操作结束时,等待这个IO操作的线程一般来说都是要及时响应回复的,所以这里有可能被先执行,具体其实可以看到

    IoCompleteRequest WDK中,此函数有两个参数,第一个参数为IO请求包,第二个参数就是优先级的相关的参数设置。详细信息 MSDN 里面有明确的解释。

  2:当一个Event或者其他可等待的OBJECT或者HANDLE被触发之后,等待它的线程可能被优先运行,具体可以看到

    KeSetEvent WDK中,此函数三个参数中,第二个参数的含义就是设置的优先级增量。

还有其他的多种情况都可以提高一个线程的优先级,此处只列举两点。

线程状态切换的大致流程为,

当线程初始化结束之后,调用过 KeInitThread 函数之后,线程状态被设置为 Initialized

KiReadyThread 、 KiAttachProcess 、KxQueueReadyThread 三个函数会让线程状态变成 Ready

其中最重要的是KiReadyThread函数,这个函数的调用意味着线程已经进入Ready状态,可以被调度了

当前函数在以下几种情况中会被调用:

  1:当一个线程被解除等待的时候,也就是调用 KiUnwaitThread 函数的时候,最后调用了一次当前函数,详见 WRK。

  2:插入内核队列,并且满足Ready条件之后,调用 KiInsertQueue 之后,详见 WRK。

  3:当附加到指定的线程之后,并且满足Ready条件,调用 KiAttachProcess 之后,详见WRK。

  4:调用 KiInSwapProcesses 函数,换入进程链表之后,WRK。

  5:调用 KiOutSwapProcesses 函数,换出进程链表之后,WRK。

  6:调用 KeSetEventBoostPriority 函数内部,WRK。

  7:最特殊的一处,就是CreateThread的底层函数PspCreateThread会在自己内部通过调用KeReadyThread来间接调用KiReadyThread。

至于 Running 状态没有那么复杂,只是在几个关键的执行位置有置状态

  1:最重要的就是 KiSwapThread ,这个函数,算是个传奇函数了,用赵本山的话说,操作系统这辈子就指着它活着呢。

    内部切换线程之后,新的线程就准备运行了,所以这里是肯定要给新线程置位的。

  2:退出调度器函数 KiExitDispatcher ,当线程进入调度器之后,退出来的时候,需要恢复自己以前的各种状态,然后再让自己继续执行,

    所以这个函数内部有必要恢复线程自己的状态。

  3:线程自己放弃时间片,让其他线程来运行,有点像协作式多任务的切换方法了。NtYieldExecution 就是这个函数。

    自己内部放弃,就必然有个其他地方拿到自己放弃的东西,或者根本没有人在等待拿东西。

  4:自己时间片用完了之后,调用 KiQuantumEnd 这个函数去找优先级高的线程,然后运行。

    找到了之后,新的线程就继续跑,旧的线程就停下了。

当当前线程的时间片正常结束时,调度器会抢占当前线程的状态,然后把CPU资源让给同样优先级,或者优先级更高的线程。

至于线程的切换过程,可以直接看 SwapContext 。

WRK里面,代码在Ctxswap.asm文件里面,汇编写的,太晚了,不看了,有空再看。

至于其他的状态重要性没有那么高,我也没兴趣一个一个点找完剩下的。

相关文档:https://msdn.microsoft.com/en-us/library/ms810029.aspx

WRK1.2

baidu图片搜索

原文地址:https://www.cnblogs.com/suanguade/p/6736710.html