Memory management - multi-threading technology

Multithreading

Single-threaded process structure

Traditional single-threaded process is a process structure

Single-threaded process of structural problems:

Structure in a single-threaded process concurrent programming problems, the process switching overhead, big process communication overhead, limiting the size of the concurrent process, reducing the efficiency of the parallel computing

Problem-solving ideas:

The process of the two functions, namely "independent resource allocation" and "assignment scheduled to perform" basic unit separated from the process as a system resource allocation and protection of independent units, no need to switch frequently, as the system thread scheduling and dispatching of can run lightly armed, are frequently scheduled and switching, appear threads will reduce the cost of space-time paid concurrently executing processes, making concurrent finer granularity, concurrency better

Multi-threaded architecture process

Multi-threaded environment process concepts:

In a multithreaded environment, a process is an independent unit of resource allocation and protection of the operating system. It has: access protection mechanisms to accommodate the process image of the virtual address space of processes, files and equipment

A thread is a path of execution process, is the basic unit of scheduling, main memory space and resources obtained with the process all threads share a process. It includes: a thread execution state, protected thread context, when the thread is not running, for storing site information, independent program counter instruction, the execution stack, local variable to accommodate static memory

Threaded multi-threaded environment state and Scheduling

Thread states are running, ready and sleep, not hang about the state of the thread and the thread change operation are: incubation, blockade, activation, deprivation, assign, end-threaded environment under OS perception: the object is scheduled processor threads, processes not under the three-state (or suspend state only), OS is not aware threaded environment: the object is still the processor scheduling process, the user scheduler in user space thread scheduling

Concurrent advantage of multi-threaded programming: fast thread switching, reducing the (system) management overhead (thread) communication is easy to implement and improve the degree of parallelism, save memory space

Application of Multithreading: the foreground and background work, C / S mode applications, faster execution, the user interface design

ULT and KLT

Kernel-level threads KLT, Kernel-LevelThreads

All the work done by the thread management of the OS kernel, OS provides an application programming interface API, for developers to use KLT, OS scheduling KLT direct user space kernel space P P

The characteristics of kernel-level threads

进程中的一个线程被阻塞了,内核能调度同一进程的其它线程占有处理器运行,多处理器环境中,内核能同时调度同一进程中多个线程并行执行,内核自身也可用多线程技术实现,能提高操作系统的执行速度和效率,应用程序线程在用户态运行,线程调度和管理在内核实现,在同一进程中,控制权从一个线程传送到另一个线程时需要模式切换,系统开销较大

用户级线程ULT,User-LevelThreads

用户空间运行的线程库,提供多线程应用程序的开发和运行支撑环境,任何应用程序均需通过线程库进行程序设计,再与线程库连接后运行,线程管理的所有工作都由应用程序完成,内核没有意识到线程的存在P用户空间线程库内核空间

用户级线程的特点

所有线程管理数据结构均在进程的用户空间中,线程切换不需要内核模式,能节省模式切换开销和内核的宝贵资源,允许进程按应用特定需要选择调度算法,甚至根据应用需求裁剪调度算法,能运行在任何OS上,内核在支持ULT方面不需要做任何工作,不能利用多处理器的优点,OS调度进程,仅有一个ULT能执行,一个ULT的阻塞,将引起整个进程的阻塞

Jacketing技术

把阻塞式系统调用改造成非阻塞式的,当线程陷入系统调用时,执行jacketing程序,由jacketing程序来检查资源使用情况,以决定是否执行进程切换或传递控制权给另一个线程

ULT适用于解决逻辑并行性问题,KLT适用于解决物理并行性问题

多线程实现的混合式策略

线程创建是完全在用户空间做的,单应用的多个ULT可以映射成一些KLT,通过调整KLT数目,可以达到较好的并行效果

多线程实现混合式策略的特点

组合用户级线程/内核级线程设施,线程创建完全在用户空间中完成,线程的调度和同步也在应用程序中进行,一个应用中的多个用户级线程被映射到一些(小于等于用户级线程数目)内核级线程上,程序员可以针对特定应用和机器调节内核级线程的数目,以达到整体最佳结果,该方法将会结合纯粹用户级线程方法和内核级线程方法的优点,同时减少它们的缺点

