How to wait for a process executed by proc_open() in php?
Posted: Sat Jan 25, 2014 9:23 pm
**What i am doing :->**
I am taking a source file (say .c) through php by running apache server on local host. I am saving that file in "/code/" directory and compiling and executing this source file using proc_open() in php. Now i want to wait main php script until the process created by "proc_open()" terminates, so i used "pcntl_waitpid()". But i think there is a problem in using "pcntl_waitpid()" because the script after the "pcntl_waitpid()" is not executing.
Process created by "pcntl_waitpid()" takes input from "/code/input.txt" file and gives output to "/code/output.txt" file, so for redirecting purpose i am using proc_open() , so that i can easily redirect streams.
**My machine configuration:-**
OS -> Ubuntu 12.04 LTs
PHP 5.3.10
APACHE 2.2.22
Running on local host
**Permissions :-**
/code -> 777
Folder where main php file resides -> 777 (I know that 777 is not good due to security reasons , but as i am running script on local server so i have no problem with these permissions.)
**What i want :-**
Can any one tell me any way so that i can stop main php script until the process created by proc-open() terminates or if anyone can tell me other way to meet my requirement of redirection ?
**Here is a part of the code :**
**THANK YOU!**
I am taking a source file (say .c) through php by running apache server on local host. I am saving that file in "/code/" directory and compiling and executing this source file using proc_open() in php. Now i want to wait main php script until the process created by "proc_open()" terminates, so i used "pcntl_waitpid()". But i think there is a problem in using "pcntl_waitpid()" because the script after the "pcntl_waitpid()" is not executing.
Process created by "pcntl_waitpid()" takes input from "/code/input.txt" file and gives output to "/code/output.txt" file, so for redirecting purpose i am using proc_open() , so that i can easily redirect streams.
**My machine configuration:-**
OS -> Ubuntu 12.04 LTs
PHP 5.3.10
APACHE 2.2.22
Running on local host
**Permissions :-**
/code -> 777
Folder where main php file resides -> 777 (I know that 777 is not good due to security reasons , but as i am running script on local server so i have no problem with these permissions.)
**What i want :-**
Can any one tell me any way so that i can stop main php script until the process created by proc-open() terminates or if anyone can tell me other way to meet my requirement of redirection ?
**Here is a part of the code :**
Code: Select all
$descriptors = array(
2 => array("pipe","w")
);
$process = proc_open($cmd,$descriptors,$pipes,$code);
sleep(2);
if(is_resource($process))
{
$status = proc_get_status($process);
$pid = $status ["pid"];
echo getmypid()."<br />";
echo $pid;
if(pcntl_waitpid($pid,$child_status) == -1)
{
fclose($pipes[2]);
proc_close($process);
exit("Sorry!Unable to compile the source_file.\n");
}
while(!file_exists("/code/test"))
{
proc_close($process);
echo stream_get_contents($pipes[2]),"\n";
fclose($pipes[2]);
exit("Sorry!Unable to compile the source_file.\n");
}
echo "Compiled Successfully!\n";
echo "Running test cases...\n";
fclose($pipes[2]);
proc_close($process);
}
else
{
exit("Sorry!Unable to compile the source_file.\n");
}