# Halting problem(停机问题)

### 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.

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.

### 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
#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:
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()设计如下：

#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); }

### 3.Conclusion

#### 图灵停机问题(halting problem)

2018-01-12 15:00:57

#### 图灵停机问题（The Halting Problem）

2012-02-15 13:45:31

#### 停机问题--The Halting Problem

2011-05-29 15:21:00

#### 图灵停机问题（The Halting Problem）------巧妙的证明

2018-05-06 20:33:13

#### <em>The</em> Princeton Companion to Mathematics

2018年05月08日 00:00

#### [计算理论] 停机问题不可解决的简单证明 halting problem

2015-08-19 20:30:25

#### 图灵停机问题（The Halting Problem）

2016-08-03 17:56:21

#### 史上最牛诗歌：一个停机问题不可判定的证明

2009-03-05 11:45:00

#### 图灵机停机问题的不可判定性

2017-04-09 23:04:57

#### TLS chapter9:turing halting problem图灵停机问题

2015-08-05 18:13:55