libuv::进程

摘要:
对于基于事件的程序,有一个限制。您不能充分利用多个内核来提高CPU利用率。即使您可以使用多线程编程来分配句柄,但每个循环仍然只有一个线程。此时,使用多个进程可以分担循环的压力,多进程+通信的方法比多线程+共享内存的方法更安全,更容易开发。#include<csdio>#include<studio。h˃ #include #我
对于基于事件(event-based)的程序来说, 有个限制,没办法很好地利用多核,提高CPU使用率.
即使能够使用多线程编程来分发 handle, 但是每个 loop 还是只有一个线程.
这时候, 使用多进程就能够分担 loop 的压力,并且通过多进程
+ 通信的方法, 会比 多线程 + 共享内存的方法更加安全, 易于开发.
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <libuv/uv.h>

void on_exit(uv_process_t* req, int64_t exit_status, int term_signal) {
    printf("exit_status = %lld , signal = %d
", exit_status, term_signal);
    uv_close((uv_handle_t*)req, NULL);
}

int main() {
    uv_loop_t* loop = uv_default_loop();

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

    uv_process_options_t options;
    options.exit_cb = on_exit;
    options.file = "mkdir";
    //子进程就与父进程脱离了关系.
    options.flags = UV_PROCESS_DETACHED;
    options.args = args;


    //创建子进程,子线程创建一个文件夹
    int r;
    uv_process_t child_req;
    if ((r = uv_spawn(loop, &child_req, &options))) {
        printf("%s
", uv_strerror(r));
        return 1;
    }
    else {
        printf(" child process ID =  %d
", child_req.pid);
    }
    return uv_run(loop, UV_RUN_DEFAULT);
}

免责声明:文章转载自《libuv::进程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇借助取色工具ColorPix对Pycharm编辑器设定自己喜欢的代码颜色_20161202MATLAB元胞数组下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[转]天龙八部服务器端Lua脚本系统

一、Lua脚本功能接口 1. LuaInterface.h/.cpp声明和实现LuaInterface。 LuaInterface成员如下: //脚本引擎 FoxLuaScriptmLua ; //注册器 LuaCFuncRegistermFuncRegister; //场景关联 Scene*mOwner; //已经读取的脚本表 IDTablem_Sc...

threejs纹理

纹理 纹理用来表现物体的细节。理论上可以将物体的每个细节建模出来,但是这样时间成本和性能成本都太高,因此,将物体的一些细节用纹理来表示。 图片纹理 图片纹理直接在物体表面应用图片。可以使用TextureLoader类的load方法来加载纹理。 function loadImgTexture(){ var loader = new THREE.Te...

《Linux内核Makefile分析》之 auto.conf, auto.conf.cmd, autoconf.h【转】

转自:http://blog.sina.com.cn/s/blog_87c063060101l25y.html 转载:http://blog.csdn.net/lcw_202/article/details/6661364 在编译构建性目标时(如 make vmlinux),顶层 Makefile 的 $(dot-config) 变量值为 1 。 在顶层...

uboot学习之三-----uboot启动第一阶段--start.S之一

uboot分为两个阶段:start.S是uboot的第一阶段。   一:引入start.S     u-boot.s找到start.S的入口       ①首先在C语言中整个项目的入口就是main函数(这是C语言规定的),所以如果要去了解C语言的项目,从main函数开始,这样才能分析,如果随便拿一个文件就开始看,最后看得一头雾水,对自己没有信心。怎么来找呢...

opencv输出图片像素值

需求:在控制台输出灰度图像的像素值 代码: #include <stdio.h>#include <iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgpr...

【转】一个URL编码和解码的C++类

下面的代码实现了一个用于C++中转码的类strCoding。里面有UTF8、UNICODE、GB2312编码的互相转换。 .H文件: #pragma once #include <iostream> #include <string> #include <windows.h> using namespace std;...