File parallel.c added (mode: 100644) (index 0000000..09588d6) |
|
1 |
|
|
|
2 |
|
/* |
|
3 |
|
* parallel.c - run commands in parallel until you run out of commands |
|
4 |
|
* |
|
5 |
|
* Copyright © 2008 Tollef Fog Heen <tfheen@err.no> |
|
6 |
|
* |
|
7 |
|
* This program is free software; you can redistribute it and/or |
|
8 |
|
* modify it under the terms of the GNU General Public License |
|
9 |
|
* version 2 as published by the Free Software Foundation. |
|
10 |
|
* |
|
11 |
|
* This program is distributed in the hope that it will be useful, |
|
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
|
* General Public License for more details. |
|
15 |
|
* |
|
16 |
|
* You should have received a copy of the GNU General Public License |
|
17 |
|
* along with this program; if not, write to the Free Software |
|
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
|
* USA |
|
20 |
|
* |
|
21 |
|
*/ |
|
22 |
|
|
|
23 |
|
#define _GNU_SOURCE |
|
24 |
|
#include <unistd.h> |
|
25 |
|
#include <string.h> |
|
26 |
|
#include <stdio.h> |
|
27 |
|
#include <sys/time.h> |
|
28 |
|
#include <time.h> |
|
29 |
|
#include <stdlib.h> |
|
30 |
|
#include <sys/select.h> |
|
31 |
|
#include <sys/types.h> |
|
32 |
|
#include <sys/wait.h> |
|
33 |
|
|
|
34 |
|
void usage() |
|
35 |
|
{ |
|
36 |
|
printf("parallel [OPTIONS] command -- arguments: for each argument," |
|
37 |
|
"run command with argument\n"); |
|
38 |
|
exit(1); |
|
39 |
|
} |
|
40 |
|
|
|
41 |
|
void exec_child(char **command, char *argument) |
|
42 |
|
{ |
|
43 |
|
char **argv; |
|
44 |
|
int argc = 0; |
|
45 |
|
int i; |
|
46 |
|
|
|
47 |
|
while (command[argc] != 0) { |
|
48 |
|
argc++; |
|
49 |
|
} |
|
50 |
|
argc++; |
|
51 |
|
argv = calloc(sizeof(char*), argc+1); |
|
52 |
|
for (i = 0; i < argc; i++) { |
|
53 |
|
argv[i] = command[i]; |
|
54 |
|
} |
|
55 |
|
argv[i-1] = argument; |
|
56 |
|
if (fork() == 0) { |
|
57 |
|
/* Child */ |
|
58 |
|
execvp(argv[0], argv); |
|
59 |
|
exit(1); |
|
60 |
|
} |
|
61 |
|
return; |
|
62 |
|
} |
|
63 |
|
|
|
64 |
|
int main(int argc, char **argv) |
|
65 |
|
{ |
|
66 |
|
int maxjobs = -1; |
|
67 |
|
int curjobs = 0; |
|
68 |
|
int maxload = -1; |
|
69 |
|
int opt; |
|
70 |
|
char **command = calloc(sizeof(char*), argc); |
|
71 |
|
char **arguments; |
|
72 |
|
int argidx = 0; |
|
73 |
|
int cidx = 0; |
|
74 |
|
|
|
75 |
|
while ((opt = getopt(argc, argv, "+hj:l:")) != -1) { |
|
76 |
|
switch (opt) { |
|
77 |
|
case 'h': |
|
78 |
|
usage(); |
|
79 |
|
break; |
|
80 |
|
case 'j': |
|
81 |
|
maxjobs = atoi(optarg); |
|
82 |
|
break; |
|
83 |
|
case 'l': |
|
84 |
|
maxload = atoi(optarg); |
|
85 |
|
break; |
|
86 |
|
default: /* ’?’ */ |
|
87 |
|
usage(); |
|
88 |
|
break; |
|
89 |
|
} |
|
90 |
|
} |
|
91 |
|
|
|
92 |
|
if (maxjobs < 0) { |
|
93 |
|
usage(); |
|
94 |
|
} |
|
95 |
|
|
|
96 |
|
while (optind < argc) { |
|
97 |
|
if (strcmp(argv[optind], "--") == 0) { |
|
98 |
|
int i; |
|
99 |
|
size_t mem = (sizeof (char*)) * (argc - optind); |
|
100 |
|
|
|
101 |
|
arguments = malloc(mem); |
|
102 |
|
if (! arguments) { |
|
103 |
|
exit(1); |
|
104 |
|
} |
|
105 |
|
memset(arguments, 0, mem); |
|
106 |
|
optind++; /* Skip the -- separator, skip after |
|
107 |
|
* malloc so we have a trailing |
|
108 |
|
* null */ |
|
109 |
|
|
|
110 |
|
for (i = 0; i < argc - optind; i++) { |
|
111 |
|
arguments[i] = strdup(argv[optind + i]); |
|
112 |
|
} |
|
113 |
|
optind += i; |
|
114 |
|
} else { |
|
115 |
|
command[cidx] = strdup(argv[optind]); |
|
116 |
|
cidx++; |
|
117 |
|
} |
|
118 |
|
optind++; |
|
119 |
|
} |
|
120 |
|
|
|
121 |
|
while (arguments[argidx] != 0) { |
|
122 |
|
if (maxjobs > 0 && curjobs < maxjobs) { |
|
123 |
|
exec_child(command, arguments[argidx]); |
|
124 |
|
argidx++; |
|
125 |
|
curjobs++; |
|
126 |
|
} |
|
127 |
|
|
|
128 |
|
if (maxjobs > 0 && curjobs == maxjobs) { |
|
129 |
|
id_t id_ignored; |
|
130 |
|
siginfo_t infop_ignored; |
|
131 |
|
waitid(P_ALL, id_ignored, &infop_ignored, WEXITED); |
|
132 |
|
curjobs--; |
|
133 |
|
} |
|
134 |
|
} |
|
135 |
|
while (curjobs > 0) { |
|
136 |
|
id_t id_ignored; |
|
137 |
|
siginfo_t infop_ignored; |
|
138 |
|
waitid(P_ALL, id_ignored, &infop_ignored, WEXITED); |
|
139 |
|
curjobs--; |
|
140 |
|
} |
|
141 |
|
|
|
142 |
|
return 0; |
|
143 |
|
} |
|
144 |
|
|