进程管理的静态工具类
More...
#include <sttnet.h>
|
| template<class... Args> |
| static bool | startProcess (const std::string &name, const int &sec=-1, Args ...args) |
| | 启动一个新进程(可选择是否定时重启)
|
| |
| template<class Fn, class... Args> |
| static std::enable_if<!std::is_convertible< Fn, std::string >::value, bool >::type | startProcess (Fn &&fn, const int &sec=-1, Args &&...args) |
| | 通过函数创建子进程(可选择是否定时重启)
|
| |
◆ startProcess() [1/2]
template<class... Args>
| static bool stt::system::Process::startProcess |
( |
const std::string & | name, |
|
|
const int & | sec = -1, |
|
|
Args ... | args ) |
|
inlinestatic |
启动一个新进程(可选择是否定时重启)
当 sec == -1 时,仅启动一次子进程;否则,会创建一个辅助子进程,定期重启该目标进程。
- 不会修改调用方(父进程)的信号处置。
- 定时调度进程退出时,Linux 会向其当前目标子进程发送 SIGTERM,避免孤儿服务。
- Template Parameters
-
| Args | 可变参数类型(用于传递给目标程序的 argv) |
- Parameters
-
| name | 要执行的程序路径(如 /usr/bin/myapp) |
| sec | 定时间隔(单位:秒)。若为 -1,则表示只启动一次,不定时。 |
| args | 启动参数(第一个参数必须是程序名称,即 argv[0],末尾不需要添加 nullptr) |
- Returns
- true 父进程返回 true 表示启动(或调度)成功
-
false fork 或 execv 出错时返回 false
- Note
- 参数中不需要手动添加 nullptr;内部自动添加。
- 执行失败后不会抛出异常;返回 false。
- execv 会在子进程中替换当前进程镜像,不返回。
◆ startProcess() [2/2]
template<class Fn, class... Args>
| static std::enable_if<!std::is_convertible< Fn, std::string >::value, bool >::type stt::system::Process::startProcess |
( |
Fn && | fn, |
|
|
const int & | sec = -1, |
|
|
Args &&... | args ) |
|
inlinestatic |
通过函数创建子进程(可选择是否定时重启)
使用一个可调用对象(如 Lambda、函数指针、std::function)作为新子进程的主体逻辑。
- 当
sec == -1,函数仅执行一次;
- 否则,会创建一个辅助进程定期重新 fork 并执行该函数。
- Template Parameters
-
| Fn | 可调用对象类型(如函数、Lambda) |
| Args | 可调用对象的参数类型 |
- Parameters
-
| fn | 要执行的函数或可调用对象 |
| sec | 定时间隔(单位:秒)。若为 -1,则表示只执行一次,不定时。 |
| args | 要传递给函数的参数 |
- Returns
- true 父进程返回 true 表示启动成功;子进程内部也会返回 true(用于链式调用等)
-
false 创建进程失败
- Note
- 函数的实际执行是在新 fork 出的子进程中。
- 函数 fn 必须是可调用的;参数类型需能完美转发。
- 子进程执行完毕后会退出;辅助进程将周期性地重新创建它。
The documentation for this class was generated from the following file: