我正在尝试创建一个人们可以在线编译和运行代码的网站,因此我们需要找到一种用户发送指令的交互方式.
实际上,首先想到的是exec()或者system(),但是当用户想要输入时,这种方式将不起作用.所以我们必须使用proc_open().
例如,以下代码
int main()
{
int a;
printf("please input a integer\n");
scanf("%d", &a);
printf("Hello World %d!\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用时proc_open(),就像这样
$descriptorspec = array(
0 => array( 'pipe' , 'r' ) ,
1 => array( 'pipe' , 'w' ) ,
2 => array( 'file' , 'errors' , 'w' )
);
$run_string = "cd ".$addr_base."; ./a.out 2>&1";
$process = proc_open($run_string, $descriptorspec, $pipes);
if (is_resource($process)) {
//echo fgets($pipes[1])."<br/>";
fwrite($pipes[0], '12');
fclose($pipes[0]);
while (!feof($pipes[1])) …Run Code Online (Sandbox Code Playgroud)