0

I am trying to execute a .exe file using exec() function on my webpage. It sometimes is entering infinite loops and the .exe file is never terminated preventing my php page from proceeding further.

exec('CodeFile.exe 1>temp\\OutputFile.txt<temp\\InputFile.txt');
unlink('CodeFile.exe');

Can any one help me out in ending the process when it enters infinite loop?

And i am using XAMPP running on Windows 10

Maheshwar Reddy K
  • 1,111
  • 2
  • 7
  • 9

2 Answers2

0

Since CodeFile.exe is a program under your control, I would focus on fixing that first. With rare exceptions, infinite loops do not represent intended or desired behavior. Looking for a timeout option is the equivalent of "sweeping the problem under the rug."

Clearly, CodeFile.exe is an important step in the process, otherwise you wouldn't have written it and made it part of your architecture. Your intention is that it complete before moving on with any subsequent processing. So if it occasionally runs off and never finishes, how can you continue from a broken and unknown state?

Charlie Hills
  • 636
  • 6
  • 13
  • No, actually CodeFile.exe is an executable code typed by user on my website. Sometimes they are ending up with loops. @Charlie Hills – Maheshwar Reddy K Jun 29 '16 at 05:21
  • I'm confused. Earlier you said, "Yes CodeFile.exe is C program compiled by me." What do you mean by "executable code typed by user"? – Charlie Hills Jun 29 '16 at 14:55
  • I want to conduct coding contests through my website. So that's where user might enter any type of code – Maheshwar Reddy K Jun 29 '16 at 15:29
  • I hope I'm not reading that right: any person anywhere can upload any executable to your website and you'll just run it? That's exceedingly dangerous. If it's a coding contest, have them submit the source code along with InputFile.txt and OutputFile.txt. Don't blindly execute arbitrary user-supplied code. – Charlie Hills Jun 29 '16 at 16:59
  • I want to make something like them "codechef" "hackerrank" "hackerearth" – Maheshwar Reddy K Jun 29 '16 at 17:02
0
    exec('CodeFile.exe 1>temp\\OutputFile.txt<temp\\InputFile.txt & timeout /t 10 & taskkill /im CodeFile.exe /f');
    unlink('CodeFile.exe');

This worked for me.:) Thank you @Charlie Hills and @FirstOne for your suggestions.

Maheshwar Reddy K
  • 1,111
  • 2
  • 7
  • 9