Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

batch file - Check whether a process is running and terminate it if it's exceeded time limit

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:

  • main.bat
@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

  • judge.bat
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In

echo|set /p=Test %%i: >>result.log
judge %prob%.o
if errorlevel 0 set /a pass+=1

You are executing judge which appears to be a batch file.

You need

CALL judge %prob%.o

in order that processing will continue in your main procedure after judge ends. As it is, processing will be switched to judge.bat and end when judge.bat ends.

if errorlevel 0 set /a pass+=1

IF ERRORLEVEL n is TRUE if the runtime (ie. current) errorlevel is n or greater than n. IF ERRORLEVEL 0 is therefore always true. IF NOT ERRORLEVEL 1 is a test for errorlevel=0.

Hope this helps a bit - not really sure about your other problems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...