FIFO (computing and electronics): Difference between revisions
m Reverted 1 edit by 111.68.96.209 identified as test/vandalism using STiki |
restore Linux view ref, fix example (bugs, shrink, head/tail -> back/front, as noted in article) |
||
Line 28: | Line 28: | ||
private: |
private: |
||
struct Node |
struct Node { |
||
⚫ | |||
⚫ | |||
⚫ | |||
Node *next; |
Node *next; |
||
Node() : |
Node(T _value) : value(_value), next(NULL) {} |
||
void set(T newValue, Node *newNext) |
|||
{ |
|||
⚫ | |||
next = newNext; |
|||
} |
|||
void setNext(Node *newNext) |
|||
{ |
|||
next = newNext; |
|||
} |
|||
}; |
}; |
||
Node * |
Node *front; |
||
Node * |
Node *back; |
||
public: |
public: |
||
FIFO() : front(NULL), back(NULL) {} |
|||
FIFO( |
~FIFO() { |
||
while (front != NULL) |
|||
⚫ | |||
⚫ | |||
void enqueue(T _value) |
void enqueue(T _value) { |
||
⚫ | |||
{ |
|||
⚫ | |||
if (front == NULL) |
|||
front = newNode; |
|||
{ |
|||
tail = newNode; |
|||
} |
|||
else |
else |
||
back->next = newNode; |
|||
head->setNext(newNode); |
|||
back = newNode; |
|||
head = newNode; |
|||
} |
} |
||
T dequeue() { |
|||
if (front == NULL) |
|||
{ |
|||
throw std::underflow_error("Nothing to dequeue"); |
|||
{ |
|||
Node *temp = front; |
|||
throw std::underflow_error("Nothing to dequeue."); |
|||
T result = front->value; |
|||
front = front->next; |
|||
delete temp; |
|||
tail = tail->next; |
|||
return result; |
return result; |
||
} |
} |
||
Line 89: | Line 77: | ||
===Head or tail first=== |
===Head or tail first=== |
||
Controversy over the terms "head" and "tail" exists in reference to FIFO queues |
Controversy over the terms "head" and "tail" exists in reference to FIFO queues: |
||
* To many people, items should enter a queue at the tail, remain in the queue until they reach the head and leave the queue from there. This point of view is justified by analogy with queues of people waiting for some kind of service and parallels the use of "front" and "back" in the above example. |
|||
* Other people believe that objects enter a queue at the head and leave at the tail, in the manner of food passing through a snake. Queues written in that way appear in places that could be considered authoritative, such as the [[GNU/Linux]] [[operating system]]. |
|||
===Pipes=== |
===Pipes=== |
Revision as of 13:23, 1 December 2014
FIFO is an acronym for First In, First Out, a method for organizing and manipulating a data buffer, where the oldest (first) entry, or 'head' of the queue, is processed first. It is analogous to processing a queue with first-come, first-served (FCFS) behaviour: where the people leave the queue in the order in which they arrive.
FCFS is also the jargon term for the FIFO operating system scheduling algorithm, which gives every process CPU time in the order in which it is demanded.
FIFO's opposite is LIFO, Last-In-First-Out, where the youngest entry or 'top of the stack' is processed first.[1]
A priority queue is neither FIFO or LIFO but may adopt similar behaviour temporarily or by default.
Queueing theory encompasses these methods for processing data structures, as well as interactions between strict-FIFO queues.
Computer science
Data structure
A typical data structure in the C++ language will look like this:
#include <iostream>
#include <stdexcept>
template <typename T>
class FIFO
{
private:
struct Node {
T value;
Node *next;
Node(T _value) : value(_value), next(NULL) {}
};
Node *front;
Node *back;
public:
FIFO() : front(NULL), back(NULL) {}
~FIFO() {
while (front != NULL)
dequeue();
}
void enqueue(T _value) {
Node *newNode = new Node(_value);
if (front == NULL)
front = newNode;
else
back->next = newNode;
back = newNode;
}
T dequeue() {
if (front == NULL)
throw std::underflow_error("Nothing to dequeue");
Node *temp = front;
T result = front->value;
front = front->next;
delete temp;
return result;
}
};
(For information on the abstract data structure, see Queue. For details of a common implementation, see Circular buffer.)
Popular Unix systems include a sys/queue.h C/C++ header file which provides macros usable by applications which need to create FIFO queues.
Head or tail first
Controversy over the terms "head" and "tail" exists in reference to FIFO queues:
- To many people, items should enter a queue at the tail, remain in the queue until they reach the head and leave the queue from there. This point of view is justified by analogy with queues of people waiting for some kind of service and parallels the use of "front" and "back" in the above example.
- Other people believe that objects enter a queue at the head and leave at the tail, in the manner of food passing through a snake. Queues written in that way appear in places that could be considered authoritative, such as the GNU/Linux operating system.
Pipes
In computing environments that support the pipes and filters model for interprocess communication, a FIFO is another name for a named pipe.
Disk scheduling
Disk controllers can use the FIFO as a disk scheduling algorithm to determine the order to service disk I/O requests.
Communications and networking
Communications bridges, switches and routers used in Computer networks use FIFOs to hold data packets en route to their next destination. Typically at least one FIFO structure is used per network connection. Some devices feature multiple FIFOs for simultaneously and independently queuing different types of information.
Electronics
FIFOs are commonly used in electronic circuits for buffering and flow control which is from hardware to software. In its hardware form, a FIFO primarily consists of a set of read and write pointers, storage and control logic. Storage may be SRAM, flip-flops, latches or any other suitable form of storage. For FIFOs of non-trivial size, a dual-port SRAM is usually used, where one port is dedicated to writing and the other to reading.
A synchronous FIFO is a FIFO where the same clock is used for both reading and writing. An asynchronous FIFO uses different clocks for reading and writing. Asynchronous FIFOs introduce metastability issues. A common implementation of an asynchronous FIFO uses a Gray code (or any unit distance code) for the read and write pointers to ensure reliable flag generation. One further note concerning flag generation is that one must necessarily use pointer arithmetic to generate flags for asynchronous FIFO implementations. Conversely, one may use either a "leaky bucket" approach or pointer arithmetic to generate flags in synchronous FIFO implementations.
Examples of FIFO status flags include: full, empty, almost full, almost empty, etc.
The first known FIFO implemented in electronics was done by Peter Alfke in 1969 at Fairchild Semiconductors. Peter Alfke was later a Director at Xilinx.
FIFO full/empty
A hardware FIFO is used for synchronization purposes. It is often implemented as a circular queue, and thus has two pointers:
- Read Pointer/Read Address Register
- Write Pointer/Write Address Register
Read and write addresses are initially both at the first memory location and the FIFO queue is Empty.
- FIFO Empty
- When the read address register reaches the write address register, the FIFO triggers the Empty signal.
- FIFO FULL
- When the write address register reaches the read address register, the FIFO triggers the FULL signal.
In both cases, the read and write addresses end up being equal. To distinguish between the two situations, a simple and robust solution is to add one extra bit for each read and write address which is inverted each time the address wraps. With this set up, the conditions are:
- FIFO Empty
- When the read address register equals the write address register, the FIFO is empty.
- FIFO FULL
- When the read address LSBs equal the write address LSBs and the extra MSBs are different, the FIFO is full.
See also
- LIFO (Last in, first out)
- GIGO (Garbage In, Garbage Out)
- FINO (First in, never out)
- Queueing theory, the study of waiting lines
- Queue (data structure), in computing is an abstract data type
Notes and references
- ^ Kruse, Robert L. (1987) [1984]. Data Structures & Program Design (second edition). Joan L. Stone, Kenny Beck, Ed O'Dougherty (production process staff workers) (second (hc) textbook ed.). Englewood Cliffs, New Jersey 07632: Prentice-Hall, Inc. div. of Simon & Schuster. p. 150. ISBN 0-13-195884-4.
The definition of a finite sequence immediately makes it possible for us to attempt a definition of a list: A 'list' of terms of type T is simply a finite sequence of elements of the set T. ... The only difference among stacks and queues and more general lists is the operations by which changes or accesses can be made to the list.
{{cite book}}
: CS1 maint: location (link)
- Cummings et al., Simulation and Synthesis Techniques for Asynchronous FIFO Design with Asynchronous Pointer Comparisons, SNUG San Jose 2002
- Ronen Perry & Tal Zarsky, Queues in Law, Iowa Law Review (August 10, 2012)