Developers Club geek daily blog

1 year, 2 months ago
imageIn kratets I will describe contents of article:

There is a cyclic hardware counter which, for example, considers seconds, and there is an interruption on its overflow. We expand notation range with a program method, inkremeniruya value of other cell in interruption. Thus, we have an opportunity to consider and minutes. The essence of a problem is that generally at the same time it is impossible to read value of minutes and seconds, and at consecutive reading there can be an interruption and increase in minutes. Effects: travel in time back.

Example: 1 min. 20 sec., we read out minutes. There is an interruption which lasts 50 seconds, minutes inkremenirutsya —
2 min. 10 sec., to schityva of second.

We receive time: 1:10 (1 min. 10 sec.), we could receive such time only after travel in time back because we began measurements at 1:20, and received result at 2:10. It should be noted that we will not be able to escape forward at such sequence of reading (as the author claimed) as with reading we lag behind on discharge of minutes, and it is always more than all seconds which are located in the corresponding discharge (lag for a minute is minus minute, and they only with plus and it is no more than a minute of second).

If to read out at first seconds, then minutes, then we on the contrary can give future time around one minute, and in the past flights are cancelled.

So, the criterion was described by thought that the read-out time has to be between time of start of function and its termination that is seemingly logical, and on the basis of it all reasonings were constructed. At the same time we courageously fought for compliance of the read seconds to value of minutes, and it is not really simple because generally it is necessary to assume that the counter considers very quickly, or system of a vysokonagruzhenn therefore for runtime of our function there are numerous and long interruptions in the course of which "minutes" change (for minutes we call it conditionally, for descriptive reasons). We cannot prohibit interruptions as minutes will cease to be considered (at us minutes inkremenirutsya programmatically), and it is already complete Armageddon.

How it is possible to be sure that the known seconds correspond to the necessary minute? Only hoping that we made everything very quickly and we were lucky therefore interruptions between reading minutes, seconds, and then again minutes (test reading), were not or it was flash-like, and minute did not manage to expire. It is checked easily: test reading minutes has to give the same value. Only the matter is that at worst we will infinitely try to receive such combination of circumstances, and we will never receive result.

The output was thought up in original article such: we only once check and if minute changed, then we assume that it changed here just, so there is even the whole 59 seconds fast to read once again minutes and seconds, and la's vua — we in kings. Such acceptance will work at practice quite not bad, but in the theory it at all not the solution because no 59 seconds are guaranteed to us (we right there can receive interruption on all 70) and reading seconds and minutes at us besides, generally, breaks the same crime. But statistically — yes, we sharply reduced probability of the wrong data by means of such heuristics.

In comments of the author put on the right track, and he is very simple. We too abstracted from what is actually necessary and we try to solve a certain spherical problem of synchronization which, in fact does not require a solution.

I will speak: we start function to learn "how many now time" if we are answered quickly, then we can are interested seconds if instantly — we are able to use also milliseconds in the practical purposes, etc. If we are answered for an hour, then it is not important at all how many there milliseconds, "now" we will not be able to measure time more precisely than hour, but as it was stated above — travel in time, nevertheless, are not admissible.

Therefore very simple algorithm arises:

Read minutes, read seconds, test reading minutes if all are normal, then we return result. If minutes changed, then the current time of n of minutes and 00 seconds or later, but no more than for 59 seconds, and most likely it is read seconds because as well as in heuristics of the author, we have to be in the neighborhood of transition of one minute in another. Our task consists in as soon as possible to return this result (n of minutes of 00 seconds), any attempt of new synchronization, reading or check can end (and at worst — has to end) with work interruption, and irrespectively obtaining methods we at once move away from the current time and we worsen any following value what exact, concerning the synchronization moment, it was not.

Thus, return of 00 seconds will be even more precisely, than really read value (we do not spend time for reading seconds and for further checks), we want to receive time "now", but not time when was succeeded to be synchronized. Actually, we suit any time taken at random main that it was in a function work interval, and itself function worked as little as possible. It is just that heuristics which allows to make very exact assumption, and it precisely lays down in a function work interval.

Criticism: it is possible to use time differently if we try to measure an interval, having twice received "now", then the similar algorithm will give the smallest error if we wait for "alarm clock" — similarly. If we just want to begin with some moment, at once behind which there will be, for example, a record in a log file which has to correspond in accuracy to the real time registered in the same place, then, perhaps, it makes sense to be synchronized before loss of pulse or to think up something poizoshchryonny, but as on me is already absolutely other task.

Well and, of course, we spoiled statistical distribution of seconds, having made 00 more probable therefore something like Randomize can give ghost effects.

This article is a translation of the original post at habrahabr.ru/post/274805/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus