# FIFO（计算）

FIFO，即先进先出，是一种队列规则：最先加入集合的元素最先被移除。作为一种数据结构，队列限制只能从两端访问：元素只能从尾部插入，这一操作称为入队（enqueue）；只能从头部移除，称为出队（dequeue）。<sup>[1](https://lut-dsa.cc.lut.fi/Books/LUTDSA/html/_sources/Queue.rst.txt)</sup> 同样的排序规则贯穿整个计算领域，从按到达顺序转发数据包的路由器缓冲区<sup>[2](https://book.systemsapproach.org/congestion/queuing.html)</sup>，到并行应用和操作系统中被众多线程共享的并发队列。<sup>[3](https://cs.rochester.edu/%7Escott/papers/1996_PODC_queues.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 顺序保证 | 元素严格按照加入的顺序被移除。<sup>[4](https://opendatastructures.org/ods-cpp/2_3_Array_Based_Queue.html)</sup> |
| 核心操作 | 尾部入队，头部出队。<sup>[1](https://lut-dsa.cc.lut.fi/Books/LUTDSA/html/_sources/Queue.rst.txt)</sup> |
| 复杂度 | 环形数组队列每次入队和出队均为 O(1)，无需摊还分析。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup> |
| 满/空区分 | 固定容量缓冲区保留显式计数或预留一个槽位，使满的条件为 (tail + 1) % cap == head。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup> |
| 无锁界限 | 迈克尔–斯科特队列的入队使用一次 CAS 链接新节点，另一次推进 Tail；成功的出队使用一次 CAS 推进 Head。<sup>[3](https://cs.rochester.edu/%7Escott/papers/1996_PODC_queues.pdf)</sup> |
| 已知失效模式 | 在先来先服务的 LLM 推理中，队头阻塞占总端到端延迟的比例可高达 90%。<sup>[6](https://www.usenix.org/system/files/conference/nsdi26/nsdi26spring_wu-bingyang_prepub.pdf)</sup> |

## 工作原理

FIFO 队列是一种类列表结构，访问受限：只在尾部插入，只在头部移除。<sup>[1](https://lut-dsa.cc.lut.fi/Books/LUTDSA/html/_sources/Queue.rst.txt)</sup> 这一单一限制产生了定义性的不变式：元素按照加入的相同顺序被移除。<sup>[4](https://opendatastructures.org/ods-cpp/2_3_Array_Based_Queue.html)</sup> 该规则与优先队列形成对比，后者按分配的优先级而非到达顺序服务。<sup>[2](https://book.systemsapproach.org/congestion/queuing.html)</sup>

在网络中，FIFO 是一种调度规则，决定数据包的传输顺序，它不同于丢弃策略——后者决定缓冲区满时丢弃哪些数据包。<sup>[2](https://book.systemsapproach.org/congestion/queuing.html)</sup>

## 实现方式

教科书式的实现是带有 head 和 tail 索引的环形数组，索引按缓冲区长度取模回绕，从而实现 O(1) 的入队和出队，无需摊还。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup> 数组被视为环形的，因此索引会回绕而不是移动内容。<sup>[4](https://opendatastructures.org/ods-cpp/2_3_Array_Based_Queue.html)</sup> 由于缓冲区为空和满时 head 与 tail 都重合，固定容量的环形缓冲区需要两种区分策略之一：保留显式的计数字段，或预留一个槽位使满的条件为 (tail + 1) % cap == head。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup> 可增长变体在溢出时将底层缓冲区加倍，摊还为每次操作 O(1)，而固定环形缓冲区是真正的 O(1)，没有平均情况的附加条件。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup> 基于可增长数组的队列具有 O(1) 摊还操作，偶尔有 O(n) 的扩容；而固定容量的环形队列在其容量策略约束下具有 O(1) 最坏情况操作。<sup>[4](https://opendatastructures.org/ods-cpp/2_3_Array_Based_Queue.html)</sup>

朴素使用单链表是一个糟糕的选择，因为链表的一端需要执行运行时间与[链表](https://www.edgepedia.org/zh/linked-list)长度成正比的操作。<sup>[4](https://opendatastructures.org/ods-cpp/2_3_Array_Based_Queue.html)</sup> 各语言库因此有所不同：在 Python 中，list.pop(0) 的代价为 O(n)，而 collections.deque 的 popleft 为 O(1)；Java 的 ArrayDeque 以 O(1) 摊还时间提供 push、pop 和 peek。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup>

硬件 FIFO 遵循相同的指针方案。当入队和出队指针指向同一元素时，硬件在没有额外状态的情况下无法区分满和空，因此满和空寄存器记录是哪个操作使指针移动到了重叠位置；更大的硬件 FIFO 以环形缓冲区构建。<sup>[7](https://csg.csail.mit.edu/6.175/labs/lab4-fifo.html)</sup>

## 起源

对于并发 FIFO 队列，非阻塞和双锁队列算法由马吉德·M·迈克尔（Maged M. Michael）和迈克尔·L·斯科特（Michael L. Scott）于 1995 年报告。<sup>[8](https://doi.org/10.21236/ada309412)</sup> 差额轮询公平排队近似由 M·什里达尔（M. Shreedhar）和乔治·瓦尔盖塞（George Varghese）于 1995 年在《ACM SIGCOMM 计算机通信评论》上报告。<sup>[9](https://doi.org/10.1145/217391.217453)</sup>

## 变体

若干结构修改了普通 FIFO 规则。双端队列允许在两端进行插入和移除，而非仅一端；环形缓冲区是固定容量的 FIFO，用于 UART 驱动缓冲区、[Linux 内核](https://www.edgepedia.org/zh/linux-kernel) kfifo、journald 日志环和 LMAX Disruptor。<sup>[5](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)</sup> 优先排队运行多个 FIFO 队列，每个优先级类一个，总是先服务最高优先级的非空队列。<sup>[2](https://book.systemsapproach.org/congestion/queuing.html)</sup> 在网络中，随机早期检测以概率方式丢弃到达的数据包，丢弃概率随估计的平均队列长度增长而增加。<sup>[10](https://docbox.etsi.org/reference/IETF/RFC/RFC2309.pdf)</sup> 差额轮询在吞吐量上实现近乎完美的公平性，处理一个数据包只需 O(1) 的工作量，且简单到足以在硬件中实现，而早期的公平排队方案对 n 个活跃流每个数据包需要 O(log(n)) 的工作量。<sup>[9](https://doi.org/10.1145/217391.217453)</sup>

并发变体放宽或加强了排序规则。k-FIFO 队列允许每个元素最多乱序出队 \( k - 1 \) 个位置；1-FIFO 队列就是普通 FIFO，有限的 k 提供了有界公平性。<sup>[11](https://ckirsch.github.io/publications/reports/SBG12-FastScalableQueues.pdf)</sup> 迈克尔–斯科特队列将 FIFO 实现为带有 Head 和 Tail 指针的单链表，其中 Head 始终指向一个哑节点；成功的入队可能执行一次链接 CAS 和一次 Tail 推进 CAS，而成功的出队通常用一次 CAS 推进 Head，在竞争下可能重试。<sup>[3](https://cs.rochester.edu/%7Escott/papers/1996_PODC_queues.pdf)</sup> 其双锁对应版本在出队时获取 Head 锁，读取 node->next 作为新头，并将 next 为 NULL 视为空队列。<sup>[12](https://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html)</sup> 后来的设计用乐观双向链表替代代价高昂的 CAS 插入单链表，其指针通过简单存储更新。<sup>[13](https://link.springer.com/article/10.1007/s00446-007-0050-0)</sup> 在现代 x86 系统上，使用[取加](https://www.edgepedia.org/zh/fetch-and-add)（fetch-and-add）而非 CAS 循环来递增有竞争的计数器可避免重试，带来 4 倍或更大的加速，这推动了基于 F&A 的队列设计。<sup>[14](https://www.cs.tau.ac.il/~mad/publications/ppopp2013-x86queues.pdf)</sup> 2019 年的一项设计使用间接寻址和两个队列构建无锁多生产者多消费者环形缓冲区，在环形缓冲区内部集成了迈克尔–斯科特队列。<sup>[15](https://drops.dagstuhl.de/storage/00lipics/lipics-vol146-disc2019/LIPIcs.DISC.2019.28/LIPIcs.DISC.2019.28.pdf)</sup>

## 应用

在互联网路由器中，FIFO 因其实现简单而被广泛使用，按到达顺序接受传入数据包。<sup>[16](https://www-mura.ist.osaka-u.ac.jp/achievements/annual_report/web1999/papers/hasegawa/hasegawa00infocom-RouterFairness.pdf)</sup> 由于它简单地按数据包到达队列的顺序转发，FIFO 处理已成为现代数据包处理器中的事实标准，尽管它不保证数据包按顺序到达目的地。<sup>[17](https://dspace.networks.imdea.org/bitstream/handle/20.500.12761/1496/tcsystems-10.pdf?isAllowed=y&sequence=1)</sup><sup> • </sup><sup>[24](https://www.rfc-editor.org/info/rfc3366/)</sup> 在 CPU 调度中，FIFO 按到达顺序运行作业，产生下文所述的护航效应。<sup>[18](https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched.pdf)</sup> 在缓存淘汰中，FIFO 可以用环形缓冲区实现，无需为每个对象保存指针元数据，且缓存命中时不提升对象，消除了 LRU 的可扩展性瓶颈；S3-FIFO 算法完全由静态 FIFO 队列构建，被淘汰的对象可能被重新插入以保留热对象。<sup>[19](https://pdl.cmu.edu/PDL-FTP/Storage/FIFOqueues-SOSP23.pdf)</sup> 并发 FIFO 队列还出现在实时数据处理引擎、[TensorFlow](https://www.edgepedia.org/zh/tensorflow) 和 PyTorch 等机器学习框架、CUDA 流以及最短路径算法中。<sup>[20](https://www.mdpi.com/2076-3417/15/4/1834)</sup> 在 LLM 推理中，Orca 和 vLLM 系统按先来先服务处理推理作业。<sup>[6](https://www.usenix.org/system/files/conference/nsdi26/nsdi26spring_wu-bingyang_prepub.pdf)</sup>

## 局限与替代方案

FIFO 的主要弱点是队头阻塞。在 CPU 调度中，队首的长作业使短作业等待：100 秒、10 秒和 10 秒的作业给出平均周转时间 \( (100 + 110 + 120)/3 = 110 \) 秒，这个问题称为护航效应。<sup>[18](https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched.pdf)</sup> 代价可能很大：在 ShareGPT 和 Alpaca 等真实 LLM 工作负载中，偏斜的输出长度分布导致队头阻塞，在 FCFS 调度下占总端到端延迟的比例可高达 90%，这就是 FastServe 用跳转加入的多级反馈队列和令牌级抢占替代 FCFS 的原因。<sup>[6](https://www.usenix.org/system/files/conference/nsdi26/nsdi26spring_wu-bingyang_prepub.pdf)</sup> 优先排队按类别确定服务顺序，但可能使低优先级队列饥饿，因此需要对高优先级流量设置硬限制。<sup>[2](https://book.systemsapproach.org/congestion/queuing.html)</sup>

溢出行为是第二个局限。尾部丢弃设置最大队列长度，一旦队列满就丢弃新到达的数据包；RFC 2309 记录了这一传统技术有两个重要缺点，推动了主动队列管理，其中 RED 在缓冲区溢出之前以概率方式丢弃数据包。<sup>[10](https://docbox.etsi.org/reference/IETF/RFC/RFC2309.pdf)</sup> 在缓存淘汰中，FIFO 因 Belady 异常而臭名昭著，即增加更多缓存空间有时反而降低命中率。<sup>[21](https://www.usenix.org/system/files/hotstorage20_paper_eytan.pdf)</sup> 相对于 LRU，FIFO 的优点在于管理简单：只需保留头和尾，因此在持久存储上有元数据时，FIFO 避免了 LRU 将命中项移到尾部所需的昂贵随机 I/O，而在大缓存下，早期关于 LRU 实际更好的论断不再成立。<sup>[21](https://www.usenix.org/system/files/hotstorage20_paper_eytan.pdf)</sup>

甚至顺序保证也有实测的附加条件。元素公平性和操作公平性指标量化并发队列中的重排序，在这些指标下，严格 FIFO 实现可能比快速宽松实现在这些公平性指标上得分更差，尽管它们在线性一致性意义上仍是 FIFO。<sup>[22](https://dl.acm.org/doi/10.1145/2414729.2414731)</sup> 2024 年 FORTH 技术报告描述了持久 FIFO 队列，其入队操作将单元索引对环大小 R 取模后与 Tail 值比较。<sup>[23](http://publications.ics.forth.gr/tech-reports/2024/2024.TR493_Highly-Efficient_FIFO_Queues.pdf)</sup>

## References

1. [Queue (LUT DSA textbook chapter)](https://lut-dsa.cc.lut.fi/Books/LUTDSA/html/_sources/Queue.rst.txt)
2. [Queuing Disciplines, Computer Networks: A Systems Approach (Peterson and Davie)](https://book.systemsapproach.org/congestion/queuing.html)
3. [Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms (PODC 1996)](https://cs.rochester.edu/%7Escott/papers/1996_PODC_queues.pdf)
4. [Open Data Structures (C++ edition), Section 2.3: An Array-Based Queue](https://opendatastructures.org/ods-cpp/2_3_Array_Based_Queue.html)
5. [Queues, deques, and circular buffers, The DSA Handbook](https://dsa.handbook.academy/curriculum/linear-data-structures/queues-deques/)
6. [FastServe: Iteration-Level Preemptive Scheduling for Large Language Model Inference (NSDI '26)](https://www.usenix.org/system/files/conference/nsdi26/nsdi26spring_wu-bingyang_prepub.pdf)
7. [MIT 6.175 Constructive Computer Architecture, Lab 4: N-Element FIFOs](https://csg.csail.mit.edu/6.175/labs/lab4-fifo.html)
8. [Maged M. Michael, Michael L. Scott (1995). Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms.. .](https://doi.org/10.21236/ada309412)
9. [M. Shreedhar, George Varghese (1995). Efficient fair queueing using deficit round robin. ACM SIGCOMM Computer Communication Review.](https://doi.org/10.1145/217391.217453)
10. [Recommendations on Queue Management and Congestion Avoidance in the Internet (RFC 2309)](https://docbox.etsi.org/reference/IETF/RFC/RFC2309.pdf)
11. [Fast and Scalable k-FIFO Queues](https://ckirsch.github.io/publications/reports/SBG12-FastScalableQueues.pdf)
12. [Fast Concurrent Queue Algorithms, pseudocode (Rochester synchronization page)](https://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html)
13. [An optimistic approach to lock-free FIFO queues (Distributed Computing, 2007)](https://link.springer.com/article/10.1007/s00446-007-0050-0)
14. [Fast Concurrent Queues for x86 Processors (PPoPP 2013)](https://www.cs.tau.ac.il/~mad/publications/ppopp2013-x86queues.pdf)
15. [A Scalable, Portable, and Memory-Efficient Lock-Free FIFO Queue (DISC 2019)](https://drops.dagstuhl.de/storage/00lipics/lipics-vol146-disc2019/LIPIcs.DISC.2019.28/LIPIcs.DISC.2019.28.pdf)
16. [Comparisons of Packet Scheduling Algorithms for Fair Service among Connections on the Internet (INFOCOM)](https://www-mura.ist.osaka-u.ac.jp/achievements/annual_report/web1999/papers/hasegawa/hasegawa00infocom-RouterFairness.pdf)
17. [Online Scheduling FIFO Policies with Admission and Push-Out (IMDEA Networks)](https://dspace.networks.imdea.org/bitstream/handle/20.500.12761/1496/tcsystems-10.pdf?isAllowed=y&sequence=1)
18. [Scheduling: Introduction (OSTEP)](https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched.pdf)
19. [FIFO Queues are All You Need for Cache Eviction (SOSP 2023)](https://pdl.cmu.edu/PDL-FTP/Storage/FIFOqueues-SOSP23.pdf)
20. [Boundary-Aware Concurrent Queue: A Fast and Scalable Concurrent FIFO Queue on GPU Environments (Applied Sciences, MDPI, 2025)](https://www.mdpi.com/2076-3417/15/4/1834)
21. [It's Time to Revisit LRU vs. FIFO (USENIX HotStorage 2020)](https://www.usenix.org/system/files/hotstorage20_paper_eytan.pdf)
22. [How FIFO is your concurrent FIFO queue? (ACM, 2012)](https://dl.acm.org/doi/10.1145/2414729.2414731)
23. [Highly-Efficient Persistent FIFO Queues (FORTH TR493, 2024)](http://publications.ics.forth.gr/tech-reports/2024/2024.TR493_Highly-Efficient_FIFO_Queues.pdf)
24. [rfc-editor.org](https://www.rfc-editor.org/info/rfc3366/)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026; Oct 25, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Stacks, queues, and deques

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/fifo-computing

English original: https://www.edgepedia.org/fifo-computing
