Hubbry Logo
logo
Polling (computer science)
Community hub

Polling (computer science)

logo
0 subscribers
Read side by side
from Wikipedia

Polling, or interrogation, refers to actively sampling the status of an external device by a client program as a synchronous activity. Polling is most often used in terms of input/output (I/O), and is also referred to as polled I/O or software-driven I/O. A good example of hardware implementation is a watchdog timer.

Description

[edit]

Polling is the process where the computer or controlling device waits for an external device to check for its readiness or state, often with low-level hardware. For example, when a printer is connected via a parallel port, the computer waits until the printer has received the next character. These processes can be as minute as only reading one bit. This is sometimes used synonymously with 'busy-wait' polling. In this situation, when an I/O operation is required, the computer does nothing other than check the status of the I/O device until it is ready, at which point the device is accessed. In other words, the computer waits until the device is ready. Polling also refers to the situation where a device is repeatedly checked for readiness, and if it is not, the computer returns to a different task. Although not as wasteful of CPU cycles as busy waiting, this is generally not as efficient as the alternative to polling, interrupt-driven I/O.

In a simple single-purpose system, even busy-wait is perfectly appropriate if no action is possible until the I/O access, but more often than not this was traditionally a consequence of simple hardware or non-multitasking operating systems.

Polling is often intimately involved with very low-level hardware. For example, polling a parallel printer port to check whether it is ready for another character involves examining as little as one bit of a byte. That bit represents, at the time of reading, whether a single wire in the printer cable is at low or high voltage. The I/O instruction that reads this byte directly transfers the voltage state of eight real world wires to the eight circuits (flip flops) that make up one byte of a CPU register.

Polling has the disadvantage that if there are too many devices to check, the time required to poll them can exceed the time available to service the I/O device.

Algorithm

[edit]

Polling can be described in the following steps:

Host actions:

  1. The host repeatedly reads the busy bit of the controller until it becomes clear (with a value of 0).
  2. When clear, the host writes the command into the command register. If the host is sending output, it sets the write bit and writes a byte into the data-out register. If the host is receiving input, it reads the controller-written data from the data-in register, and sets the read bit to 0 as the next command.
  3. The host sets the command-ready bit to 1.

Controller actions:

  1. When the controller notices that the command-ready bit is set, it sets the busy bit to 1.
  2. The controller reads the command register. If the write bit inside is set, it reads from the data-out register and performs the necessary I/O operations on the device. If the read bit is set, data from the device is loaded into the data-in register for the host to read.
  3. Once the operations are over, the controller clears the command-ready bit, clears the error bit to show the operation was successful, and clears the busy bit.

Types

[edit]

A polling cycle is the time in which each element is monitored once. The optimal polling cycle will vary according to several factors, including the desired speed of response and the overhead (e.g., processor time and bandwidth) of the polling.

In roll call polling, the polling device or process queries each element on a list in a fixed sequence. Because it waits for a response from each element, a timing mechanism is necessary to prevent lock-ups caused by non-responding elements. Roll call polling can be inefficient if the overhead for the polling messages is high, there are numerous elements to be polled in each polling cycle and only a few elements are active.

In hub polling, also referred to as token polling, each element polls the next element in some fixed sequence. This continues until the first element is reached, at which time the polling cycle starts all over again.

Polling can be employed in various computing contexts in order to control the execution or transmission sequence of the elements involved. For example, in multitasking operating systems, polling can be used to allocate processor time and other resources to the various competing processes.

In networks, polling is used to determine which nodes want to access the network. It is also used by routing protocols to retrieve routing information, as is the case with EGP (exterior gateway protocol).

An alternative to polling is the use of interrupts, which are signals generated by devices or processes to indicate that they need attention, want to communicate, etc. Although polling can be very simple, in many situations (e.g., multitasking operating systems) it is more efficient to use interrupts because it can reduce processor spinnning and/or bandwidth consumption. Sometimes a single interrupt will be shared with multiple devices. When such an interrupt is raised, the interrupt service routine will poll the devices sharing the interrupt to determine which needs servicing. Vectored interrupts are a more efficient alternative which dispatches each device directly to its own private interrupt service routine.

Poll message

[edit]

A poll message is a control-acknowledgment message.

