关闭

Halting problem(停机问题)

标签: halting停机问题图灵
701人阅读 评论(0) 收藏 举报
分类:

1.Introduction

In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running or continue to run forever.

The halting problem is a decision problem about properties of computer programs on a fixed Turing-complete model of computation, i.e., all programs that can be written in some given programming language that is general enough to be equivalent to a Turing machine. The problem is to determine, given a program and an input to the program, whether the program will eventually halt when run with that input. In this abstract framework, there are no resource limitations on the amount of memory or time required for the program’s execution; it can take arbitrarily long, and use arbitrarily as much storage space, before halting. The question is simply whether the given program will ever halt on a particular input.

停机问题是用于判断这个程序是否会自动退出或者会一直运行下去。例如一个死循环,在内存没有限制的条件下,这个程序不能自动退出,即它会一直运行下去。对于普通正常的程序,举一个非常简单的例子,如helloworld,在运行结束后会自动退出。你会发现, 这是一个很值得研究的问题。因为计算机的资源是有限的,如果一个程序不会自动停止,它便会消耗大量资源与时间,大大降低了效率。假如我们能够设计一个范型算法,适用于对所有程序进行测试,对不能自动退出的程序发出警告,那么这便会大大提高我们的工作效率。可惜的是,目前并不存在这种算法。

Alan Turing proved in 1936 that a general algorithm to solve the halting problem for all possible program-input pairs cannot exist. A key part of the proof was a mathematical definition of a computer and program, which became known as a Turing machine; the halting problem is undecidable over Turing machines. It is one of the first examples of a decision problem.

对于这个问题,图灵本人便做过验证,发现并不存在一种通用的算法能实现这一功能,他认为这是图灵机上仍不能解决的一个问题,同时他也给出了证明:Halting_problem-Sketch_of_proof

更多的细节详见:Halting problem

那么,我们能不能通过一些条件限制,如时间限制,内存限制来大约估计程序是否会停机?

显然是可以的。虽然停机问题是无法解决的,但是我们可以通过条件限制,即根据它的趋势来预测是否会出现停机,以达到我们测试的目的。

下面我通过讲解一道例题,帮助读者进一步了解停机问题!

2.Example

In this project, the rule is:
TIME LIMIT: 1 second
MEMORY LIMIT: 8 MB
FUNCTION INPUT: 5

What should you do in this project:
1. Define the function “Decide()”;
2. In “Decide()”, different functions will be as input;
3. The program you write should stop the unstopable function;
4. The program you write should run through the stopable function;

Simple understanding of “halt problem”:
Can a program know whether itself can be stop? It just like the “Barber paradox”.

题目已给出main.cpp 与 function.h

// main.cpp
#include <iostream>
#include "Decide.h"
using namespace std;

// time_1 record the time of beginning
// time_2 record the time of ending 
static time_t time_1, time_2;

// Time limit: 1 second
// Memory limit: 8 M
int main() {
    int n;
    time_1 = getCurrentTime(); // define in function.h
    while (cin >> n && n != -1 /* 1 */) {
        switch (n) {
            case 0:
            Decide(display, 1, 1); // test display()
            break;
            case 1:
            Decide(add, 1, 1); // test add()
            break;
            case 2:
            Decide(inf, 1, 1); // test inf()
            break;
            case 3:
            Decide(find, 1, 1); // test find()
            break;
            case 4:
            Decide(alloc, 1, 1); // test alloc()
            break;
            default:
            cout << "Wrong number.\n";
        }
    }
    time_2 = getCurrentTime();
    if (time_2 - time_1 > 1000) // get the time of while and judge whether it exceed 1s
        cout << "Exceed time (1 second).\n";
    else cout << "Finish all function in 1 second.\n";
    return 0;
}

// function.h
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <sys/time.h>
#include <vector>
using namespace std;

// 8M = 8*1024*1024, here "50" may have other function
// it may be different in different situation
static long long int maxMem = 8 * 1024 * 1024 - 50; // 8M
static time_t time_begin, time_now;
static vector<long long int> v;

// check is the type of function pointer that bool()
// func is the type of function pointer that void(check, int, int) 
typedef bool (*check) ();
typedef void (*func) (check, int, int);

long getCurrentTime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
// tv.tv_sec * 1000 + tv.tv_usec / 1000 transform seconds into millisecond

#define TIC() time_begin = getCurrentTime();
#define TOC() time_now = getCurrentTime();
#define MEM_CHECK() (v.size()*sizeof(long long int) > maxMem ? 1 : 0) // calculate the storage that vector takes

void display(check c, int a, int b) {
    cout << "Input is " << a << " and " << b << ".\n";
}

void add(check c, int a, int b) {
    cout << a << " + " << b << " = " << a + b << ".\n";
}

// Infinite loop inside.
void inf(check c, int a, int b) {
    TIC();
    while (1) {
        TOC();
        // if the time is enough, c() will return true
        // That means the function may run forever
        if (c()) {
            cout << "Infinite break.\n";
            time_now = time_begin = 0;
            break;
        }
    }
}

// Find prime number.
void find(check c, int a, int b) {
    int pos = 1, num = 3;
    TIC();
    while (1) {
        for (int i = 2; i < num; i++)
            if (num % i == 0) break;
            else if (i == num - 1) pos++;
        TOC();
        if (c()) {
            if (num > 20000) cout << "Find succeed!\n";
            else cout << "Find nothing.\n";
            time_now = time_begin = 0;
            break;
        }
        num++; // increase everytime the loop finish
    }
}

// Allocate memory for the program.
void alloc(check c, int a, int b) {
    while (1) {
        long long int ch = 999999999;
        v.push_back(ch);
    // if the memory is enough, c() will return true
    // That means the function may be run forever
        if (c()) {
            if (MEM_CHECK()) cout << "Memory has been full.\n";
            else cout << "Memory hasn't been full.\n";
            v.clear();
            break;
        }
    }
}

其实题目便是要求我们设计出decide()函数以实现对function.h中的函数的测试。测试的标准便是函数运行的时间是否会超出我们预先规定的时间与内存大小。

对于main.cpp 与 function.h 的解析,我都以注释的形式标注在代码旁边。

decide()设计如下:

#include <iostream>
#include "function.h"
using namespace std;

bool breakLoop() {
    if ((time_now - time_begin > 180) ||  // maxTime:1 second
        v.size()*sizeof(long long int) > 8 * 1024 * 1024 - 50)  // maxMemory:8 M
        return true;
    return false;
}

void Decide(func f, int a, int b) { f(breakLoop, a, b); }

这里需要解释的便是180了,其实180 = 200 -20,这里的20跟前面所讲的50的道理是一样的。即这20ms可能会因代码运行的环境不同而不同。我认为,这20s应该是分配给代码中其他组分的时间。对于200,由题意可知,输入最多为5个,假如五个输入对应的都是带有死循环的函数,而整个程序的时间限制为1s, 那么每个输入的最大的时间限制便是1s/5 = 1000ms/5 = 200ms.

3.Conclusion

停机问题是一个很有趣的问题,有兴趣的同学可以进一步地学习。

链接:
Halting problem
如何通俗地解释停机问题(Halting Problem)?


以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨!


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:120095次
    • 积分:2470
    • 等级:
    • 排名:第15494名
    • 原创:124篇
    • 转载:4篇
    • 译文:0篇
    • 评论:42条
    文章分类
    最新评论