Watching worker processes

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-03 18:36:29 +08:00
parent 9ae7b25f84
commit f044dc97a9
2 changed files with 27 additions and 3 deletions

View File

@ -136,10 +136,19 @@ static void uh_stop_accept(struct uh_server_internal *srv)
ev_io_stop(srv->loop, &srv->ior);
}
static void uh_worker_exit(struct ev_loop *loop, struct ev_child *w, int revents)
{
struct worker *wk = container_of(w, struct worker, w);
uh_log_info("worker %d exit\n", wk->i);
free(wk);
}
static void uh_start_worker(struct uh_server *srv, int n)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;
pid_t pid;
pid_t pids[20];
int i;
if (n < 0)
@ -151,18 +160,28 @@ static void uh_start_worker(struct uh_server *srv, int n)
uh_stop_accept(srvi);
for (i = 0; i < n; i++) {
pid = fork();
switch (pid) {
pids[i] = fork();
switch (pids[i]) {
case -1:
uh_log_err("fork: %s\n", strerror(errno));
return;
case 0:
ev_loop_fork(srvi->loop);
uh_start_accept(srvi);
uh_log_info("worker %d started\n", i);
ev_run(srvi->loop, 0);
return;
}
}
while (i-- > 0) {
struct worker *w = calloc(1, sizeof(struct worker));
w->i = i;
ev_child_init(&w->w, uh_worker_exit, pids[i], 0);
ev_child_start(srvi->loop, &w->w);
}
}
struct uh_server *uh_server_new(struct ev_loop *loop, const char *host, int port)

View File

@ -45,4 +45,9 @@ struct uh_server_internal {
struct uh_path_handler *handlers;
};
struct worker {
struct ev_child w;
int i;
};
#endif