ubuntu中如何防止A文件进入死循环
how to protect C file from entering into infinite-loop in ubunto
我目前正在编写一个 python3 脚本,该脚本通过 运行 C 代码和各种输入文件检查 C 源文件。如果重要的话,编译由 GCC 完成。
在某些情况下,C 代码会进入无限循环(我想通了,因为我 运行 内存不足)。
有没有一种方法可以 "protect" 我的代码像看门狗之类的东西
X 分钟后告诉我 运行 进入无限循环?
我无法对输入做出任何假设,所以我无法得到答案,例如更改它或其他内容...
#runfile is an exe file, code file is .c file, inputlist/outputlist are directories
import subprocess as sbp
import os
sbp.call('gcc -o {0} {1}'.format(runfile,codefile), shell = True)
for i in range(numoFiles):
#run the file with input i and save it to output i in outdir
os.system("./{0} <{1} >{2}".format(ID,inputList[i],outputList[i]))
查找 "Halting Problem"。无法确定任意程序最终是否会完成,或者是否会永远陷入循环。
我想出了一个避免进入无限循环的方法 method:
import subprocess
for i in range(numofiles):
cmd="gcc -o {0} {1}".format(runfile,codefile)
subprocess.call(cmd, shell = True)
try:
cmd="./{0} <{1} >'/dev/null'".format(Cfile,inputfile) #check if runtime>100 sec
subprocess.run(cmd,shell=True,timeout=100)
except subprocess.TimeoutExpired:
print("infinite loop")
continue
cmd="./{0} <{1} >{2}".format(Cfile,inputfile,outputfile)
subprocess.run(cmd,shell=True) #print output to txt file
我目前正在编写一个 python3 脚本,该脚本通过 运行 C 代码和各种输入文件检查 C 源文件。如果重要的话,编译由 GCC 完成。 在某些情况下,C 代码会进入无限循环(我想通了,因为我 运行 内存不足)。 有没有一种方法可以 "protect" 我的代码像看门狗之类的东西 X 分钟后告诉我 运行 进入无限循环?
我无法对输入做出任何假设,所以我无法得到答案,例如更改它或其他内容...
#runfile is an exe file, code file is .c file, inputlist/outputlist are directories
import subprocess as sbp
import os
sbp.call('gcc -o {0} {1}'.format(runfile,codefile), shell = True)
for i in range(numoFiles):
#run the file with input i and save it to output i in outdir
os.system("./{0} <{1} >{2}".format(ID,inputList[i],outputList[i]))
查找 "Halting Problem"。无法确定任意程序最终是否会完成,或者是否会永远陷入循环。
我想出了一个避免进入无限循环的方法 method:
import subprocess
for i in range(numofiles):
cmd="gcc -o {0} {1}".format(runfile,codefile)
subprocess.call(cmd, shell = True)
try:
cmd="./{0} <{1} >'/dev/null'".format(Cfile,inputfile) #check if runtime>100 sec
subprocess.run(cmd,shell=True,timeout=100)
except subprocess.TimeoutExpired:
print("infinite loop")
continue
cmd="./{0} <{1} >{2}".format(Cfile,inputfile,outputfile)
subprocess.run(cmd,shell=True) #print output to txt file