1
0
ithewei 6 жил өмнө
parent
commit
11ee1aeb6e
8 өөрчлөгдсөн 307 нэмэгдсэн , 285 устгасан
  1. 6 3
      README.md
  2. 1 1
      etc/test.conf
  3. 4 0
      h.h
  4. 185 0
      hmain.cpp
  5. 4 0
      hmain.h
  6. 1 0
      hplatform.h
  7. 55 0
      hproc.h
  8. 51 281
      main.cpp.tmpl

+ 6 - 3
README.md

@@ -30,18 +30,21 @@ hw 是一套跨平台c++工具集,类名以H开头
 - hobj.h: 对象基类
 - hvar.h: var变量
 - hbuf.h: 缓存类
-- iniparser.h: ini解析
 - hscope.h: 作用域RAII机制
+- hmain.h: main_ctx: arg env
+- hproc.h: 子进程/线程类
+- hsysinfo.h: 系统信息
 - singleton.h: 单例模式
+- iniparser.h: ini解析
 
 ## other
 
 - Makefile: 通用Makefile模板
-- hmain.cpp.tmpl: 通用main.cpp模板
+- main.cpp.tmpl: 通用main.cpp模板
 
 ## BUILD
 
 ```
-mv hmain.cpp.tmpl hmain.cpp
+mv main.cpp.tmpl main.cpp
 make DIRS="."
 ```

+ 1 - 1
etc/test.conf

@@ -1,7 +1,7 @@
 # [root]
 
 # loglevel = {"DEBUG", "INFO", "WARN", "ERROR"}
-loglevel = INFO
+loglevel = DEBUG
 
 # if not set worker_processes, default = ncpu
 worker_processes = 4

+ 4 - 0
h.h

@@ -29,6 +29,10 @@
 
 #include "hgui.h"
 
+#include "hmain.h"
+#include "hproc.h"
+#include "hsysinfo.h"
+
 #include "singleton.h"
 #include "iniparser.h"
 

+ 185 - 0
hmain.cpp

