进程管理器
Process Manager
我正在尝试编写一个包装器周围的模拟器,它提供了一个web API,以便用户可以在服务器上产生模拟并收集结果。
我最初的设计是有一个php模拟StartSimulation.php在Apache中运行,它将分叉&执行模拟,并将结果传输到一个文件。然而,你不能分叉&在apache模块中执行。此外,由于没有管理孤立模拟的进程,因此在此实现中会变得有点混乱,因此很难杀死它们并对它们进行计数。使用标准的web服务器似乎有点困难,因为没有全局状态来存储正在运行的进程列表(模拟)。
模拟器是用c++编写的,但应该尽可能少地修改。我更喜欢为它编写一个包装器,并执行模拟器并捕获其标准输出,并使其可供用户访问。
基本上,包装器应该有一个带有3个命令的web访问API。
1.) start_simulation -它基本上分叉了&执行模拟器的一个实例,将其pid记录在一个表中,并将其标准输出管道输送到一个已知的缓冲区。包装器不应该允许在服务器上运行超过16个模拟器实例。如果成功,它将向用户返回模拟代码。否则,返回一个错误代码。
2.) stop simulation -获取(1)中返回的仿真代码,并终止相关进程。
3.) get_process -获取(1)中返回的模拟代码,查找保存该进程的标准输出的已知缓冲区,并将该信息返回给用户。
我应该为此编写一个自定义应用服务器吗?如果有,是否有一个带有一些入门代码的不错的包?
谢谢凯尔
我知道这是一个老问题,但希望这仍然能证明对某人有帮助…
在PHP脚本中通过http请求直接从Apache调用进程管理(fork/exec/wait/kill/signal等)绝对不是要走的路。就像你说的,它很快就会变得混乱:)
我建议通过http调用的PHP脚本只是一个简单的服务器进程的命令代理。如果PHP是您的首选语言,您可以通过这种方式实现这两种语言。
例如,您可以对消息队列执行如下操作…
-
您可以创建一个相当简单的PHP服务器,它创建一个消息队列并等待有消息进来。然后它可以执行启动或停止模拟器进程的工作
-
远程用户通过网页表单选择操作(启动、停止、获取输出)。
-
这导致HTTP/POST请求发送表单数据到您的PHP脚本(我这样做作为一个AJAX调用,所以我可以发送数据和解释结果,而无需重新加载页面)
-
您的服务器端PHP脚本可以解释表单数据并通过消息向PHP服务器进程发送命令
这是我们解释传入的表单请求并将其转换为服务器进程的消息的地方
<?php
/*
* Responses are sent as simple text strings to be interpreted by
* the client-side JavaScript handling this AJAX call. Responses
* starting with 'ERR:' are errors, responses starting with 'ACK:'
* are acknowledgements. Simply check the first few characters of
* the response in the client-side JavaScript.
*/
header('Content-Type: text/plain; charset=UTF-8);
/*
* Here we define some message types, one per command. These should correspond
* to the command string sent from the web form
*/
$command = array(
'START_SIM' => 1,
'STOP_SIM' => 2,
'GET_STATUS' => 3,
'GET_OUTOUT' => 4
);
$queue_id = 0xbeef; /* An arbitrary message queue id */
$cmd = $_REQUEST['command']; /* The command from the web form */
/* get simulator instance id to act upon, if specified */
if (isset($_REQUEST['id']))
$sim_id = $_REQUEST['id'];
else
$sim_id = ''; /* no sim id? probably a create command */
/* check the message queue exists... */
if (msg_queue_exists($queue_id) === false) {
echo 'ERR:Message queue not found!';
return;
}
/* open the message queue and pass the command on to the server... */
if (($qhandle = msg_get_queue($queue_id)) === false) {
echo 'ERR:Failed to open message queue to server';
return;
}
if (msg_send($qhandle, $command[$cmd], $sim_id) === false)
echo 'ERR:Failed to send command';
else
echo 'ACK:Command sent ok';
?>
PHP Server(在您的web服务器上单独运行)
这里有一个同样简单的服务器…
<?php
/*
* assume the same queue id's and defines as in the
* client code above, etc..
*/
if (($qhandle = msg_get_queue($queue_id)) === false) {
/* emit failure message to log file etc.. */
...
return;
}
while (1) {
if (msg_receive($qhandle, 0, $msgtype, $message,
true, 0, $rc) === false) {
/* log error message ... */
} else {
/*
* Get the client id (in case you want to send
* a reply back to the client) and the
* message data, which is the simulation id.
*
* Remember that we use the message type to
* indicate the command being requested
*/
$client = $message['client'];
$sim_id = $message['msg'];
evaluate_command($client, $msgtype, $sim_id);
}
}
?>
显然这非常简单,没有错误检查,您需要自己编写"evaluate_command()"函数。我只是为了说明这个想法而草草写了下来(我是即兴写的,所以它可能也充满了其他错误!)
- 如何维护资源管理器项目视图中当前可见的项目列表
- 使用"Task"函数指针队列定义作业管理器
- 实现动态插件管理器
- 编译器资源管理器和 GCC 具有不同的输出
- Windows 资源管理器中的图标在使用 resource.rc 时显示 2 个不同的图标
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- Linux 包管理器如何处理 C++20 个模块?
- 编译器资源管理器(godbolt)如何安全地运行代码?
- 创建跨平台 C++ 触摸管理器.在 c++ 中传递 Objective-c 对象涉及代码
- 无法删除EXE崩溃后,即使进程未显示在任务管理器中
- 如何以编程方式获取任务管理器进程CPU使用率(不是PerfMon API)
- 任务管理器如何获取进程的命令行可执行路径
- 使应用程序/服务,以便在任务管理器中尝试结束/终止其进程将导致"Unable to Terminate Process"
- 如何在后台进程上处理来自Windows任务管理器的"End Task"?
- 终止Qt进程:Windows任务管理器在做什么,我没有?
- 防止进程在任务管理器中关闭
- QT启动资源管理器进程参数
- 进程管理器
- 防止用户进程被进程资源管理器中的"End Process"杀死
- 如何将应用程序作为新创建的资源管理器进程的子进程启动