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

• 本文已收录于以下专栏：

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

• niushuai666
• 2012年02月15日 13:45
• 16485

## 停机问题--The Halting Problem

http://www.juniata.edu/faculty/rhodes/intro/theory2.htm 看停机问题时，觉得它跟我国的“以子之矛攻子之盾”的想法很相似 ...
• 2011年05月29日 15:21
• 1899

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

• MyLinChi
• 2018年01月12日 15:00
• 57

## Halting problem(停机问题)

1.Introduction In computability theory, the halting problem is the problem of determining, from a ...
• linwh8
• 2016年06月02日 00:40
• 959

## HomeWork——Halt Problem

• jimmy_curry
• 2016年05月30日 10:44
• 201

## 停机问题的定义和证明

• NGram
• 2008年03月18日 16:23
• 497

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

• u014786849
• 2015年08月19日 20:30
• 1007

## SVN Problem

http://stackoverflow.com/questions/35962861/how-to-fix-unable-to-create-pristine-install-stream 1、...
• NRlovestudy
• 2016年08月19日 16:37
• 875

## 虚拟机类加载机制

• u013678930
• 2016年07月20日 11:43
• 632

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

• bat67
• 2016年08月03日 17:56
• 288

举报原因： 您举报文章：Halting problem(停机问题) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)