What is the issue with PHP waiting for the return of a C program started with shell_exec?
When PHP uses `shell_exec` to start a C program, it waits for the program to finish before continuing execution. If the C program does not return a result immediately, PHP will hang indefinitely. To solve this issue, you can use `proc_open` instead of `shell_exec` and set the `bypass_shell` option to true. This will allow the C program to run independently of PHP and prevent the script from hanging.
$descriptorspec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w']
];
$process = proc_open('/path/to/your/c_program', $descriptorspec, $pipes, null, null, ['bypass_shell' => true]);
if (is_resource($process)) {
fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
// Use $result as needed
}
Keywords
Related Questions
- What are the best practices for handling authentication and authorization in PHP to ensure secure access to restricted pages?
- What are the best practices for handling page reloading and frame-like behavior in PHP scripts?
- What are the best practices for handling multiple radio buttons with dynamic names in PHP forms?