@@ -0,0 +1,185 @@
+#include "hmain.h"
+
+#include <signal.h> // for kill
+
+#include "hplatform.h"
+#include "hlog.h"
+
+main_ctx_t g_main_ctx;
+
+int main_ctx_init(int argc, char** argv) {
+    g_main_ctx.pid = getpid();
+    char* cwd = getcwd(g_main_ctx.run_path, sizeof(g_main_ctx.run_path));
+    if (cwd == NULL) {
+        printf("getcwd error\n");
+    }
+    //printf("run_path=%s\n", g_main_ctx.run_path);
+    const char* b = argv[0];
+    const char* e = b;
+    while (*e) ++e;
+    --e;
+    while (e >= b) {
+        if (*e == '/' || *e == '\\') {
+            break;
+        }
+        --e;
+    }
+    strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
+#ifdef _WIN32
+    if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
+        *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
+    }
+#endif
+    //printf("program_name=%s\n", g_main_ctx.program_name);
+
+    // save arg
+    int i = 0;
+    g_main_ctx.os_argv = argv;
+    g_main_ctx.argc = 0;
+    g_main_ctx.arg_len = 0;
+    for (i = 0; argv[i]; ++i) {
+        g_main_ctx.arg_len += strlen(argv[i]) + 1;
+    }
+    g_main_ctx.argc = i;
+    char* argp = (char*)malloc(g_main_ctx.arg_len);
+    memset(argp, 0, g_main_ctx.arg_len);
+    g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
+    for (i = 0; argv[i]; ++i) {
+        g_main_ctx.save_argv[i] = argp;
+        strcpy(g_main_ctx.save_argv[i], argv[i]);
+        argp += strlen(argv[i]) + 1;
+    }
+    g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
+
+    // save env
+    g_main_ctx.os_envp = environ;
+    g_main_ctx.envc = 0;
+    g_main_ctx.env_len = 0;
+    for (i = 0; environ[i]; ++i) {
+        g_main_ctx.env_len += strlen(environ[i]) + 1;
+    }
+    g_main_ctx.envc = i;
+    char* envp = (char*)malloc(g_main_ctx.env_len);
+    memset(envp, 0, g_main_ctx.env_len);
+    g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
+    for (i = 0; environ[i]; ++i) {
+        g_main_ctx.save_envp[i] = envp;
+        strcpy(g_main_ctx.save_envp[i], environ[i]);
+        envp += strlen(environ[i]) + 1;
+    }
+    g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
+
+    // parse env
+    for (i = 0; environ[i]; ++i) {
+        char* b = environ[i];
+        char* delim = strchr(b, '=');
+        if (delim == NULL) {
+            continue;
+        }
+        g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
+    }
+
+    /*
+    // print argv and envp
+    printf("---------------arg------------------------------\n");
+    for (auto& pair : g_main_ctx.arg_kv) {
+        printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
+    }
+    printf("---------------env------------------------------\n");
+    for (auto& pair : g_main_ctx.env_kv) {
+        printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
+    }
+
+    printf("PWD=%s\n", get_env("PWD"));
+    printf("USER=%s\n", get_env("USER"));
+    printf("HOME=%s\n", get_env("HOME"));
+    printf("LANG=%s\n", get_env("LANG"));
+    printf("TERM=%s\n", get_env("TERM"));
+    printf("SHELL=%s\n", get_env("SHELL"));
+    printf("================================================\n");
+    */
+
+    char logpath[MAX_PATH] = {0};
+    snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_path);
+    MKDIR(logpath);
+    snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_path, g_main_ctx.program_name);
+    snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_path, g_main_ctx.program_name);
+    snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
+
+    g_main_ctx.oldpid = getpid_from_pidfile();
+#ifdef __unix__
+    if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
+        g_main_ctx.oldpid = -1;
+    }
+#else
+
+#endif
+
+    return 0;
+}
+
+const char* get_arg(const char* key) {
+    auto iter = g_main_ctx.arg_kv.find(key);
+    if (iter == g_main_ctx.arg_kv.end()) {
+        return NULL;
+    }
+    return iter->second.c_str();
+}
+
+const char* get_env(const char* key) {
+    auto iter = g_main_ctx.env_kv.find(key);
+    if (iter == g_main_ctx.env_kv.end()) {
+        return NULL;
+    }
+    return iter->second.c_str();
+}
+
+#ifdef __unix__
+/*
+ * memory layout
+ * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
+ */
+void setproctitle(const char* title) {
+    //printf("proctitle=%s\n", title);
+    memset(g_main_ctx.os_argv[0], 0, g_main_ctx.arg_len + g_main_ctx.env_len);
+    strncpy(g_main_ctx.os_argv[0], title, g_main_ctx.arg_len + g_main_ctx.env_len);
+}
+#endif
+
+int create_pidfile() {
+    FILE* fp = fopen(g_main_ctx.pidfile, "w");
+    if (fp == NULL) {
+        printf("fopen [%s] error: %d\n", g_main_ctx.pidfile, errno);
+        return -10;
+    }
+
+    char pid[16] = {0};
+    snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
+    fwrite(pid, 1, strlen(pid), fp);
+    fclose(fp); atexit(delete_pidfile);
+    hlogi("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
+
+    return 0;
+}
+
+void delete_pidfile() {
+    remove(g_main_ctx.pidfile);
+    hlogi("delete_pidfile [%s]", g_main_ctx.pidfile);
+}
+
+pid_t getpid_from_pidfile() {
+    FILE* fp = fopen(g_main_ctx.pidfile, "r");
+    if (fp == NULL) {
+        //printf("fopen [%s] error: %d\n", g_conf_ctx.pidfile, errno);
+        return -1;
+    }
+    char pid[64];
+    int readbytes = fread(pid, 1, sizeof(pid), fp);
+    fclose(fp);
+    if (readbytes <= 0) {
+        //printf("fread [%s] bytes=%d\n", g_main_ctx.pidfile, readbytes);
+        return -1;
+    }
+    return atoi(pid);
+}
+

+ 4 - 0
hmain.h

@@ -41,4 +41,8 @@ const char* get_env(const char* key);
 void setproctitle(const char* title);
 #endif
 
+int      create_pidfile();
+void     delete_pidfile();
+pid_t    getpid_from_pidfile();
+
 #endif // H_MAIN_H_

+ 1 - 0
hplatform.h

@@ -38,6 +38,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <signal.h>
 
 #ifdef __unix__
 #define MKDIR(dir) mkdir(dir, 0777)

+ 55 - 0
hproc.h

@@ -0,0 +1,55 @@
+#ifndef H_PROC_H_
+#define H_PROC_H_
+
+#include "hdef.h"
+#include "hplatform.h"
+#include "hlog.h"
+#include "hmain.h"
+
+typedef struct proc_ctx_s {
+    pid_t           pid; // tid in win32
+    char            proctitle[256];
+    procedure_t     proc;
+    void*           userdata;
+} proc_ctx_t;
+
+#ifdef __unix__
+// unix use multi-processes
+inline int create_proc(proc_ctx_t* ctx) {
+    pid_t pid = fork();
+    if (pid < 0) {
+        hloge("fork error: %d", errno);
+        return -1;
+    } else if (pid == 0) {
+        // child proc
+        hlogi("proc start/running, pid=%d", getpid());
+        if (strlen(ctx->proctitle) != 0) {
+            setproctitle(ctx->proctitle);
+        }
+        if (ctx->proc) {
+            ctx->proc(ctx->userdata);
+        }
+        exit(0);
+    } else if (pid > 0) {
+        // parent proc
+    }
+    ctx->pid = pid;
+    return pid;
+}
+#elif defined(_WIN32)
+// win32 use multi-threads
+#include <process.h>
+inline int create_proc(proc_ctx_t* ctx) {
+    HANDLE h = (HANDLE)_beginthread(ctx->proc, 0, ctx->userdata);
+    if (h == NULL) {
+        hloge("_beginthread error: %d", errno);
+        return -1;
+    }
+    int tid = GetThreadId(h);
+    ctx->pid = tid;
+    hlogi("proc start/running, tid=%d", tid);
+    return tid;
+}
+#endif
+
+#endif // H_PROC_H_

+ 51 - 281
hmain.cpp.tmpl → main.cpp.tmpl

@@ -1,10 +1,8 @@
-#include <signal.h> // for signal,kill...
-
 #include "h.h"
-#include "hsysinfo.h"
 
-#include "hmain.h"
-main_ctx_t g_main_ctx;
+#define DEFAULT_WORKER_PROCESSES    4
+#define MAXNUM_WORKER_PROCESSES     1024
+static proc_ctx_t s_worker_processes[MAXNUM_WORKER_PROCESSES];
 
 typedef struct conf_ctx_s {
     IniParser* parser;
@@ -14,8 +12,6 @@ typedef struct conf_ctx_s {
 } conf_ctx_t;
 conf_ctx_t g_conf_ctx;
 
-#define DEFAULT_WORKER_PROCESSES    4
-#define MAXNUM_WORKER_PROCESSES     1024
 inline void conf_ctx_init(conf_ctx_t* ctx) {
     ctx->parser = new IniParser;
     ctx->loglevel = LOG_LEVEL_DEBUG;
@@ -23,198 +19,18 @@ inline void conf_ctx_init(conf_ctx_t* ctx) {
     ctx->port = 0;
 }
 
-static int      create_pidfile();
-static void     delete_pidfile();
-static pid_t    getpid_from_pidfile();
-
-static int  parse_cmdline(int argc, char** argv);
 static void print_version();
 static void print_help();
-static void handle_signal();
-static int  parse_confile(const char* confile);
-
-static int  master_process_init();
-static void master_process_exit();
-static int  master_process_cycle();
-
-static int  create_worker_processes(int worker_processes);
-static int  worker_process_cycle(void* ctx);
-
-int create_pidfile() {
-    FILE* fp = fopen(g_main_ctx.pidfile, "w");
-    if (fp == NULL) {
-        printf("fopen [%s] error: %d\n", g_main_ctx.pidfile, errno);
-        return -10;
-    }
 
-    char pid[16] = {0};
-    snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
-    fwrite(pid, 1, strlen(pid), fp);
-    fclose(fp); atexit(delete_pidfile); 
-    hlogi("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
-
-    return 0;
-}
-
-void delete_pidfile() {
-    remove(g_main_ctx.pidfile);
-    hlogi("delete_pidfile [%s]", g_main_ctx.pidfile);
-}
-
-pid_t getpid_from_pidfile() {
-    FILE* fp = fopen(g_main_ctx.pidfile, "r");
-    if (fp == NULL) {
-        //printf("fopen [%s] error: %d\n", g_conf_ctx.pidfile, errno);
-        return -1;
-    }
-    char pid[64];
-    int readbytes = fread(pid, 1, sizeof(pid), fp);
-    fclose(fp);
-    if (readbytes <= 0) {
-        printf("fread [%s] bytes=%d\n", g_main_ctx.pidfile, readbytes);
-        return -1;
-    }
-    return atoi(pid);
-}
-
-int main_ctx_init(int argc, char** argv) {
-    g_main_ctx.pid = getpid();
-    char* cwd = getcwd(g_main_ctx.run_path, sizeof(g_main_ctx.run_path));
-    if (cwd == NULL) {
-        printf("getcwd error\n");
-    }
-    //printf("run_path=%s\n", g_main_ctx.run_path);
-    const char* b = argv[0];
-    const char* e = b;
-    while (*e) ++e;
-    --e;
-    while (e >= b) {
-        if (*e == '/' || *e == '\\') {
-            break;
-        }
-        --e;
-    }
-    strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
-#ifdef _WIN32
-    if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
-        *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
-    }
-#endif
-    //printf("program_name=%s\n", g_main_ctx.program_name);
-
-    // save arg
-    int i = 0;
-    g_main_ctx.os_argv = argv;
-    g_main_ctx.argc = 0;
-    g_main_ctx.arg_len = 0;
-    for (i = 0; argv[i]; ++i) {
-        g_main_ctx.arg_len += strlen(argv[i]) + 1;
-    }
-    g_main_ctx.argc = i;
-    char* argp = (char*)malloc(g_main_ctx.arg_len);
-    memset(argp, 0, g_main_ctx.arg_len);
-    g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
-    for (i = 0; argv[i]; ++i) {
-        g_main_ctx.save_argv[i] = argp;
-        strcpy(g_main_ctx.save_argv[i], argv[i]);
-        argp += strlen(argv[i]) + 1;
-    }
-    g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
-
-    // save env
-    g_main_ctx.os_envp = environ;
-    g_main_ctx.envc = 0;
-    g_main_ctx.env_len = 0;
-    for (i = 0; environ[i]; ++i) {
-        g_main_ctx.env_len += strlen(environ[i]) + 1;
-    }
-    g_main_ctx.envc = i;
-    char* envp = (char*)malloc(g_main_ctx.env_len);
-    memset(envp, 0, g_main_ctx.env_len);
-    g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
-    for (i = 0; environ[i]; ++i) {
-        g_main_ctx.save_envp[i] = envp;
-        strcpy(g_main_ctx.save_envp[i], environ[i]);
-        envp += strlen(environ[i]) + 1;
-    }
-    g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
-
-    // parse env
-    for (i = 0; environ[i]; ++i) {
-        char* b = environ[i];
-        char* delim = strchr(b, '=');
-        if (delim == NULL) {
-            continue;
-        }
-        g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
-    }
-
-    /*
-    // print argv and envp
-    printf("---------------arg------------------------------\n");
-    for (auto& pair : g_main_ctx.arg_kv) {
-        printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
-    }
-    printf("---------------env------------------------------\n");
-    for (auto& pair : g_main_ctx.env_kv) {
-        printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
-    }
-
-    printf("PWD=%s\n", get_env("PWD"));
-    printf("USER=%s\n", get_env("USER"));
-    printf("HOME=%s\n", get_env("HOME"));
-    printf("LANG=%s\n", get_env("LANG"));
-    printf("TERM=%s\n", get_env("TERM"));
-    printf("SHELL=%s\n", get_env("SHELL"));
-    printf("================================================\n");
-    */
-
-    char logpath[MAX_PATH] = {0};
-    snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_path);
-    MKDIR(logpath);
-    snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_path, g_main_ctx.program_name);
-    snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_path, g_main_ctx.program_name);
-    snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
-
-    g_main_ctx.oldpid = getpid_from_pidfile();
-#ifdef __unix__
-    if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
-        g_main_ctx.oldpid = -1;
-    }
-#else
-
-#endif
-
-    return 0;
-}
-
-const char* get_arg(const char* key) {
-    auto iter = g_main_ctx.arg_kv.find(key);
-    if (iter == g_main_ctx.arg_kv.end()) {
-        return NULL;
-    }
-    return iter->second.c_str();
-}
+static int  parse_cmdline(int argc, char** argv);
+static int  parse_confile(const char* confile);
 