线程混合式策略下的线程状态

KLT三态,系统调度负责,ULT三态,用户调度负责,活跃态ULT代表绑定KLT的三态,活跃态ULT运行时可激活用户调度,非阻塞系统调用可使用Jacketing启动用户调度,调整活跃态ULT

多线程实现的各种策略总结

处理器调度的层次

高级调度:又称长程调度,作业调度,决定能否加入到执行的进程池中

中级调度,又称平衡负载调度,决定主存中的可用进程集合

低级调度:又称短程调度,进程调度,决定哪个可用进程占用处理器执行

高级调度

分时OS中,高级调度决定:是否接受一个终端用户的连接,命令能否被系统接纳并构成进程,新建态进程是否加入就绪进程队列

批处理OS中,高级调度又称为作业调度,功能是按照某种原则从后备作业队列中选取作业进入主存,并为作业做好运行前的准备工作和完成后的善后工作

中级调度

引进中级调度是为了提高内存利用率和作业吞吐量,中级调度决定那些进程被允许驻留在主存中参与竞争处理器及其他资源,起到短期调整系统负荷的作用,中级调度把一些进程换出主存,从而使之进入“挂起”状态,不参与进程调度,以平顺系统的负载

低级调度

低级调度:又称处理器调度、进程调度、短程调度,按照某种原则把处理器分配给就绪态进程或内核级线程,进程调度程序:又称分派程序,操作系统中实现处理器调度的程序,是操作系统的最核心部分,处理器调度策略的优劣直接影响到整个系统的性能

低级调度的主要功能:

  • 记住进程或内核级线程的状态
  • 决定某个进程或内核级线程什么时候获得处理器,以及占用多长时间
  • 把处理器分配给进程或内核级线程
  • 收回处理器

处理器调度算法

选择处理器调度算法的原则

  • 资源利用率:使得CPU或其他资源的使用率尽可能高且能够并行工作
  • 响应时间:使交互式用户的响应时间尽可能小,或尽快处理实时任务
  • 周转时间:提交给系统开始到执行完成获得结果为止的这段时间间隔称周转时间,应该使周转时间或平均周转时间尽可能短
  • 吞吐量:单位时间处理的进程数尽可能多
  • 公平性:确保每个用户每个进程获得合理的CPU份额或其他资源份额

优先数调度算法

根据分配给进程的优先数决定运行进程:抢占式优先数调度算法(对实时的适用),非抢占式优先数调度算法(对大多数适合)

优先数的确定准则:进程负担任务的紧迫程度,进程的交互性,进程使用外设的频度,进程进入系统的时间长短

与进入系统时间相关的优先数

  • 计算时间短(作业/进程)优先
  • 剩余计算时间短进程优先
  • 响应比高者(作业/进程)优先响应比=等待时间/进入时间
  • 先来先服务:先进队先被选择
    • 多用于高级调度;低级调度中,以计算为主的进程过于优越

时间片轮转调度算法

  • 根据各个进程进入就绪队列的时间先后轮流占有CPU一个时间片
  • 时间片中断
  • 时间片的确定:选择长短合适的时间片,过长则退化为先来先服务算法,过短则调度开销大
  • 单时间片,多时间片和动态时间片

分级调度算法

又称多队列策略,反馈循环队列,基本思想是建立多个不同优先级的就绪进程队列,多个就绪进程队列间按照优先数调度,高优先级就绪进程,分配的时间片短,单个就绪进程队列中进程的优先数和时间片相同

分级调度算法的分级原则

  • 一般分级原则
    • 外设访问,交互性,时间紧迫程度,系统效率,用户立场,...
  • 现代操作系统的实现模型
    • 多个高优先级的实时进程队列,如:硬实时、网络、软实时
    • 多个分时任务的进程队列,根据基准优先数和执行行为调整
    • 队列数可能多达32-128个

彩票调度算法

基本思想:为进程发放针对系统各种资源(如CPU时间)的彩票;当调度程序需要做出决策时,随机选择一张彩票,持有该彩票的进程将获得系统资源,合作进程之间的彩票交换

Guess you like

Origin www.cnblogs.com/ygjzs/p/12344695.html