0

I have written some code that allows user to enter the code in a textbox and I run that code in a separate process and collect the output of the code, and return the response from a servlet to the browser.

Problem is

What if the user made a program of infinite loop....so process will keep on running and writing to browser stream happens once, how can i write to the browser's stream in the case of an infinite for loop.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42

1 Answers1

0

You have stumbled upon the halting problem. There is no way to tell if a section of code is in an infinite loop. The best you can do is impose a time limit and stop the code after that time.

In Java, you can do that by executing the code in its own thread and then killing the thread after the given time. You can see an example of that in this SO post: Killing thread after some specified time limit in Java

Community
  • 1
  • 1
Andrew Martinez
  • 3,714
  • 3
  • 35
  • 38
  • So you mean to say wherever i am collecting the output of mybprocess i should enclose it inside an executor thread and kill it after a specified time ? – cafebabe1991 Sep 26 '13 at 06:07
  • If you are executing arbitrary code you don't controll: yes. It is impossible to correctly detect whether a given set of instructions will ever finish running. On a very simple scale you can attempt it, but your efforts will be in futility as complex input would eventually foil your attempts. – Andrew Martinez Sep 26 '13 at 15:22