1.

Solve : How to set a time limit for program execution?

Answer»

I want to set a time limit of 100 seconds for executing a Java program.

In a UNIX I just write

     ulimit -t 100
     java Program

How can I achieve the same with DOS?


start java program
ping localhost -n 101 > nul
taskkill java program
 Thanks mat123

I have tried your solution, but it is not satisfactory for me. The ping command always works for 101 seconds, regardless of the started Java program, that typically finishes in a few seconds.

I need a solution for running a bunch of student programs and want to be sure that no more than 100 seconds are USED per run.start java.exe
set a=1
:A
if %a%==100 goto out
ping localhost -n 2 > nul
tasklist|find "java.exe"
if errorlevel 1 exit
set /a a=%a%+1
goto a
:out
taskkill java.exeThanks again mat123. I implemented your solution. However, it seems to have some drawbacks:

(1) The -n option for the ping command does not specify seconds, but number of PINGS.
(2) The value of  ERRORLEVEL (exit status) from the Java program cannot be accessed.

Furthermore, I get an annoying ERROR message from taskkill if the Java program to be killed has finished.










replace ping line with Code: [Select]ping localhost -w 500 -n 2 > nuli am not getting the errorlevel of the java program but find.exe
the whole code is

Code: [Select]start java program
set a=1
:A
if %a%==100 goto out
ping localhost -w 500 -n 2 > nul
tasklist|find "java program"
if errorlevel 1 exit
set /a a=%a%+1
goto a
:out
taskkill java programThe -w option does not have the desired effect.

I even tried with '-w 1' to see if the time limit was reached faster. Unfortunately not.

The Java programs are supposed to solve a puzzle. If the puzzle cannot be solved the program must call System.exit(1); otherwise, ii should exit normally (calling System.exit(0)). After execution the argument to System.exit must be be tested using ERRORLEVEL.

This seems to be a harder problem to solve in DOS than I first thought.explain exactly what you want the batch to do  and can you post your current code
OK. Below is my code that works fine without any time limit. I want the Solver program to exit if it has not solved the puzzle within 100 seconds.

echo off
if EXIST tmp del tmp
echo %1 %2
java -Xmx1G -Xss1G Solver %3/%1 %3/%2 > tmp
if %ERRORLEVEL% EQU 0 goto else
echo *** Wrong exit status
goto endif
:else
java Tester %3/%1 %3/%2 < tmp
if %ERRORLEVEL% NEQ 0 echo *** Incorrect solver output
:endif
if EXIST tmp del tmp
echo.

TYPE tasklist give me result
start pogram and with out closing it type tasklist give me resultmat123:

Image Name                   PID Session Name     Session#    Mem Usage
....
java.exe                       4352 Console                          0     322.056 K
....

Below you can see my (messy) implementation of your suggested solution that doesn't work:

echo off
if EXIST tmp del tmp
echo %1 %2
start /B java -Xmx1G -Xss1G Solver %3/%1 %3/%2 > tmp
set i=1
:Repeat
ping localhost -w 100 -n 2 > nul
tasklist|find "java.exe"> nul
if %ERRORLEVEL% NEQ 0 goto :else
set /a i=%i%+1
if %i% LSS 100 goto :Repeat

if %ERRORLEVEL% EQU 0 goto else
echo *** Wrong exit status
goto endif
:else
taskkill /f /im java.exe > nul
java Tester %3/%1 %3/%2 < tmp
if %ERRORLEVEL% NEQ 0 echo *** Incorrect solver output
:endif
if EXIST tmp del tmp
echo.
start start /B java -Xmx1G -Xss1G Solver %3/%1 %3/%2
set a=1
:A
if %a%==100 goto out
ping localhost -w 500 -n 2 > nul
tasklist|find "java.exe"
if errorlevel 1 goto sol
set /a a=%a%+1
goto a
:out
taskkill java.exe
echo not solved
pause
exit
:sol
echo solved
pause
exitThis solution proposal doesn't work either:

(1) The ping command cannot be used to achieve a specific delay. The -n option specifies the number of echo requests to send, and the -w option specifies the timeout in MILLISECONDS to wait for each reply. Thus, there is no guarantee that the time to execute the ping is n*w milliseconds. It can take shorter or longer time.

(2) The ERRORLEVEL associated with the Java program execution is not tested.

I had hoped that I could provide a DOS command prompt for the students to test their Solver programs on a suite of test puzzles. Right now, it seems that I have to ask them to test their programs under UNIX instead.

you asked for
Quote

I want the Solver program to exit if it has not solved the puzzle within 100 seconds.
my solution works
the ping can be replaced by sleep.exe (optional download)  Quote from: hels on July 22, 2010, 01:22:25 AM
(1) The ping command cannot be used to achieve a specific delay. The -n option specifies the number of echo requests to send, and the -w option specifies the timeout in milliseconds to wait for each reply. Thus, there is no guarantee that the time to execute the ping is n*w milliseconds. It can take shorter or longer time.

IF you ping a non-existent address then it will always take the maximum timeout. I'm pretty sure that it won't work with localhost.

Quote
(2) The ERRORLEVEL associated with the Java program execution is not tested.

he isn't using the errorlevel of java.exe. he's using the errorlevel of tasklist.


Discussion

No Comment Found