-const char* get_env(const char* key) {
-    auto iter = g_main_ctx.env_kv.find(key);
-    if (iter == g_main_ctx.env_kv.end()) {
-        return NULL;
-    }
-    return iter->second.c_str();
-}
+static int  signal_init();
+static void signal_cleanup();
+static void handle_signal();
 
-#ifdef __unix__
-/*
- * memory layout
- * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
- */
-void setproctitle(const char* title) {
-    //printf("proctitle=%s\n", title);
-    memset(g_main_ctx.os_argv[0], 0, g_main_ctx.arg_len + g_main_ctx.env_len);
-    strncpy(g_main_ctx.os_argv[0], title, g_main_ctx.arg_len + g_main_ctx.env_len);
-}
-#endif
+static void master_proc(void* userdata);
+static void worker_proc(void* userdata);
 
 // unix short style
 static char options[] = "hvc:ts:dp:";
@@ -340,64 +156,23 @@ int parse_confile(const char* confile) {
     return 0;
 }
 
-int master_process_cycle() {
-    while (1) msleep(1);
-    return 0;
-}
-
-int worker_process_cycle(void* ctx) {
-    while (1) msleep(1);
-    return 0;
-}
-
 #ifdef __unix__
 // unix use signal
-// unix use multi-processes
 // we use SIGTERM to quit process
 #define SIGNAL_TERMINATE    SIGTERM
 #include <sys/wait.h>
 
