File parallel.c changed (mode: 100644) (index 2333a3d..438bc93) |
... |
... |
void usage() |
38 |
38 |
exit(1); |
exit(1); |
39 |
39 |
} |
} |
40 |
40 |
|
|
41 |
|
void exec_child(char **command, char *argument) |
|
|
41 |
|
void exec_child(char **command, char *argument, int replace_cb) |
42 |
42 |
{ |
{ |
43 |
43 |
char **argv; |
char **argv; |
44 |
44 |
int argc = 0; |
int argc = 0; |
|
... |
... |
void exec_child(char **command, char *argument) |
47 |
47 |
while (command[argc] != 0) { |
while (command[argc] != 0) { |
48 |
48 |
argc++; |
argc++; |
49 |
49 |
} |
} |
50 |
|
argc++; |
|
|
50 |
|
if (replace_cb == 0) |
|
51 |
|
argc++; |
51 |
52 |
argv = calloc(sizeof(char*), argc+1); |
argv = calloc(sizeof(char*), argc+1); |
52 |
53 |
for (i = 0; i < argc; i++) { |
for (i = 0; i < argc; i++) { |
53 |
54 |
argv[i] = command[i]; |
argv[i] = command[i]; |
|
55 |
|
if (replace_cb && (strcmp(argv[i], "{}") == 0)) |
|
56 |
|
argv[i] = argument; |
54 |
57 |
} |
} |
55 |
|
argv[i-1] = argument; |
|
|
58 |
|
if (replace_cb == 0) |
|
59 |
|
argv[i-1] = argument; |
56 |
60 |
if (fork() == 0) { |
if (fork() == 0) { |
57 |
61 |
/* Child */ |
/* Child */ |
58 |
62 |
execvp(argv[0], argv); |
execvp(argv[0], argv); |
|
... |
... |
void exec_child(char **command, char *argument) |
61 |
65 |
return; |
return; |
62 |
66 |
} |
} |
63 |
67 |
|
|
64 |
|
int wait_for_child(void) |
|
|
68 |
|
int wait_for_child(int options) |
65 |
69 |
{ |
{ |
66 |
70 |
id_t id_ignored = 0; |
id_t id_ignored = 0; |
67 |
71 |
siginfo_t infop; |
siginfo_t infop; |
68 |
|
waitid(P_ALL, id_ignored, &infop, WEXITED); |
|
|
72 |
|
|
|
73 |
|
infop.si_pid = 0; |
|
74 |
|
waitid(P_ALL, id_ignored, &infop, WEXITED | options); |
|
75 |
|
if (infop.si_pid == 0) |
|
76 |
|
return -1; /* Nothing to wait for */ |
69 |
77 |
if (infop.si_code == CLD_EXITED) |
if (infop.si_code == CLD_EXITED) |
70 |
78 |
return infop.si_status; |
return infop.si_status; |
71 |
79 |
return 1; |
return 1; |
|
... |
... |
int main(int argc, char **argv) |
82 |
90 |
int argidx = 0; |
int argidx = 0; |
83 |
91 |
int cidx = 0; |
int cidx = 0; |
84 |
92 |
int returncode = 0; |
int returncode = 0; |
|
93 |
|
int replace_cb = 0; |
85 |
94 |
|
|
86 |
|
while ((opt = getopt(argc, argv, "+hj:l:")) != -1) { |
|
|
95 |
|
while ((opt = getopt(argc, argv, "+hij:l:")) != -1) { |
87 |
96 |
switch (opt) { |
switch (opt) { |
88 |
97 |
case 'h': |
case 'h': |
89 |
98 |
usage(); |
usage(); |
90 |
99 |
break; |
break; |
|
100 |
|
case 'i': |
|
101 |
|
replace_cb = 1; |
|
102 |
|
break; |
91 |
103 |
case 'j': |
case 'j': |
92 |
104 |
maxjobs = atoi(optarg); |
maxjobs = atoi(optarg); |
93 |
105 |
break; |
break; |
|
... |
... |
int main(int argc, char **argv) |
100 |
112 |
} |
} |
101 |
113 |
} |
} |
102 |
114 |
|
|
103 |
|
if (maxjobs < 0) { |
|
|
115 |
|
if (maxjobs < 0 && maxload < 0) { |
104 |
116 |
usage(); |
usage(); |
105 |
117 |
} |
} |
106 |
118 |
|
|
|
... |
... |
int main(int argc, char **argv) |
130 |
142 |
} |
} |
131 |
143 |
|
|
132 |
144 |
while (arguments[argidx] != 0) { |
while (arguments[argidx] != 0) { |
133 |
|
if (maxjobs > 0 && curjobs < maxjobs) { |
|
134 |
|
exec_child(command, arguments[argidx]); |
|
|
145 |
|
double load; |
|
146 |
|
|
|
147 |
|
getloadavg(&load, 1); |
|
148 |
|
|
|
149 |
|
if ((maxjobs > 0 && curjobs < maxjobs) || |
|
150 |
|
(maxload > 0 && load < maxload)) { |
|
151 |
|
exec_child(command, arguments[argidx], replace_cb); |
135 |
152 |
argidx++; |
argidx++; |
136 |
153 |
curjobs++; |
curjobs++; |
137 |
154 |
} |
} |
138 |
155 |
|
|
139 |
156 |
if (maxjobs > 0 && curjobs == maxjobs) { |
if (maxjobs > 0 && curjobs == maxjobs) { |
140 |
|
returncode |= wait_for_child(); |
|
|
157 |
|
returncode |= wait_for_child(0); |
141 |
158 |
curjobs--; |
curjobs--; |
142 |
159 |
} |
} |
|
160 |
|
|
|
161 |
|
if (maxload > 0 && load > maxload) { |
|
162 |
|
int r; |
|
163 |
|
sleep(1); /* XXX We should have a better |
|
164 |
|
* heurestic than this */ |
|
165 |
|
r = wait_for_child(WNOHANG); |
|
166 |
|
if (r > 0) { |
|
167 |
|
returncode |= r; |
|
168 |
|
curjobs--; |
|
169 |
|
} |
|
170 |
|
} |
143 |
171 |
} |
} |
144 |
172 |
while (curjobs > 0) { |
while (curjobs > 0) { |
145 |
|
returncode |= wait_for_child(); |
|
|
173 |
|
returncode |= wait_for_child(0); |
146 |
174 |
curjobs--; |
curjobs--; |
147 |
175 |
} |
} |
148 |
176 |
|
|