How would you determine a loop is a infinite loop and will break out of it.
Does anyone has the algorithm or can assist me on this one.
Thanks
How would you determine a loop is a infinite loop and will break out of it.
Does anyone has the algorithm or can assist me on this one.
Thanks
There is no general case algorithm that can determine if a program is in an infinite loop or not for every turing complete language, this is basically the Halting Problem.
The idea of proving it is simple:
A
.B
that invokes A
on itself [on B
].A
answers "the program will halt" - do an infinite loopA
answers B
doesn't halt] - halt immidiatelyNow, assume you invoke A
on B
- the answer will be definetly wrong, thus A
doesn't exist.
Note: the above is NOT a formal proof, just a sketch of it.
As written by others, it cannot be determined.
However, if you want to have some checking, you can use the WatchDog design pattern. This is a separate thread that checks if a task still is active. Your own thread should give a signal regularly to say it is alive. Make sure this signal is not set inside your (infinite) loop.
If there was no signal, the program is inside an infinite loop or has stopped and the watchdog can act on it.