-static pid_t s_worker_processes[MAXNUM_WORKER_PROCESSES];
-
-int create_worker_processes(int worker_processes) {
-    for (int i = 0; i < worker_processes; ++i) {
-        pid_t pid = fork();
-        if (pid < 0) {
-            hloge("fork error: %d", errno);
-            return errno;
-        }
-        if (pid == 0) {
-            hlogi("worker process start/running, pid=%d", getpid());
-            char proctitle[256] = {0};
-            snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
-            setproctitle(proctitle);
-
-            long port = g_conf_ctx.port + i + 1;
-            worker_process_cycle((void*)port);
-            exit(0);
-        }
-
-        for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
-            if (s_worker_processes[i] <= 0) {
-                s_worker_processes[i] = pid;
-                break;
-            }
-        }
-    }
-    return 0;
-}
-
-void master_process_signal_handler(int signo) {
+void signal_handler(int signo) {
     hlogi("pid=%d recv signo=%d", getpid(), signo);
     switch (signo) {
     case SIGINT:
     case SIGNAL_TERMINATE:
-        hlogi("killall worker processes");
+        hlogi("killall processes");
         signal(SIGCHLD, SIG_IGN);
         for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
-            if (s_worker_processes[i] <= 0) break;
-            kill(s_worker_processes[i], SIGKILL);
-            s_worker_processes[i] = -1;
+            if (s_worker_processes[i].pid <= 0) break;
+            kill(s_worker_processes[i].pid, SIGKILL);
+            s_worker_processes[i].pid = -1;
         }
         exit(0);
         break;
@@ -406,14 +181,14 @@ void master_process_signal_handler(int signo) {
         pid_t pid = 0;
         int status = 0;
         while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
-            hlogw("worker process stop/waiting, pid=%d status=%d", pid, status);
+            hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
             for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
-                if (s_worker_processes[i] == pid) {
-                    s_worker_processes[i] = -1;
+                if (s_worker_processes[i].pid == pid) {
+                    s_worker_processes[i].pid = -1;
+                    create_proc(&s_worker_processes[i]);
                     break;
                 }
             }
-            create_worker_processes(1);
         }
     }
         break;
@@ -422,28 +197,19 @@ void master_process_signal_handler(int signo) {
     }
 }
 
