So I'm working a project that make competitive judger from batch file, and it works well with correct and incorrect answer. But I stuck with my problem when I try to measure time execution this code. I used timeout and taskkill to delay it for an amount of time, and kill it if didn't finish.
But I faced two problem:
- My code process can be killed twice, and it can crash my batch file.
- It denied my input and output file, so I can't compare my file for verdict.
So how can I measure time, kill a process on Windows and get rid all of my problem?
Here is my code:
@echo off
:init
set home=%cd%
chcp 65001>nul
set total=0
set pass=0
:input
set /p user="Enter username: "
if not exist .submits\%user% (
echo [[91mERROR[0m] Invalid username.
pause 0
exit /b 1
)
set /p prob="Problem: "
if not exist .problems\%prob% (
echo [[91mERROR[0m] Invalid problem.
pause 0
exit /b 1
)
if not exist .submits\%user%\%prob%.cpp (
echo [[91mERROR[0m] User haven't submited this problem yet.
pause 0
exit /b 1
)
:judge
rem **compile file, setup for judging**
echo [[93mJury[0m] Judging problem %prob% from user %user%...
if exist result.log del /q result.log
cd submits\%user%
g++ %prob%.cpp -o %prob%.o -std=c++14 -O2
if not errorlevel 0 (
echo [91mERROR[0m Compile failed...
pause 0
exit /b 1
)
move %prob%.o "%home%">nul
cd %home%problems\%prob%ests
for /d %%i in ("*") do (
set /a total+=1
rem echo [[93mJury[0m] Test %%i...
copy "%%i*.txt" "%home%">nul
cd %home%
ren out.txt ans.txt
rem more ans.txt
echo|set /p=Test %%i: >>result.log
judge %prob%.o
if errorlevel 0 set /a pass+=1
del /q *.txt
cd %home%problems\%prob%ests
)
cd %home%
echo [[92mOK[0m] Judging completed, user passed %pass%/%total% test(s), please check result.log for more detail.
del /q %prob%.o
pause 0
rem @echo off
start %1
timeout /t 1 /nobreak
taskkill /im %1 /f
rem echo %errorlevel%
if not errorlevel 128 (
echo TLE.>>result.log
exit /b 1
)
fc /a /w out.txt ans.txt>nul && (
echo Correct.>>result.log
exit /b 0
) || (
echo Wrong Answer.>>result.log
exit /b 1
)
(I know my English is so bad, so I can't explain all of my problem, if you have any question, feel free to ask me, I will answer for you)
question from:
https://stackoverflow.com/questions/65918669/check-whether-a-process-is-running-and-terminate-it-if-its-exceeded-time-limit 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…