我想用php在远程计算机上运行一个C程序。最终目标是使用手机或任何其他计算机上的web浏览器控制程序。
我的C程序在几十分钟内从不同的传感器获取数据。它在linux中从命令行运行,我可以通过按计算机键盘上的“q”键来关闭它。主线程如下所示:
int main(){
printf("Program is running... Press 'q' <enter> to quit\n");
fflush(stdout);
//create one thread per sensor
while (getchar() != 'q'){
}
//ask the threads to terminate
return(0);
}
每个线程执行一些printf以给出每个传感器的状态。我想在我的手机上监测这些值,并有一个按钮来终止远程程序。
我可以使用system()、open()或proc_open()成功监视值。它的问题是主程序中的getchar。它挂起了php脚本。。。
<?php
if(isset($_POST['start'])){
$descriptorspec = array(
0 => array("pipe", "r"), // // stdin est un pipe où le processus va lire
1 => array("pipe", "w"), // stdout est un pipe où le processus va écrire
2 => array("file", "/tmp/error-output.txt", "a") // stderr est un fichier
);
$cwd = '/tmp';
$env = array();
$process = proc_open('/home/tristan/www/a.out', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
fwrite($pipes[0], 'q');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);
echo "La commande a retourné $return_value\n";
}
}
?>
使用fwrite($pipes[0],'q')
运行良好,但如果我使用fwrite($pipes[0],''),php脚本将挂起
为了保持程序运行。。。
编辑:我研究了缓冲问题
$process=proc_open('stdbuf-i0/home/tristan/www/a.out',$descriptorspec,$pipes,$cwd,$env)
没有成功。。。
有人知道如何以交互方式监视值和向程序发送命令吗?
谢谢你的帮助!
我终于在system()函数中使用管道得到了我想要的:
<?php
if(isset($_POST['start'])){
system('> /tmp/progOut');
system('mkfifo /tmp/progIn');
$proc = popen('./a.out < /tmp/progIn > /tmp/progOut &', 'r');
}
?>
上面的PHP代码有两个问题使您的程序挂起:
>
proc\u close()
也将永远等待,除非进程已终止。因为无休止的循环,这永远不会发生。您必须使用proc\u terminate()
显式终止它(这在我看来毫无意义)
我已经准备了一个代码不会挂起的例子,但是我需要说,你的代码没有多大意义。我将把C程序编码为一个监听UNIX域套接字的UNIX守护程序。然后,您可以为您的命令设计一个简单的基于文本的通信协议。然后在PHP中连接到套接字。但也许我错了,因为我不详细了解你的应用场景。
但是,下面是您的固定代码:
$descriptorspec = array(
0 => array("pipe", "r"), // // stdin est un pipe où le processus va lire
1 => array("pipe", "w"), // stdout est un pipe où le processus va écrire
2 => array("file", "/tmp/error-output.txt", "a") // stderr est un fichier
);
$cwd = '/tmp';
$env = array();
$process = proc_open(__DIR__ . '/a.out', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
fwrite($pipes[0], ' ');
fclose($pipes[0]);
// set stream to unblocking mode
stream_set_blocking($pipes[1], 0);
// now the following line will not wait forever
// for an EOF
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// proc_close will wait until the process has finished - forever in this case
// if you want to terminate the process. You'll have to terminate it explicitely
proc_terminate($process);
$return_value = proc_close($process);
// if you want to have a meaningful return value
// you'll have to implement a handler for SIGTERM
// in your C program
echo "La commande a retourné $return_value\n";
}
问题内容: 只是想知道,是否有可能单独使用PHP创建一个交互式shell。我的意思是您在数据库,python等方面拥有类似的东西。 如果是,怎么办? 问题答案: 是的,有可能。为了进行交互,该程序必须能够等待并从 stdin中 读取用户输入。在PHP中,您可以通过向打开文件描述符来从 stdin中 读取。从一个不同的问题的答案中获得,这是PHP中的交互式用户提示的示例(当然是从命令行运行时): 当
由于A-Frame能支持各种平台、设备和输入方法。与2D网页不同,虚拟现实的交互方式是无限的。对于电脑、平板和手机,我们只需要考虑鼠标和 触摸输入,对于Cardboard,我们只需要考虑单个按钮,而对于更为复杂的VR设备,我们可以做任何事情:抓、扔、擦、转、戳、伸、压等等动作,就像人在现实世界中一样。 更进一步,混合现实中的跟踪和自定义控制器甚至提供了超出现实的交互方式! 我们在本节中可以做的是过
我正在开发一个交互式排序应用程序。我必须将数字表示为矩形,并且例如,当排序算法运行时,当两个数字互换时,矩形必须互换。我想用动画来做这件事。我如何交换矩形?我目前正在使用transition测试这个功能,但我遇到了一些问题。我有两个矩形在一组。当我尝试交换矩形时,两者会在中间相遇并停止。代码如下: 我需要一个类似于帆布的窗格。我需要能够设置矩形坐标。
我注册了一个SlashCommand。。。但它的回答是“交互应用程序的命令无效”,有人能帮忙吗。。。
交换机与控制器交互 我们可以启动一个简单的控制器,默认没有任何流表项,仅仅作为一台带学习功能的交换机。控制器默认监听端口是 6633。 以下控制器与交换机之间的消息交互过程,可以通过 wireshark,配置 of 过滤器观察到交换机跟控制器之间的交互消息。 参见下面的表格。 消息 类型 描述 Hello Controller->Switch 跟着 TCP 握手,控制器发送它的版本号到交换机。 H
首先下面代码是有问题,大佬看一下代码,这个输入这里应该有问题,或者这个库不支持这样的持续交互吗?