External program Timeout blocking problem under Windows to resolve PHP calls

Source: Internet
Author: User

I recently made a Dongdong, the general framework is:

The visitor submits the C program through the Web, the server invokes the compiler compile and runs the compiled program after the compilation completes, and returns the results of the run back to the visitor's browser.

And regardless of security, because the visitor can be believed to be trustworthy, the command-line compiler will eventually return, but for a program that is temporarily compiled, although the user is trustworthy, but does not rule out deadlocks due to imperfections, the process that started after the PHP call fails to return and times out, and the process persists until the server restarts , as time passes, server-side resources will be depleted.

Considering that PHP itself does not provide multithreading and process management functions (perhaps I do not see this information), the use of either exec, or Popen, the main program once blocked can not extricate themselves, Therefore, a thread must be reserved to manage the initiated process if necessary. And I don't want to make changes to the server configuration. So think of yourself to write a program management startup process, PHP indirectly through this program to invoke the compiled client program, to achieve the client program timeout control.

Here is the test PHP program.

?
filename:test1.php
$cmd = "Test.exe 154";//input your command here

$cmd = "Process.exe 5000". $cmd;
$descriptorspec = Array (
0 => Array ("Pipe", "R"),//stdin is a pipe this child would read from
1 => Array ("Pipe", "w"),//stdout is a pipe which the child would write to
2 => Array ("File", "Error-output.txt", "w+"),//stderr is a file to write to
);
$process = Proc_open ($cmd, $descriptorspec, $pipes);
if (Is_resource ($process)) {
$pipes now looks like this:
0 => writeable handle connected to child stdin
1 => readable handle connected to child stdout
Any error output'll be appended to/tmp/error-output.txt

Fwrite ($pipes [0], ' 12345678 ');/input integer to scanf, and you should add ' \ n ' at the end of string as ' Ente R ';

Fclose ($pipes [0]);

while (!feof ($pipes [1])) {
Echo nl2br (Fgets ($pipes [1], 1024));
}
Fclose ($pipes [1]);
It is important so close any pipes before calling
Proc_close in order to avoid a deadlock
Proc_terminate ($process);
$return _value = Proc_close ($process);

echo "<br>command returned $return _value\n";
}
?>

Process.exe is the agent program that I write to PHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.