Libuv库(探讨)---第六节:进程相关

系列目录:

libuv概设:https://blog.csdn.net/knowledgebao/article/details/82251307

libuv异步调度:https://blog.csdn.net/knowledgebao/article/details/82251513

libuv文件系统:https://blog.csdn.net/knowledgebao/article/details/82252619

libuv网络相关:https://blog.csdn.net/knowledgebao/article/details/82252653

libuv线程相关:https://blog.csdn.net/knowledgebao/article/details/82252664

libuv进程调度:https://blog.csdn.net/knowledgebao/article/details/82252683

libuv高级调度:https://blog.csdn.net/knowledgebao/article/details/82252698

libuv其它:https://blog.csdn.net/knowledgebao/article/details/82252716

官网简介:http://docs.libuv.org/en/v1.x/guide/processes.html

官网API:http://docs.libuv.org/en/v1.x/process.html

libuv offers considerable child process management, abstracting the platform differences and allowing communication with the child process using streams or named pipes.(libuv提供子进程的管理工作,抽象平台的差异性,允许通过stream和pipe进行线程之间的交互)

A common idiom in Unix is for every process to do one thing and do it well. In such a case, a process often uses multiple child processes to achieve tasks (similar to using pipes in shells). A multi-process model with messages may also be easier to reason about compared to one with threads and shared memory.(unix建议每个进程只做一件事并且做好。所以一个进程常常使用多个子进程来完成各种任务,类似于shell中使用多个pipe。与线程共享内存类似,含有消息的多进程模型也很简单。)

A common refrain against event-based programs is that they cannot take advantage of multiple cores in modern computers. In a multi-threaded program the kernel can perform scheduling and assign different threads to different cores, improving performance. But an event loop has only one thread. The workaround can be to launch multiple processes instead, with each process running an event loop, and each process getting assigned to a separate CPU core.(基于事件的程序想利用现代计算机的多核功能是有一定挑战性的。在多线程程序里,内核可以通过调度把不同的线程分配到不同的系统核core上来提供core的利用率。但是libuv的loop只有一个线程,通过变通的使用多进程,每个进程上运行一个loop,每个进程关联到不同的core上以达到利用多多核系统。)

创建线程:

The simplest case is when you simply want to launch a process and know when it exits. This is achieved using uv_spawn.

使用uv_spawn来创建一个进程。代码如下:

uv_loop_t *loop;
uv_process_t child_req;
uv_process_options_t options;
void on_exit(uv_process_t *req, int64_t exit_status, int term_signal) {
    fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
    uv_close((uv_handle_t*) req, NULL);
}
int main() {
    loop = uv_default_loop();

    char* args[3];
    args[0] = "mkdir";
    args[1] = "test-dir";
    args[2] = NULL;

    options.exit_cb = on_exit;
    options.file = "mkdir";
    options.args = args;

    int r;
    if ((r = uv_spawn(loop, &child_req, &options))) {
        fprintf(stderr, "%s\n", uv_strerror(r));
        return 1;
    } else {
        fprintf(stderr, "Launched process with ID %d\n", child_req.pid);
    }

    return uv_run(loop, UV_RUN_DEFAULT);
}

options必须被初始化位0,上边代码因为options是全局的,所以默认会被置为0.

args的数组最后一个参数必须是NULL。

完成调用之后,uv_process_t.pid包含的就是进程ID.

on_exit()在进程退出时,会触发。

改变参数:

在进程运行前,可以改变参数uv_process_options_t。

Set uv_process_options_t.cwd to the corresponding directory.设置运行目录。

uv_process_options_t.env is a null-terminated array of strings, each of the form VAR=VALUE used to set up the environment variables for the process. Set this to NULL to inherit the environment from the parent (this) process.运行环境。

按位或flags参数。

Setting uv_process_options_t.flags to a bitwise OR of the following flags, modifies the child process behaviour:

  • UV_PROCESS_SETUID - sets the child’s execution user ID to uv_process_options_t.uid.
  • UV_PROCESS_SETGID - sets the child’s execution group ID to uv_process_options_t.gid.

Changing the UID/GID is only supported on Unix, uv_spawn will fail on Windows with UV_ENOTSUP.

  • UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS - No quoting or escaping of uv_process_options_t.args is done on Windows. Ignored on Unix.
  • UV_PROCESS_DETACHED - Starts the child process in a new session, which will keep running after the parent process exits. See example below.

猜你喜欢

转载自blog.csdn.net/knowledgebao/article/details/82252683