9
9
#include " ipc/ipc.h"
10
10
#include " ipc/debug.h"
11
11
12
-
13
-
14
12
typedef struct
15
13
{
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.
18
16
19
17
// internal
20
18
pid_t pid;
21
19
int alive;
20
+ char * args[10 ]; // increase as needed
22
21
23
22
} child;
24
23
@@ -27,14 +26,17 @@ child children[] =
27
26
// ==================== FILL IN PROCESSES ====================
28
27
{
29
28
.name = " /usr/bin/cpp_ipc_brief_demo_prod" ,
29
+ .respawn = 1 ,
30
30
},
31
31
{
32
32
.name = " /usr/bin/cpp_ipc_brief_demo_cons" ,
33
+ .args = { " --my_option" , " --my_other_option" , },
33
34
},
34
35
// ===========================================================
35
36
};
36
37
37
38
#define n_children (int )(sizeof (children) / sizeof (children[0 ]))
39
+ #define lengthof (var ) (sizeof (var) / sizeof (var[0 ]))
38
40
39
41
int fork_child (child *c)
40
42
{
@@ -48,7 +50,12 @@ int fork_child(child *c)
48
50
49
51
setpgid (0 , 0 ); // switch process group so ctrl-c only interrupts god
50
52
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
+
52
59
execv (c->name , child_argv);
53
60
}
54
61
@@ -63,7 +70,6 @@ void exit_handler(int sig)
63
70
{
64
71
debug (" got signal %d\n " , sig);
65
72
66
-
67
73
// kill the children manually, since they're in a different group
68
74
signal (SIGCHLD, SIG_IGN);
69
75
for (int i=0 ; i < n_children; ++i)
@@ -101,13 +107,13 @@ void child_handler(int sig)
101
107
c->alive = 0 ;
102
108
debug (" child %u (%s) exited with status %d\n " , c->pid , c->name , status);
103
109
104
- if (c->restart != 0 )
110
+ if (c->respawn != 0 )
105
111
{
106
- debug (" restarting child\n " );
112
+ debug (" respawning child\n " );
107
113
fork_child (c);
108
114
109
- if (c->restart > 0 )
110
- --c->restart ;
115
+ if (c->respawn > 0 )
116
+ --c->respawn ;
111
117
}
112
118
}
113
119
0 commit comments