1.

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

Answer» <html><body><p>I want to set a time limit of 100 seconds for executing a Java program. <br/><br/>In a UNIX I just write<br/><br/>     ulimit -t 100<br/>     java Program<br/><br/>How can I achieve the same with DOS?<br/><br/><br/>start java program<br/>ping localhost -n 101 &gt; nul<br/>taskkill java program<br/> Thanks mat123<br/><br/>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. <br/><br/>I need a solution for running a bunch of student programs and want to be sure that no more than 100 seconds are <a href="https://interviewquestions.tuteehub.com/tag/used-763273" style="font-weight:bold;" target="_blank" title="Click to know more about USED">USED</a> per run.start java.exe<br/>set a=1<br/>:A<br/>if %a%==100 goto out<br/>ping localhost -n 2 &gt; nul<br/>tasklist|find "java.exe"<br/>if errorlevel 1 exit<br/>set /a a=%a%+1<br/>goto a<br/>:out<br/>taskkill java.exeThanks again mat123. I implemented your solution. However, it seems to have some drawbacks:<br/><br/>(1) The -n option for the ping command does not specify seconds, but number of <a href="https://interviewquestions.tuteehub.com/tag/pings-2929260" style="font-weight:bold;" target="_blank" title="Click to know more about PINGS">PINGS</a>.<br/>(2) The value of  ERRORLEVEL (exit status) from the Java program cannot be accessed.<br/><br/>Furthermore, I get an annoying <a href="https://interviewquestions.tuteehub.com/tag/error-25548" style="font-weight:bold;" target="_blank" title="Click to know more about ERROR">ERROR</a> message from taskkill if the Java program to be killed has finished. <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>replace ping line with Code: <a>[Select]</a>ping localhost -w 500 -n 2 &gt; nuli am not getting the errorlevel of the java program but find.exe<br/>the whole code is<br/><br/> Code: <a>[Select]</a>start java program<br/>set a=1<br/>:A<br/>if %a%==100 goto out<br/>ping localhost -w 500 -n 2 &gt; nul<br/>tasklist|find "java program"<br/>if errorlevel 1 exit<br/>set /a a=%a%+1<br/>goto a<br/>:out<br/>taskkill java programThe -w option does not have the desired effect. <br/><br/>I even tried with '-w 1' to see if the time limit was reached faster. Unfortunately not.<br/><br/>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.<br/><br/>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<br/>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. <br/><br/>echo off<br/>if EXIST tmp del tmp<br/>echo %1 %2<br/>java -Xmx1G -Xss1G Solver %3/%1 %3/%2 &gt; tmp<br/>if %ERRORLEVEL% EQU 0 goto else<br/>echo *** Wrong exit status<br/>goto endif<br/>:else<br/>java Tester %3/%1 %3/%2 &lt; tmp<br/>if %ERRORLEVEL% NEQ 0 echo *** Incorrect solver output<br/>:endif<br/>if EXIST tmp del tmp<br/>echo.<br/><br/><a href="https://interviewquestions.tuteehub.com/tag/type-238192" style="font-weight:bold;" target="_blank" title="Click to know more about TYPE">TYPE</a> tasklist give me result<br/>start pogram and with out closing it type tasklist give me resultmat123:<br/><br/>Image Name                   PID Session Name     Session#    Mem Usage<br/>....<br/>java.exe                       4352 Console                          0     322.056 K<br/>....<br/><br/>Below you can see my (messy) implementation of your suggested solution that doesn't work:<br/><br/>echo off<br/>if EXIST tmp del tmp<br/>echo %1 %2<br/><strong>start /B java -Xmx1G -Xss1G Solver %3/%1 %3/%2 &gt; tmp<br/>set i=1<br/>:Repeat<br/>ping localhost -w 100 -n 2 &gt; nul<br/>tasklist|find "java.exe"&gt; nul<br/>if %ERRORLEVEL% NEQ 0 goto :else<br/>set /a i=%i%+1<br/>if %i% LSS 100 goto :Repeat</strong><br/>if %ERRORLEVEL% EQU 0 goto else<br/>echo *** Wrong exit status<br/>goto endif<br/>:else<br/><strong>taskkill /f /im java.exe &gt; nul</strong><br/>java Tester %3/%1 %3/%2 &lt; tmp<br/>if %ERRORLEVEL% NEQ 0 echo *** Incorrect solver output<br/>:endif<br/>if EXIST tmp del tmp<br/>echo.<br/>start start /B java -Xmx1G -Xss1G Solver %3/%1 %3/%2<br/>set a=1<br/>:A<br/>if %a%==100 goto out<br/>ping localhost -w 500 -n 2 &gt; nul<br/>tasklist|find "java.exe"<br/>if errorlevel 1 goto sol<br/>set /a a=%a%+1<br/>goto a<br/>:out<br/>taskkill java.exe<br/>echo not solved<br/>pause<br/>exit<br/>:sol<br/>echo solved<br/>pause<br/>exitThis solution proposal doesn't work either:<br/><br/>(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 <a href="https://interviewquestions.tuteehub.com/tag/milliseconds-1096920" style="font-weight:bold;" target="_blank" title="Click to know more about MILLISECONDS">MILLISECONDS</a> 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.<br/><br/>(2) The ERRORLEVEL associated with the Java program execution is not tested.<br/><br/>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. <br/><br/>you asked for<br/> Quote</p><blockquote>I want the Solver program to exit if it has not solved the puzzle within 100 seconds.<br/></blockquote> my solution works<br/>the ping can be replaced by sleep.exe (optional download)  Quote from: hels on July 22, 2010, 01:22:25 AM<blockquote>(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.<br/></blockquote> <br/>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.<br/><br/> Quote<blockquote>(2) The ERRORLEVEL associated with the Java program execution is not tested.<br/></blockquote> <br/>he isn't using the errorlevel of java.exe. he's using the errorlevel of tasklist.</body></html>


Discussion

No Comment Found