What is the difference between a busy activity and a survey?

From Wikipedia article on Polling

A survey or survey in computer science refers to an active sampling of the state 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 polling I / O or program-controlled I / O.

A survey is sometimes used synonymously with a lively wait survey (expectation). In this situation, when an I / O operation is required, the computer does nothing but check the status of the I / O device until it is ready, and at that moment it will have access to the device. In other words, the computer waits until the device is ready.
The survey also refers to a situation where the device is repeatedly checked for availability, and if the computer does not return to another task. Although it is not as wasteful as CPU cycles like waiting, it is usually not as efficient as an alternative to polling, interrupting with I / O.

So, when a thread does not use “condition variables”, will it be called “polling” to change data or “waiting”?

+10
multithreading polling condition-variable
May 15 '12 at 5:07
source share
2 answers

The difference between the two is what the application does between polls.

If the program checks the device every second and does something else in the middle case, if there is no data available (including, perhaps, just a dream, leaving the CPU accessible to others), it is a poll. If a program continuously examines a device (or resource or something else) without doing anything between checks, it causes a wait-wait.

This is not directly related to synchronization. A program that blocks a condition variable (which should signal when a device or resource is available) is neither polling nor expected. This is more like event / interrupt control I / O.
(But, for example, the thread that goes around try_lock is a polling form, and possibly waiting if the loop is tight.)

+26
May 15 '12 at 5:19
source share

Suppose you have a microprocessor or microcontroller that needs to perform an action when it notices that a button is pressed.

The first approach is for the program to enter a cycle that does nothing except to see if the button has changed and by performing the required action.

In some cases, a second approach would be to program the hardware to trigger an interrupt when a button is pressed, assuming the button is connected to an input that is connected, so it can cause an interrupt.

The third approach is to set the timer to interrupt the processor at a certain speed (say, 1000x / second) and the handler for this interrupt checks the state of the button and affects it.

The first approach uses wait-wait. It can provide a very good response time to one particular stimulus by fully customizing everything else. The second approach uses an interrupt caused by events. It often offers a slightly slower response time than wait, but allows the processor to do other things while waiting for I / O. It can also allow the processor to go into low power mode until the button is pressed. The third approach will offer a response time that is much inferior to the other two, but will be used even if the hardware does not allow you to initiate an interrupt by pressing a button.

In cases where a fast response is required, it is often necessary to use an interrupt caused by events or a wait. In many cases, however, the survey approach may be the most practical. The hardware may not exist to support all the events that may be of interest, or the number of events that interest you may well exceed the number of available interrupts. In addition, it may be desirable for certain conditions to generate a delayed response. For example, suppose everyone wants to count the number of times the switch fires, provided that the following criteria are met:

  • Each legitimate switching event will consist of an interval from 0 to 900us (microseconds), during which the switch can be arbitrarily closed and reopened, followed by an interval of at least 1.1 ms, during which the switch remains closed, followed by an interval of 0 to 900US, during which the switch can randomly open and close, followed by an interval of at least 1.1 ms, during which the switch will be open.
  • The software should ignore the switch state for the 950us after any unoccupied opening or closing of the switch.
  • The software is allowed to arbitrarily count or ignore switch events that occur outside of the above blanking interval, but which are less than 1.1 ms.
  • The report calculated by the program must be valid for 1.99 ms when the switch is stable "closed".

The easiest way to ensure that this requirement is met is to monitor the status of the switch 1,000x / second; if it is “closed” when the previous state is “open”, increase the counter. Very simple and easy; even if the switch opens and closes in all sorts of strange ways, during the 900us preceding and following a real event, the software does not care.

One could use a switching interrupt with an input signal together with a timer to respond more quickly to the switch input, fulfilling the required blanking requirement. Initially, the input would be armed to start the next time the switch closes. Once the interrupt has been started, the software will disable it, but set a timer to trigger the interrupt after 950us. Once this timer has expired, it will trigger an interrupt, which would cause the interrupt to light up the next time the switch is "open". This interrupt, in turn, will disable the interrupt of the switch and set the timer for the 950us again, so interrupting the timer will again enable the interrupt of the switch. Sometimes this approach can be useful, but the software is much more complicated than the simple surveyed approach. When a timer-based approach is sufficient, it is often preferable.

On systems that use a multi-tasking OS rather than direct interrupt, many of the same principles apply. Periodic polling of I / O will spend some CPU time compared to code that the OS will not work until certain events occur, but in many cases both the response time of the event and the amount of time wasted when it does not no event would be acceptable when using a periodic survey. Indeed, in some buffered I / O situations, periodic polling can be quite effective. For example, suppose one receives a large amount of data from a remote machine through a serial port, no more than 11,520 bytes will arrive per second, the device will send up to 2K of data before the last confirmed packet, and the serial port will receive a 4K input buffer. While it is possible to process data using the “received data”, it can be just as efficient to simply check the 100x / second port and process all received packets to this point. Such a survey would be a waste of time when the remote device did not send data, but if the expected data was expected, it might be more efficient to process it in chunks of about 1.15 K than to process every small piece of incoming data as soon as it arrives.

+6
May 15 '12 at 5:20
source share



All Articles