In a multidrop line arrangement (a central computer and different terminals in which the terminals share a single communication line to and from the computer), the system uses a master/slave polling arrangement whereby the central computer sends message (called polling message) to a specific terminal on the outgoing line. All terminals listen to the outgoing line, but only the terminal that is polled replies by sending any information that it has ready for transmission on the incoming line.[1]

In star networks, which, in its simplest form, consists of one central switch, hub, or computer that acts as a conduit to transmit messages, polling is not required to avoid chaos on the lines, but it is often used to allow the master to acquire input in an orderly fashion. These poll messages differ from those of the multidrop lines case because there are no site addresses needed, and each terminal only receives those polls that are directed to it.[1]

See also

[edit]

References

[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
In computer science, polling, also known as polled operation or interrogation, is a synchronous technique in which a program or processor repeatedly samples the status of an external device, resource, or process to detect events, availability of data, or readiness for interaction, typically occurring at regular intervals or high frequencies.[1] This method contrasts with event-driven approaches like interrupts, where devices signal the processor directly, and is commonly employed in input/output (I/O) operations, device management, and communication protocols to manage multiple sources without dedicated hardware notifications.[2] Polling operates by having the polling entity—such as a central processor or client application—issue sequential queries to attached devices or queues in a cyclic manner, processing responses only when data or events are present.[3] For instance, in operating systems, the CPU might poll input devices like keyboards at rates of 100 to 1,000 times per second to capture key presses, or in networking, a controller could poll terminals in a multipoint setup to check for outgoing messages.[1] Variants include roll-call polling, where devices are queried in a fixed order, and hub polling, a method in which elements are polled in a fixed sequence for resource sharing; longer forms like long polling hold a single request until a change occurs, reducing but not eliminating repeated checks.[1] While straightforward to implement and requiring no specialized interrupt-handling circuitry, polling's primary drawback is its inefficiency, as it consumes CPU cycles even during idle periods when no events occur, potentially leading to wasted resources in systems with infrequent activity.[2] In contrast, interrupt-driven I/O allows the processor to perform other tasks until notified, making polling more suitable for scenarios with predictable, high-frequency events, such as real-time systems or high-performance storage interfaces.[4] Applications span embedded systems, where simple polling loops manage sensors;[5] telecommunication networks modeled as polling queues for traffic handling;[6] and modern software like Unix's poll() system call for multiplexing I/O from multiple file descriptors.[7]

Fundamentals

Definition and Purpose

In computer science, polling refers to a synchronous input/output (I/O) mechanism where a processor or client program actively and repeatedly samples the status of an external device to check for readiness, data availability, or event occurrence.[8] This process involves the polling entity issuing queries at regular intervals and waiting for responses, ensuring direct control over device interactions without passive waiting.[9] The primary purpose of polling is to manage I/O operations in systems where devices do not initiate notifications, allowing the processor to determine when an action can proceed, such as transferring data from a peripheral or confirming task completion.[10] By enabling this active checking, polling facilitates reliable synchronization in environments lacking advanced signaling hardware, though it contrasts with asynchronous alternatives like interrupts that respond to device-generated events.[11] A key characteristic of polling is its synchronous nature, which requires the polling component to halt or loop until a favorable status is detected, often through busy-waiting on device registers like a "busy" bit.[8] This approach demands continuous CPU involvement during checks, prioritizing simplicity over efficiency in resource-constrained setups. Polling originated in early computer systems during the 1950s and 1960s as a straightforward method for I/O management, particularly in hardware-limited environments where interrupt support was rudimentary or absent.[12] For instance, machines like the PDP-1 (1959) and PDP-8 (1965) relied on polling device status registers for synchronization, while the CDC 6600 (1964) used peripheral units to poll for I/O requests, reflecting the era's emphasis on minimal hardware complexity for mainframe operations.[12]

Comparison to Interrupts

Polling and interrupts represent two fundamental mechanisms for managing input/output (I/O) operations between the CPU and peripheral devices in computer systems. In polling, the CPU actively and repeatedly queries the status registers of devices to determine if they require attention, such as checking bits for readiness, busyness, or errors, through a busy-wait loop that halts other processing until a response is obtained.[13] This approach ties the CPU directly to device monitoring, making it synchronous and CPU-intensive. In contrast, interrupts enable devices to asynchronously notify the CPU of events via dedicated hardware signal lines, such as the Interrupt Request (IRQ) line, prompting the CPU to suspend its current task, save context, and execute an interrupt service routine (ISR) only when needed.[14] From an efficiency standpoint, polling is often less optimal because it consumes CPU cycles on frequent, unproductive checks, particularly when devices are idle or slow, leading to wasted processing power and reduced system throughput in multitasking environments.[13] Interrupts, however, promote better resource allocation by allowing the CPU to execute other computations or handle multiple processes until an event occurs, thereby minimizing idle time and enhancing overall system performance for sporadic I/O activities.[14] This efficiency gap becomes pronounced in modern operating systems, where polling can degrade responsiveness in high-load scenarios, while interrupts support concurrent operations more effectively. Regarding latency and responsiveness, polling incurs predictable but potentially variable delays determined by the polling interval—typically half the check frequency on average—resulting in higher response times for urgent events, as the CPU may miss immediate device states between queries.[13] Interrupts offer lower latency by enabling near-instantaneous notification and handling, often in microseconds, though they introduce overhead from context switching, interrupt acknowledgment, and ISR execution, which can accumulate in high-interrupt-rate systems.[14] In terms of resource usage, polling is well-suited for simple systems with low-frequency or predictable device interactions, such as embedded controllers where hardware simplicity outweighs CPU overhead, but it scales poorly under heavy utilization due to constant resource demands.[13] Interrupts excel for high-priority, infrequent events like keyboard inputs or disk completions, conserving CPU resources for core tasks while leveraging dedicated hardware like interrupt controllers, though they require more complex software and can lead to priority inversion if not managed carefully.[14] Hybrid approaches integrate both techniques to balance trade-offs, such as using interrupts for initial notifications followed by polling for fine-grained status checks, or implementing hybrid polling modes—like those in Linux kernels since version 4.10—that periodically sleep before polling to reduce CPU load while targeting ultra-low latency I/O.[15] These methods can optimize energy efficiency in specific scenarios, such as storage devices where classic polling outperforms pure interrupts for sub-10 μs latencies by avoiding context-switch costs, achieving up to 20% higher throughput in benchmarks with NVMe SSDs.[15]

Implementation

Basic Algorithm

The basic polling algorithm in computer science enables a processor to actively monitor the status of an input/output (I/O) device through a repetitive checking process, ensuring synchronous interaction without relying on external notifications. The process begins with the processor initializing a pointer or address to the target device, typically via its control registers. It then enters a continuous loop where it reads the device's status register to inspect a ready flag or busy bit, which indicates whether the device is prepared for data transfer or has completed an operation. If the ready flag is asserted (for example, set to 1), the processor proceeds to read or write data from/to the device's data register, processes the information as required, and clears the status flag to acknowledge the completion. If the flag is not set, the loop continues without action, effectively busy-waiting until the condition changes.[8][16] To mitigate the risk of indefinite hanging due to device malfunctions or unresponsiveness, the algorithm incorporates timeout handling. This involves tracking the number of polling iterations or elapsed time within the loop; if a predefined threshold is exceeded, the loop terminates, often triggering an error routine or fallback mechanism in the operating system. Such safeguards are essential in production environments to maintain system stability.[17] In terms of hardware implementation, polling typically targets a status register or similar control port, accessed through memory-mapped I/O. Here, the device's registers are assigned dedicated addresses in the processor's memory space, allowing the CPU to use standard load (for reading status) and store (for writing commands or data) instructions without specialized I/O commands. This approach simplifies software design but requires careful address mapping to avoid conflicts with main memory.[18][8] A representative pseudocode snippet for a basic device write operation via polling illustrates the flow, including initial wait, command issuance, and completion check with implicit timeout integration:
initialize device_address
timeout_counter = 0
MAX_TIMEOUT = 10000  // Example threshold

// Wait for device to be ready
while (read_status(device_address) & BUSY_BIT) {
    timeout_counter++
    if (timeout_counter > MAX_TIMEOUT) {
        handle_timeout_error()
        break
    }
}

// Issue write command
write_data(device_address, data)
set_command(device_address, WRITE_COMMAND)

// Wait for completion
timeout_counter = 0
while (read_status(device_address) & BUSY_BIT) {
    timeout_counter++
    if (timeout_counter > MAX_TIMEOUT) {
        handle_timeout_error()
        break
    }
}
clear_status(device_address)
This structure assumes bit-masking for flag checks (e.g., BUSY_BIT as a predefined constant).[8][16] Variations in the polling loop's frequency address different system demands. Fixed-interval polling executes checks at constant time gaps, often in a tight loop for simplicity and predictability in low-latency scenarios. In contrast, adaptive polling dynamically adjusts the interval based on observed system load or device behavior, such as increasing frequency during high activity to reduce latency or decreasing it during idle periods to conserve CPU cycles. These adaptations are particularly relevant in resource-constrained environments.[19][20]

Poll Messages

In computer networking and bus systems, a poll message serves as a structured query packet transmitted by a controller or master device to elicit a specific response from a target device or node, enabling the controller to check status, retrieve data, or initiate actions without relying on unsolicited notifications.[21] This mechanism is fundamental in master-slave architectures, where the poll ensures orderly communication over shared media like serial buses or local networks.[22] The typical structure of a poll message includes a device address field to identify the recipient, a command type or function code specifying the requested operation (such as a status inquiry or data read), and optional data fields for parameters like register addresses or quantities; it concludes with an error-checking mechanism, such as a cyclic redundancy check (CRC) or longitudinal redundancy check (LRC), to verify integrity during transmission.[21] For instance, in the Modbus protocol, a poll message in remote terminal unit (RTU) mode consists of a 1-byte slave address, a 1-byte function code (e.g., 0x03 for reading holding registers), variable data bytes (e.g., 2 bytes for starting register address and 2 bytes for the number of registers), and a 2-byte CRC, forming a compact frame like 0x11 0x03 0x00 0x6B 0x00 0x03 [CRC_low] [CRC_high] to request three registers starting from address 107.[21] Similarly, in the MIL-STD-1553 avionics bus standard, a poll is embodied in a 20-bit command word comprising a 5-bit remote terminal address, a transmit/receive (T/R) flag, a 5-bit subaddress for the operation, and a 5-bit word count for data length, transmitted at 1 Mbps over a twisted-pair bus to prompt a status or data response from the terminal.[22] Responses to these polls generally mirror the structure, incorporating acknowledgment, requested data, or error indicators, while excluding the initiating command fields. Error handling in poll-based systems primarily addresses non-responses or corrupted messages through timeout detection and retransmission protocols. If a targeted device fails to reply within a predefined interval (typically 100-500 ms in Modbus implementations), the controller treats it as a communication failure and may retransmit the poll message up to a configurable number of attempts, often with exponential backoff to mitigate bus contention.[21] In MIL-STD-1553, no response triggers a status word check via subsequent polls, with retransmission initiated by the bus controller if the remote terminal's status indicates an error like a message error bit; this ensures reliability in deterministic environments like aircraft systems, where up to 31 terminals share the bus.[22] Sequence numbers, when included in extended protocols, further aid in detecting lost polls by matching responses to specific queries, preventing duplicate processing.[23]

Types

Roll-Call Polling

Roll-call polling is a sequential, centralized technique in computer networks where a controller systematically queries multiple devices in a predetermined order, akin to taking attendance in a classroom. The controller sends a poll message containing the unique address of the next device, prompting it to transmit any pending data or indicate no activity. This method relies on a fixed list of device addresses maintained by the controller, ensuring orderly access to a shared communication medium without device-initiated coordination.[1] The process initiates with the controller addressing the first device (e.g., device 1), transmitting the poll and waiting for a response; if data is available, the device sends it, and the controller acknowledges completion before advancing, or a timeout occurs if no response is received, allowing progression to the next device. This sequence continues through all devices in the list, with the cycle completing upon reaching the end and immediately restarting from the beginning to maintain continuous monitoring. Address assignment occurs during network initialization, where each device is configured with a distinct identifier known to the controller, facilitating targeted queries and preventing conflicts in multi-device setups.[1] For systems with a predefined and stable set of devices, roll-call polling provides a simple implementation, as it requires minimal overhead beyond basic timeout handling and sequential logic, avoiding the complexity of dynamic scheduling or contention resolution.[24] This approach is prevalent in early multi-device serial buses like RS-485, particularly for industrial control applications such as fieldbus networks in manufacturing, where a master controller polls slave devices for sensor data or status updates in real-time environments.

Hub Polling

Hub polling is a token-passing variant of polling used to manage access in shared medium networks, where a central hub or controller initiates the process by passing a "poll token" to the first station in a sequential order.[25] The token holder examines its queue; if data is ready, it transmits the frame and then passes the token to the next station, ensuring only one device communicates at a time and preventing collisions on the shared medium.[1] If no data is present, the station immediately forwards the token to the subsequent station without delay.[26] The process relies on continuous token circulation around the network topology, typically a logical ring or bus, to maintain orderly access.[27] To handle failures, such as a lost token due to transmission errors or station malfunction, a timeout mechanism is implemented: stations monitor the token rotation time, and upon expiration, a designated monitor station (e.g., the active monitor in ring configurations) generates a new token and purges the network to restore operation.[25] This recovery ensures reliability without requiring constant central intervention.[28] Unlike roll-call polling, which involves strict central control where the controller directly polls each station individually, hub polling enables self-regulation among nodes by delegating token passing to the stations themselves, thereby reducing the overhead on the central controller.[27] In implementations like the IEEE 802.5 token ring standard, the token frame structure consists of three bytes: a starting delimiter (SD) for synchronization, an access control (AC) byte containing bits for priority (3 bits), token/monitor indication, and reservation, and an ending delimiter (ED) to mark the frame's conclusion.[29] Hub polling was prevalent in 1980s local area networks (LANs), such as IBM's token ring networks standardized as IEEE 802.5 in 1985, to provide deterministic, collision-free medium access in shared environments like office LANs.[30]

Applications and Considerations

Use Cases

In operating systems for simple embedded systems, device drivers commonly use polling to monitor input from peripherals like keyboards and mice by periodically checking device status registers for events such as key presses or cursor movements. This method suits resource-constrained environments where low-latency responses to user inputs are needed without advanced hardware support.[31][32] In networking applications, web servers implement short polling to deliver client updates in AJAX-based interfaces, where the client sends frequent HTTP requests to the server to fetch new data, enabling pseudo-real-time interactions like live notifications. Industrial protocols such as Profibus also rely on polling for sensor data acquisition, with a central master device querying connected slaves in sequence to retrieve measurements from field instruments. Roll-call polling appears in these industrial setups to systematically solicit responses from multiple devices.[33][34][35] Embedded systems in real-time control often involve microcontrollers polling analog-to-digital converters (ADCs) to sample analog signals from sensors, converting them into digital values for immediate processing in applications like motor control or environmental monitoring. This periodic querying ensures timely data capture aligned with control loop timing requirements.[36][37] Post-2010 adaptations in cloud services have integrated polling of message queues in brokers like RabbitMQ to detect task availability, using explicit pull requests for scenarios where consumers need to selectively retrieve and process queued workloads in distributed environments.[38]

Advantages and Disadvantages

Polling offers several advantages in computer science applications, particularly in scenarios requiring simplicity and predictability. One key benefit is its ease of implementation, as it does not require complex hardware such as interrupt controllers, allowing software to directly check device status using basic instructions. This simplicity reduces development overhead and is especially useful in embedded systems or environments with limited hardware support. Additionally, polling provides predictable timing, which is advantageous in real-time systems where deterministic behavior is critical, avoiding the variability introduced by interrupt latency and context switching. For frequent events on fast devices, polling can achieve low latency, such as in the microsecond range for high-frequency operations, outperforming interrupt-driven approaches that incur additional overhead from handling signals.[39] Despite these benefits, polling has notable disadvantages related to resource efficiency and scalability. It imposes high CPU overhead due to constant checking, often leading to busy-waiting that wastes processor cycles, particularly when devices are idle or events are rare; in such cases, polling can consume up to 100% of a CPU core, compared to less than 1% utilization with interrupts during idle periods. Scalability issues arise in multi-device systems, where sequentially polling numerous peripherals increases overall system load and reduces performance for other tasks. Furthermore, if the polling rate is set too low, events may be missed, resulting in data loss or delayed responses. To mitigate these drawbacks, techniques such as adaptive polling rates adjust the frequency based on recent activity, reducing unnecessary checks while maintaining responsiveness. Hybrid approaches, combining polling with interrupts, further improve efficiency by using polling for high-frequency, low-latency operations and falling back to interrupts for infrequent events, as demonstrated in storage I/O benchmarks where such methods balance CPU utilization and throughput.

References

User Avatar
No comments yet.