# 计算机多任务处理

**计算机多任务处理**是指计算机操作系统在单个CPU上并发运行多个程序或进程的能力，其实现方式是在它们之间快速切换，快到所有程序看起来都在同时执行。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)</sup> 在单处理器系统上，任何时刻只有一个进程能使用CPU；同时执行的外观来自快速切换。<sup>[2](https://ebooks.inflibnet.ac.in/csp3/chapter/chapter-1/)</sup> 在多处理器系统上，线程可以真正同时执行，同时执行的线程数最多与处理器数量相同。<sup>[3](https://learn.microsoft.com/en-us/windows/desktop/ProcThread/about-processes-and-threads)</sup>

| 关键事实 | 详情 |
|---|---|
| 两种基本形式 | 抢占式多任务：操作系统分配CPU时间片并强制活动程序让出CPU；协作式多任务：每个程序必须自愿让出控制权，否则就会独占CPU<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)</sup> |
| 时间片大小 | Windows的时间片约为20毫秒；现代系统通常使用4至8毫秒的时间配额<sup>[4](https://learn.microsoft.com/en-us/windows/desktop/procthread/multitasking)</sup><sup> • </sup><sup>[5](https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Multiprogramming.html)</sup> |
| 切换开销 | 上下文切换保存一个执行上下文的状态并恢复另一个执行上下文的状态；若开销为5毫秒、时间配额为20毫秒，则CPU时间的20%用于开销，而在50毫秒配额下这一比例降至约10%<sup>[6](https://os.cs.luc.edu/scheduling.html)</sup> |
| 首个分时系统 | CTSS由麻省理工学院在一台经过特殊改造的IBM 7094上开发（科尔巴托等，1962），被称为第一个真正意义上的分时系统<sup>[7](https://www.cs.vu.nl/~ast/books/mos2/sample-1.pdf)</sup> |
| Multics | 一个大型机分时操作系统，1965年由麻省理工学院与通用电气、贝尔实验室合作启动；贝尔实验室于1969年退出<sup>[8](https://web.mit.edu/multics-history/)</sup> |
| 桌面系统的采用 | Windows 3.1（1992）使用非抢占式多任务；Windows NT（1993）和Windows 95（1995）将抢占式多任务带入个人电脑；Mac OS X（2001）取代了协作式的Mac OS 9<sup>[9](https://people.cs.rutgers.edu/~pxk/articles/big-ideas-os.html)</sup><sup> • </sup><sup>[10](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup> |
| Linux调度器 | 完全公平调度器由英戈·莫尔纳实现，在Linux 2.6.23中合并，模拟了一个“理想的多任务CPU”；如今它正在为EEVDF让出位置<sup>[11](https://cdn.kernel.org/doc/html/latest/scheduler/sched-design-CFS.html)</sup> |

## 抢占式与协作式多任务

在抢占式多任务中，操作系统决定如何向每个程序分配CPU时间片。当时间片用完时，正在运行的程序必须将控制权交还操作系统，无论它自己是否愿意。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)</sup> 每次线程切换时，操作系统保存被替换线程的状态，并加载队列中下一个线程先前保存的状态。<sup>[4](https://learn.microsoft.com/en-us/windows/desktop/procthread/multitasking)</sup> Unix、Windows 95/98、[Windows NT](https://www.edgepedia.org/zh/windows-nt)以及后来的macOS都支持这种模式。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)</sup>

在协作式多任务中，每个程序控制自己需要多少CPU时间，并且必须配合将控制权让给其他程序。[Windows 3.1](https://www.edgepedia.org/zh/windows-3-1)和经典Mac OS采用了这种方法。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)</sup> 协作式多任务实现简单，当所有进程都是交互式的时候开销可以很低，但它容易受到失控进程的影响：一个陷入无限循环、不执行任何输入或输出的程序永远不会交出CPU，所有其他进程都会被阻塞。这一弱点正是现代系统倾向于抢占式的原因。<sup>[5](https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Multiprogramming.html)</sup> 苹果的文档直白地说明了后果：在[Mac OS 9](https://www.edgepedia.org/zh/mac-os-9)这个协作式环境中，哪怕只有一个应用程序不配合，所有进程的响应性都会受损。<sup>[10](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup>

**选择时间配额。** 时间配额，即进程在调度决策之前可以运行的时间长度，涉及一种权衡。较短的时间配额改善响应性，因为可运行的进程等待轮到自己的时间更短；较长的时间配额提高效率，因为系统花在上下文切换上的时间更少。对这一权衡的[实证研究](https://www.edgepedia.org/zh/empirical-research)使得现代系统普遍采用每个时间配额4至8毫秒的做法。<sup>[6](https://os.cs.luc.edu/scheduling.html)</sup><sup> • </sup><sup>[5](https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Multiprogramming.html)</sup> 协作式多任务可以看作给每个进程提供了无限的时间配额，相应的风险是某些进程会垄断CPU。<sup>[5](https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Multiprogramming.html)</sup>

## 从多道程序设计到分时

第一种允许在一台机器上运行多个任务的技术是多道程序设计，其中每个程序被赋予一个优先级，操作系统将中央处理器提供给优先级最高的程序。当该程序暂停等待输入或输出时，一个中断让调度器启动下一个程序。相当反直觉的是，最高优先级给了使用中央处理器最少的程序。这种原始方案要求操作员了解每个程序的特性，后来演变为多任务处理，即用户指定作业的特性，由操作系统调度执行。<sup>[12](https://www.cs.cornell.edu/wya/AcademicComputing/text/earlytimesharing.html)</sup>

分时将这些思想扩展到终端上的众多用户。每个程序依次获得处理器一段固定时间，然后被中断以便下一个程序继续；这就是时间分片。<sup>[12](https://www.cs.cornell.edu/wya/AcademicComputing/text/earlytimesharing.html)</sup> 分时出现在20世纪60年代初，是对大型机计算时间成本高昂以及批处理模式不便的回应——批处理模式要求用户调整自己的日程来适应机器。<sup>[13](https://www.computerhistory.org/pdp-1/timesharing/)</sup> 1962年9月，波士顿的声学研究公司BBN在其前一年购买的PDP-1上演示了一个分时系统，由BBN的埃德·弗雷德金和麻省理工学院的杰克·丹尼斯开发；该系统一直在学生中使用到20世纪70年代中期。<sup>[13](https://www.computerhistory.org/pdp-1/timesharing/)</sup>

早期的学术系统包括麻省理工学院的Multics、剑桥大学的Titan以及达特茅斯分时系统。麻省理工学院和剑桥强调研究人员的需求，而达特茅斯则侧重于本科教育。<sup>[12](https://www.cs.cornell.edu/wya/AcademicComputing/text/earlytimesharing.html)</sup> Multics于1965年作为麻省理工学院、通用电气和[贝尔实验室](https://www.edgepedia.org/zh/bell-labs)的联合项目启动，被构想为一种通用的分时公用设施，是通用电气的一款商业产品，约有85个站点运行。它的大部分代码用高级语言PL/1编写，不同于用汇编代码编写并与某一硬件架构紧密绑定的操作系统。<sup>[8](https://web.mit.edu/multics-history/)</sup><sup> • </sup><sup>[12](https://www.cs.cornell.edu/wya/AcademicComputing/text/earlytimesharing.html)</sup> Multics常被描述为Unix的前身，Unix的早期开发者此前曾在Multics项目上工作过。<sup>[12](https://www.cs.cornell.edu/wya/AcademicComputing/text/earlytimesharing.html)</sup>

## 进程、线程与上下文切换

进程提供执行程序所需的资源：虚拟地址空间、可执行代码、安全上下文、唯一标识符以及至少一个执行线程。线程是进程中可被调度执行的实体，一个进程的所有线程共享其虚拟地址空间和系统资源。<sup>[3](https://learn.microsoft.com/en-us/windows/desktop/ProcThread/about-processes-and-threads)</sup> 每个进程有自己的一组变量，而线程共享相同的数据，因此多线程程序必须小心处理线程访问和修改数据的方式。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)</sup>

上下文切换是从一个进程的虚拟内存映像切换到另一个。内存的内核部分不变，但CPU使用的用户态代码、数据、堆和栈段会改变。切换以两种方式引入开销：选择下一个进程的调度例程需要时间，内存层次结构也受到影响，因为早期的缓存系统在每次切换时都必须使所有缓存数据失效，而较新的系统按进程划分缓存，降低了其有效容量。<sup>[5](https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Multiprogramming.html)</sup>

## 现代系统中的调度

大多数现代通用操作系统使用带分时和优先级的抢占式调度；Linux、BSD系统、Windows NT及以后版本、macOS、VMS以及大多数UNIX系统都使用抢占式多任务。<sup>[6](https://os.cs.luc.edu/scheduling.html)</sup> 调度器必须平衡响应性、公平性、吞吐量和开销，没有任何策略能同时优化所有这些目标。<sup>[6](https://os.cs.luc.edu/scheduling.html)</sup>

**Linux的公平调度器。** 完全公平调度器（CFS）由英戈·莫尔纳实现并在Linux 2.6.23中合并，它模拟了一个“理想、精确的多任务CPU”，该CPU会以机器算力的1/n并行运行n个任务中的每一个。由于真实硬件一次只运行一个任务，CFS跟踪每个任务的虚拟运行时间——即其实际运行时间按运行任务数归一化后的值——并总是运行虚拟运行时间最小的任务，也就是迄今执行最少的任务。<sup>[11](https://cdn.kernel.org/doc/html/latest/scheduler/sched-design-CFS.html)</sup> 对旧的O(1)调度器和CFS的实证比较发现，CFS在不牺牲交互性能的前提下实现了公平CPU分配的设计目标。<sup>[14](https://research.monash.edu/en/publications/fairness-and-interactive-performance-of-o1-and-cfs-linux-kernel-s/)</sup> 内核文档指出，CFS如今正在为EEVDF调度器让出位置。<sup>[11](https://cdn.kernel.org/doc/html/latest/scheduler/sched-design-CFS.html)</sup>

**移动端的限制。** 手机上的多任务处理被刻意限制以延长电池寿命。在iOS上，当一个应用进入后台时，其委托对象大约有五秒钟时间完成任务，之后系统会挂起该应用；开发者可以通过`beginBackgroundTask`方法请求额外时间，系统会终止那些未及时结束任务的应用。<sup>[15](https://developer.apple.com/documentation/uikit/extending-your-app-s-background-execution-time?changes=_6)</sup> 对于更长时间的工作，连续后台任务（`BGContinuedProcessingTask`）允许应用在后台运行耗时数分钟甚至更久的作业，系统会向用户告知进度并允许取消。<sup>[16](https://developer.apple.com/documentation/BackgroundTasks/performing-long-running-tasks-on-ios-and-ipados)</sup>

## References

1. [Multithreading, MIT OpenCourseWare, Foundations of Software Engineering (Fall 2000)](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/multithreading/)
2. [Operating System: Introduction, INFLIBNET e-Books](https://ebooks.inflibnet.ac.in/csp3/chapter/chapter-1/)
3. [About Processes and Threads, Microsoft Learn](https://learn.microsoft.com/en-us/windows/desktop/ProcThread/about-processes-and-threads)
4. [Multitasking, Microsoft Learn](https://learn.microsoft.com/en-us/windows/desktop/procthread/multitasking)
5. [Processes and Multiprogramming, Computer Systems Fundamentals, James Madison University](https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Multiprogramming.html)
6. [Process/Thread Scheduling, Introduction to Operating Systems: Principles and Practice, Loyola University Chicago](https://os.cs.luc.edu/scheduling.html)
7. [History of Operating Systems, sample chapter, Modern Operating Systems (Tanenbaum), VU Amsterdam](https://www.cs.vu.nl/~ast/books/mos2/sample-1.pdf)
8. [Multics History, MIT](https://web.mit.edu/multics-history/)
9. [Big Ideas in the History of Operating Systems, Paul Krzyzanowski, Rutgers University](https://people.cs.rutgers.edu/~pxk/articles/big-ideas-os.html)
10. [Kernel Architecture Overview, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)
11. [CFS Scheduler, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/scheduler/sched-design-CFS.html)
12. [The Early Years of Academic Computing: Early Timesharing, Cornell University](https://www.cs.cornell.edu/wya/AcademicComputing/text/earlytimesharing.html)
13. [Timesharing, PDP-1 Restoration Project, Computer History Museum](https://www.computerhistory.org/pdp-1/timesharing/)
14. [Fairness and interactive performance of O(1) and CFS Linux kernel schedulers, Wong et al., ITSim 2008, IEEE](https://research.monash.edu/en/publications/fairness-and-interactive-performance-of-o1-and-cfs-linux-kernel-s/)
15. [Extending your app's background execution time, Apple Developer Documentation](https://developer.apple.com/documentation/uikit/extending-your-app-s-background-execution-time?changes=_6)
16. [Performing long-running tasks on iOS and iPadOS, Apple Developer Documentation](https://developer.apple.com/documentation/BackgroundTasks/performing-long-running-tasks-on-ios-and-ipados)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Operating system concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/computer-multitasking

English original: https://www.edgepedia.org/computer-multitasking
