I have recently made a stuff, the approximate architecture is: visitor submits the C program via the Web, the server calls the compiler and runs the compiler after compiling and runs back the visitor browser. And do not consider safety, because visitors can think that they can trust, the command line compiler is ultimately returned, but for the temporary compiler, although the user can trust, it does not rule out the deadlock because of imperfect The process that is started after the PHP call cannot return, and this process has always been until the server is restarted. Over time, server-side resources are depleted.
Considering that the PHP itself does not provide multi-threaded and process management function (maybe I have not seen this information), use whether it is exec, or POPEN, etc., the main program cannot be self-extricted, so it must be reserved The thread manages the startup process when necessary, and I don't want to make changes to the server. So I thought I wrote a process of writing a program management startup, and PHP indirectly invokes the compiled customer program through this program to implement the customer's timeout control. Below is a PHP program for testing. // filename: Test1.php $ cmd = "Test.exe 24 154"; // Input You Command Here
$ cmd = "process.exe 5000". $ cmd; $ descriptorspec = array (0 => Array ("Pipe", "R"), // stdin is a pipe what the child will read from 1 => Array (" PIPE "," W "), // stdout is a pipe what the child will write to 2 => Array (" File "," Error-Output.txt "," W "), // stderr is a file to write write writ To); $ process = proc_open ($ PIPESPESPEC, $ PIPES); if (is_resource ($ process)) {// $ pipes now looks like this: // 0 => Writeable Handle Connected to child stdin // 1 => readable handle connection to child stdout // Any error output will be appended to /tmp/error-output.txt
FWRITE ($ PIPES [0], '12345678'); // INPUT INTEGER TO Scanf, You Should Add '/ N' At the end of string as 'enter';
Fclose ($ PIPES [0]);
While (! Feof ($ pipes [1])) {Echo NL2BR (FGETS ($ PIPES [1], 1024));} fclose ($ pipes [1]); // it is important that you close Any Pipes Before Calling // proc_close in Order to Avoid A Deadlock // Proc_Terminate ($ process); $ return_value = proc_close ($ process);
Echo "COMMAND RETURNED $ RETURN_VALUE / N";}?> Process.exe is a proxy that I have written to PHP. How to use: process.exe max_time_limit client_exe_name [param1_to_client, param2_to_client, ...] max_time_limite is in milliseconds.
Under normal circumstances, we use proc_open () is such $ cmd = "test.exe 24 154";
Indirect calls through Process.exe becomes: Process.exe 1000 Test.exe 24 154 When the calling customer is timeout, Process will kill the process and return.
The following is the source program of Process, and DEV-C is compiled.
//filename:process.cpp#include ZeromeMory (& PI, SIZEOF (PI)); // Start The Child Process. If (! CreateProcess (Null, // No Module (Use Command Line). Gargv, // Command Line. Null, // Process Handle Not Inheritable NULL, // Thread Handle NOT INHERITABLE. TRUE, // set handle inheritance to false. The key is this parameter, which uses the parent console window for True. 0, // no create flags. Null, // use parent's environment BLOCK NULL, // USE Parent's Starting Directory. & Si, // Pointer to Startupinfo Structure. & Pi) // Pointer to Process_information Structure.) {Return 1;} // Wait Until Child Process EXITS. WAITFORSINGLEOBJECT (PI.HPROCESS, Infinite); // Close Process and thread handles. End_thread = false; closehandle (pi.hprocess); closehandle (pi.hthread);} Test used client program Test.cpp: #include