LibChildControl 能创建一个线程来管理子进程,并且在失败的时候重启他们。
使用示例:
int child_process (void *data) { int id = *(int *)data; int delay = (id + 1) * 1000; printf ("TEST: Start child %d.\n", id); int i; for (i = 0; i < nb_loops; i++) { printf ("TEST: Child %d is working (loop %d/%d)\n", id, i + 1, nb_loops); int j; float r = 0; for (j = 0; j < 100 * id; j++) { float x = 0; do x = (float) rand () / ((float)RAND_MAX + 1); while (x == 0); float y = (float) rand () / ((float)RAND_MAX + 1); float z = sqrtf (-2. * logf (x)) * cosf (2 * M_PI * y); r += z * (1 << 31); usleep (1000); } } kill (getpid (), SIGTERM); return 0; } int child_term (void *data) { int id = *(int *)data; printf ("TEST: Child %d is dieing.\n", id); return 0; } #define nb_children 2 #define nb_loops 10 int main (int argc, char *argv[]) { int d[nb_children]; int i; for (i = 0; i < nb_children; i++) { d[i] = i + 1; register_child (i + 1, NULL, NULL, child_process, child_term, (void *)(d + i)); } sleep (100); return 0; }