美文网首页
Windows Internals - Chapter 4 Th

Windows Internals - Chapter 4 Th

作者: 我是柯南 | 来源:发表于2018-01-12 15:59 被阅读56次

线程

主要探讨Windows中线程和线程调度的基础数据结构和算法

进程和线程的关系

ThreadDiagram.png
  • 进程是计算机资源分配的基本单位,进程包括:程序,数据和堆栈
  • 线程是计算机运行和独立调度的基本单位
  • 进程都有自己的独立地址空间,一个进程的崩溃不影响其他进程的运行,而线程共享进程的地址空间,一个线程的崩溃会导致整个进程的崩溃
  • 一个进程至少有一个线程
  • 程序是一个静态的概念
  • 进程是程序的一次执行

创建线程

Windows API 创建线程 (Kernel32.dll)

  • CreateRemoteThreadEx
  • CreateRemoteThread
  • CreateThread
  • PsCreateSystemThread (create thread in kernel model)
HANDLE WINAPI CreateThread(
  _In_opt_  LPSECURITY_ATTRIBUTES  lpThreadAttributes,
  _In_      SIZE_T                 dwStackSize,
  _In_      LPTHREAD_START_ROUTINE lpStartAddress,
  _In_opt_  LPVOID                 lpParameter,
  _In_      DWORD                  dwCreationFlags,
  _Out_opt_ LPDWORD                lpThreadId
);
  • 安全属性结构体 决定创建后的线程句柄是否可被继承,以及该线程的可访问性
  • 线程堆栈大小(默认1MB,进程中可创建的线程数是受到进程虚拟内存(32位操作系统 2GB)的限制,如果使用默认堆栈大小,那么32位操作系统单个进程中最多能创建2048个线程)
  • 入口函数地址,线程执行入口地址
  • 函数参数,该入口函数使用的参数
  • 线程创建标志 如果设置为:0,则创建后立刻执行,一般创建函数还没有返回,线程已经开始执行了。如果设置为:CREATE_SUSPENDED(0x00000004)则创建完成后处于挂起状态,通过调用 ResumeThread 函数来启动线程的执行。
  • 线程ID输出参数,如果需要得到线程ID,通过该输出变量获取

线程数据结构细节

Excutive Thread

The ETHREAD structure and the other structures it points to exist in the system
address space. The only exception is the thread environment block (TEB),
which exists in the process address space (similar to a PEB, because usermode
components need to access it).

image.png

Kernel Thread

image.png

查看线程状态


使用 Process Explorer (System internals)工具可以查看进程中线程的状态,以下是微信进程的线程状态


image.png

线程调度


Windows 实现的是基于 priority-driven preemptive 的线程调度系统,至少有一个最高优先级的线程一直在运行。

Priority levels

Windows 内部使用0-31共32级来定义线程优先级


image.png
  • 16 - 31 定义为实时级别
  • 0 - 15 定义为可变级别,其中 0 保留给 zero page thread


    image.png
image.png

Quantum

线程运行的时间片,默认情况下,桌面系统默认值为2个时钟周期,服务器系统默认为12个时钟周期

Thread states

  • Deferred Ready
  • 每个cpu只有一个处于standby状态的线程,这个状态的线程同样可以被高优先级的线程抢占


    image.png

Dispatcher database

  • 记录当前哪些线程正在执行
  • 记录处理器正在执行那些进程
image.png

Priority boosts

  • Windows 线程调度会动态调整线程优先级,这个机制叫做: priority-boosting mechanism
  • 目的是为了减少延迟和增加响应性

Context switching

线程上线文数据和上下文切换的过程跟处理器架构密切相关,典型的上下文包括:

  • instruction pointer
  • kernel stack pointer
  • a pointer to the address space(process's page table)

Direct Switch
windows 8 and server 2012 引入的一个优化机制

Scheduling Scenario

image.png image.png image.png

Idle threads

当CPU没有线程可执行时,就会执行idle线程,每个CPU都有自己的idle线程

Thread suspension

  • API SuspendThread, ResumeThread

(Deep) freeze

让进程进入挂起状态,而且不能通过调用API ResumeThread来改变状态,比如当系统需要挂起UWP应用的时候(该应用进入后台),系统给该进程5秒钟的时间来处理应用状态的保存。

Deep freeze 实在上面的基础上再加一条约束,就是该应用中新创建的线程也不能运行。

这两个功能不是直接面向user mode的,是由 Process State Manager Sevice 负责处理的。

Thread selection

Heterogeneous Scheduling(bit.LITTLE)

  • SMP 对称对处理器
  • 非对称处理器 ARM big.LITTLE includes powerful cores (consume more energy) and weaker cores。

Group-based scheduling

  • 解决的什么问题?
    前面介绍的通用调度实现的是公平分享CPU,不区分用户,终端,调度只依赖于线程优先级,带来的问题就是,当有些用户使用密集计算的应用时,占用了更多的CPU资源,对于其他用户来说显然就是不公平了。
image.png

Dynamic fair share scheduling

Dynamic fair share scheduling (DFSS) is a mechanism that can be used to
fairly distribute CPU time among sessions running on a machine. It prevents
one session from potentially monopolizing the CPU if some threads running
under that session have a relatively high priority and run a lot. It’s enabled by
default on a Windows Server system that has the Remote Desktop role.

CPU rate limites

Dynamic processor addition and replacement

线程池

API

  • CreateTimerQueue
  • CreateTImerQueueTImer
  • QueueUserWorkItem

相关文章

网友评论

      本文标题:Windows Internals - Chapter 4 Th

      本文链接:https://www.haomeiwen.com/subject/zkymnxtx.html