Skip to content

Commit 8857e0c

Browse files
committed
god example: configure line arguments for children
1 parent 76f3bef commit 8857e0c

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

examples/cpp_ipc_brief_demo/god.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
#include "ipc/ipc.h"
1010
#include "ipc/debug.h"
1111

12-
13-
1412
typedef struct
1513
{
16-
const char *name;
17-
int restart; // -1 = infinite , 0 = never, 1 = once, 2 = twice, etc.
14+
const char * const name;
15+
int respawn; // -1 = infinite , 0 = never, 1 = once, 2 = twice, etc.
1816

1917
// internal
2018
pid_t pid;
2119
int alive;
20+
char* args[10]; // increase as needed
2221

2322
} child;
2423

@@ -27,14 +26,17 @@ child children[] =
2726
//==================== FILL IN PROCESSES ====================
2827
{
2928
.name = "/usr/bin/cpp_ipc_brief_demo_prod",
29+
.respawn = 1,
3030
},
3131
{
3232
.name = "/usr/bin/cpp_ipc_brief_demo_cons",
33+
.args = { "--my_option", "--my_other_option", },
3334
},
3435
//===========================================================
3536
};
3637

3738
#define n_children (int)(sizeof(children) / sizeof(children[0]))
39+
#define lengthof(var) (sizeof(var) / sizeof(var[0]))
3840

3941
int fork_child(child *c)
4042
{
@@ -48,7 +50,12 @@ int fork_child(child *c)
4850

4951
setpgid(0, 0); // switch process group so ctrl-c only interrupts god
5052

51-
char * const child_argv[] = { (char*)(c->name), NULL };
53+
char * child_argv[lengthof(c->args) + 2];
54+
child_argv[0] = (char*)(c->name);
55+
for (int i=0; i < lengthof(c->args); ++i)
56+
child_argv[i+1] = c->args[i];
57+
child_argv[lengthof(c->args) + 1] = NULL;
58+
5259
execv(c->name, child_argv);
5360
}
5461

@@ -63,7 +70,6 @@ void exit_handler(int sig)
6370
{
6471
debug("got signal %d\n", sig);
6572

66-
6773
// kill the children manually, since they're in a different group
6874
signal(SIGCHLD, SIG_IGN);
6975
for (int i=0; i < n_children; ++i)
@@ -101,13 +107,13 @@ void child_handler(int sig)
101107
c->alive = 0;
102108
debug("child %u (%s) exited with status %d\n", c->pid, c->name, status);
103109

104-
if (c->restart != 0)
110+
if (c->respawn != 0)
105111
{
106-
debug("restarting child\n");
112+
debug("respawning child\n");
107113
fork_child(c);
108114

109-
if (c->restart > 0)
110-
--c->restart;
115+
if (c->respawn > 0)
116+
--c->respawn;
111117
}
112118
}
113119

0 commit comments

Comments
 (0)