开发者

Detecting if a Program is in an Infinite Loop (Read: Solving the Halting Problem)

Is detecting whether a deterministic program (i.e. state machine) is in an infinite loop equivalent to solving the halting problem?

I came up with a solution, and I'm not sure why it shouldn't work:

  • Let the program run
  • When you think it's in an infinite loop, take a snapshot of its memory regular intervals
  • If you ever detect the same snapshot, the program is in an infinite loop
  • As long as you don't get the same snapshot twice, it's either (1) not in an infinite loop, or (2) you need to take snapsho开发者_运维百科ts more quickly (perhaps once on every memory access?)

I'm assuming this doesn't work... but why?

It seems like a perfectly reasonable way to detect if a program is in an infinite loop (e.g. especially if you store hashes rather than the memory itself, although that will not be 100% accurate)... what's wrong with it, if anything?


In theory, it is not equivalent to the halting problem because real computers have finite number of possible states (even though it's huge). Turing machines, which the halting problem applies to, have infinite storage.

But, let's explore your idea further. You also have to take a snapshot of the "hidden" state: the CPU's program counter and other registers, and that you must have to take a snapshot before each single instruction. (The program would be in an infinite loop if the memory snapshot is the same AND the same instruction is about to be executed. It doesn't help if the memory contents is the same, but something else is going to be executed than the last time you saw the same snapshot.)

In practice, even a very small computer has such a huge number of potential states that you'd never be able to store (not even hashes!) all your snapshots. For example, even a minicomputer like the ancient commodore 64 with 64kB of RAM has 256^65536 potential states (not including the 5 CPU registers). Tracking cycles that are potentially so long is absolutely infeasbile, both in time and space.


The solution wouldn't work even in principle. A Turing machine doesn't have to ever be in precisely the same state (with the tape in the same configuration) to get into an infinite loop.

Your algorithm might work for context-sensitive languages and linear-bounded automata, but if you can't know how much tape a TM is going to need, you'd never know if you had an infinite loop or were about to hit the top. Note that your method would clearly work for real computers for a variety of reasons... chief among them being that your computer is less powerful than a (big) finite automaton.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