-int master_process_init() {
-    char proctitle[256] = {0};
-    snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
-    setproctitle(proctitle);
-
-    signal(SIGINT, master_process_signal_handler);
-    signal(SIGCHLD, master_process_signal_handler);
-    signal(SIGNAL_TERMINATE, master_process_signal_handler);
-
-    for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
-        s_worker_processes[i] = -1;
-    }
+int signal_init() {
+    signal(SIGINT, signal_handler);
+    signal(SIGCHLD, signal_handler);
+    signal(SIGNAL_TERMINATE, signal_handler);
 
-    atexit(master_process_exit);
+    atexit(signal_cleanup);
     return 0;
 }
 
-void master_process_exit() {
+void signal_cleanup() {
 }
 #elif defined(_WIN32)
 // win32 use Event
-// win32 use multi-threads
 static HANDLE s_hEventTerm = NULL;
 
 #include <mmsystem.h>
@@ -459,7 +225,7 @@ void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1,
     }
 }
 
-int master_process_init() {
+int signal_init() {
     char eventname[MAX_PATH] = {0};
     snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
     s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
@@ -467,28 +233,14 @@ int master_process_init() {
 
     timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
 
-    atexit(master_process_exit);
+    atexit(signal_cleanup);
     return 0;
 }
 
-void master_process_exit() {
+void signal_cleanup() {
     CloseHandle(s_hEventTerm);
     s_hEventTerm = NULL;
 }
-
-#include <process.h>
-void thread_proc(void* ctx) {
-    hlogi("worker thread start/running, tid=%d", gettid());
-    worker_process_cycle(ctx);
-}
-
-int create_worker_processes(int worker_processes) {
-    for (int i = 0; i < worker_processes; ++i) {
-        long port = g_conf_ctx.port + i + 1;
-        _beginthread(thread_proc, 0, (void*)port);
-    }
-    return 0;
-}
 #endif
 
 void handle_signal() {
@@ -536,6 +288,14 @@ void handle_signal() {
     }
 }
 
+void master_proc(void* userdata) {
+    while(1) msleep(1000);
+}
+
+void worker_proc(void* userdata) {
+    while(1) msleep(1000);
+}
+
 int main(int argc, char** argv) {
     // g_main_ctx
     main_ctx_init(argc, argv);
@@ -572,9 +332,8 @@ int main(int argc, char** argv) {
         exit(0);
     }
 
-    master_process_init();
-
     // signal
+    signal_init();
     handle_signal();
 
 #ifdef __unix__
@@ -589,15 +348,26 @@ int main(int argc, char** argv) {
         // parent process exit after daemon, so pid changed.
         g_main_ctx.pid = getpid();
     }
+    // proctitle
+    char proctitle[256] = {0};
+    snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
+    setproctitle(proctitle);
 #endif
 
     // pidfile
     create_pidfile();
     hlogi("%s start/running, pid=%d", g_main_ctx.program_name, g_main_ctx.pid);
 
-    // cycle
-    create_worker_processes(g_conf_ctx.worker_processes);
-    master_process_cycle();
+    // master-worker proc
+    memset(s_worker_processes, 0, sizeof(s_worker_processes));
+    for (int i = 0; i < g_conf_ctx.worker_processes; ++i) {
+        proc_ctx_t* ctx = &s_worker_processes[i];
+        snprintf(ctx->proctitle, sizeof(ctx->proctitle), "%s: worker process", g_main_ctx.program_name);
+        ctx->proc = worker_proc;
+        ctx->userdata = NULL;
+        create_proc(ctx);
+    }
+    master_proc(NULL);
 
     return 0;
 }