• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Terminating a infinite loop from within the program itself

 
V Bel
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a website which has java puzzles like "fill in the missing code".
I am compiling and executing the programs online and displaying the results with respect to some test cases.
My problem is when a user tries to implement a program which goes into a infinite loop.
eg,

I want to know how I can time out programs while they are executing so I can halt the execution of programs which exceed a certain timeout or my web server will become too slow if such infinite loops are allowed to be compiled.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run the user code in a new thread. After a predetermined time period if the thread is still running call stop the user thread.

Have a look at this for an example:

http://www.acme.com/resources/classes/Acme/TimeKiller.java
 
V Bel
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Worked for my case...
I am posting the code for further reference for other googlers. I modified your code for the infinite while loop.



Thanks a lot. This is really helpful.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am glad it worked for you.

I modified your code for the infinite while loop


It was not my code, I found it using google.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sure hope you're installing a strict SecurityManager, or sooner rather than later, one of the "puzzle solutions" is going to pwn your server.
 
V Bel
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am working on that too.
Any suggestions on that. ?
Any useful links ?
Thanks
 
Moojid Hamid
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one of the "puzzle solutions" is going to pwn your server


Oops I just tried System.exit(), I guess I will have wait until you restart the server to try running System.reformatHardDrive()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic