Endless loop: Define and turn off the endless loop

How would you define a cycle, it is an infinite cycle and will come out of it.

Does anyone have an algorithm or can help me with this.

thanks

+4
source share
2 answers

There is no general case algorithm that can determine if a program is in an infinite loop or not for each turing complete , this is basically a Stop problem .

The idea to prove this is simple:

  • Suppose you had algorithm A .
  • Create a program B that calls A by itself [on B ].
  • if A says "program will stop" - do an endless loop
  • else [ A answers B doesn't stop] - don't stop yourself

Now suppose you call A on B - the answer will definitely be wrong, so A does not exist.

Note: the above is not a formal proof, just a sketch of it.

+7
source

As written by others, it is impossible to determine.

However, if you want to do some testing, you can use the WatchDog design template. This is a separate thread that checks if a task is active. Your own stream should regularly signal to say that it is alive. Make sure this signal is not set inside your (infinite) loop.

If there was no signal, the program is in an endless loop or stopped, and the watchdog timer can act on it.

+4
source

Source: https://habr.com/ru/post/1401821/


All Articles