Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to set a time limit for program execution  (Read 21617 times)

0 Members and 2 Guests are viewing this topic.

hels

    Topic Starter


    Rookie

    How to set a time limit for program execution
    « on: July 21, 2010, 02:39:15 AM »
    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?



    mat123



      Hopeful

      Thanked: 16
      • Yes
      • Yes
      • Yes
    • Experience: Familiar
    • OS: Windows XP
    Re: How to set a time limit for program execution
    « Reply #1 on: July 21, 2010, 10:00:47 AM »
    start java program
    ping localhost -n 101 > nul
    taskkill java program
     



    hels

      Topic Starter


      Rookie

      Re: How to set a time limit for program execution
      « Reply #2 on: July 21, 2010, 11:49:44 AM »
      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.

      mat123



        Hopeful

        Thanked: 16
        • Yes
        • Yes
        • Yes
      • Experience: Familiar
      • OS: Windows XP
      Re: How to set a time limit for program execution
      « Reply #3 on: July 21, 2010, 12:04:43 PM »
      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.exe



      hels

        Topic Starter


        Rookie

        Re: How to set a time limit for program execution
        « Reply #4 on: July 21, 2010, 02:34:06 PM »
        Thanks 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.











        mat123



          Hopeful

          Thanked: 16
          • Yes
          • Yes
          • Yes
        • Experience: Familiar
        • OS: Windows XP
        Re: How to set a time limit for program execution
        « Reply #5 on: July 21, 2010, 02:47:26 PM »
        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 program



        hels

          Topic Starter


          Rookie

          Re: How to set a time limit for program execution
          « Reply #6 on: July 21, 2010, 03:31:16 PM »
          The -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.

          mat123



            Hopeful

            Thanked: 16
            • Yes
            • Yes
            • Yes
          • Experience: Familiar
          • OS: Windows XP
          Re: How to set a time limit for program execution
          « Reply #7 on: July 21, 2010, 03:47:40 PM »
          explain exactly what you want the batch to do  and can you post your current code



          hels

            Topic Starter


            Rookie

            Re: How to set a time limit for program execution
            « Reply #8 on: July 21, 2010, 04:05:32 PM »
            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.


            mat123



              Hopeful

              Thanked: 16
              • Yes
              • Yes
              • Yes
            • Experience: Familiar
            • OS: Windows XP
            Re: How to set a time limit for program execution
            « Reply #9 on: July 21, 2010, 05:13:57 PM »
            type tasklist give me result
            start pogram and with out closing it type tasklist give me result



            hels

              Topic Starter


              Rookie

              Re: How to set a time limit for program execution
              « Reply #10 on: July 21, 2010, 06:06:33 PM »
              mat123:

              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.

              mat123



                Hopeful

                Thanked: 16
                • Yes
                • Yes
                • Yes
              • Experience: Familiar
              • OS: Windows XP
              Re: How to set a time limit for program execution
              « Reply #11 on: July 21, 2010, 06:27:27 PM »
              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
              exit



              hels

                Topic Starter


                Rookie

                Re: How to set a time limit for program execution
                « Reply #12 on: July 22, 2010, 01:22:25 AM »
                This 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.


                mat123



                  Hopeful

                  Thanked: 16
                  • Yes
                  • Yes
                  • Yes
                • Experience: Familiar
                • OS: Windows XP
                Re: How to set a time limit for program execution
                « Reply #13 on: July 22, 2010, 10:21:13 AM »
                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) 



                BC_Programmer


                  Mastermind
                • Typing is no substitute for thinking.
                • Thanked: 1140
                  • Yes
                  • Yes
                  • BC-Programming.com
                • Certifications: List
                • Computer: Specs
                • Experience: Beginner
                • OS: Windows 11
                Re: How to set a time limit for program execution
                « Reply #14 on: July 22, 2010, 11:07:02 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.
                I was trying to dereference Null Pointers before it was cool.