nginx lua 获取当前所有进程号

来源:互联网 发布:tcp编程用什么语言 编辑:程序博客网 时间:2024/06/05 04:10
lua local pids = ngx.worker.pids() --return table

ngx_http_lua_worker.c

voidngx_http_lua_inject_worker_api(lua_State *L){    lua_createtable(L, 0 /* narr */, 2 /* nrec */);    /* ngx.worker. */    lua_pushcfunction(L, ngx_http_lua_ngx_worker_exiting);    lua_setfield(L, -2, "exiting");    lua_pushcfunction(L, ngx_http_lua_ngx_worker_pids);    lua_setfield(L, -2, "pids");    lua_setfield(L, -2, "worker");}static intngx_http_lua_ngx_worker_num(){    int i, num = 0;    for(i = 0; i < NGX_MAX_PROCESSES ; i++){        if(ngx_processes[i].pid != -1 && ngx_processes[i].pid != 0 ){            num++;        }    }    return num;}static intngx_http_lua_ngx_worker_pids(lua_State *L){    lua_createtable(L, 0, ngx_http_lua_ngx_worker_num()+1);    /* ngx.worker.pids() */    int i, num = 1;    lua_pushinteger(L, (lua_Integer) num++);    lua_pushinteger(L, (lua_Integer) getppid());  //master pid    lua_settable(L, -3);    for(i = 0; i < NGX_MAX_PROCESSES; ++i){        if(ngx_processes[i].pid != -1 && ngx_processes[i].pid != 0){            lua_pushinteger(L, (lua_Integer)num++);            lua_pushinteger(L, (lua_Integer)ngx_processes[i].pid);            lua_settable(L, -3);        }    }    lua_pushinteger(L, (lua_Integer) num);    lua_pushinteger(L, (lua_Integer) ngx_pid);   //current pid    lua_settable(L, -3);    return 1;}
原创粉丝点击