summaryrefslogtreecommitdiff
path: root/parallel.c
blob: 0895c1372465b5b080121f177178800c7264a66b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 *  parallel.c - run commands in parallel until you run out of commands
 *
 *  Copyright © 2008  Tollef Fog Heen <tfheen@err.no>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  version 2 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>

void usage() {
	printf("parallel [OPTIONS] command -- arguments: for each argument, "
	       "run command with argument\n");
	exit(1);
}

void exec_child(char **command, char *argument, int replace_cb) {
	char **argv;
	int argc = 0;
	int i;

	while (command[argc] != 0) {
		argc++;
	}
	if (replace_cb == 0)
		argc++;
	argv = calloc(sizeof(char*), argc+1);
	for (i = 0; i < argc; i++) {
		argv[i] = command[i];
		if (replace_cb && (strcmp(argv[i], "{}") == 0))
			argv[i] = argument;
	}
	if (replace_cb == 0)
		argv[i-1] = argument;
	if (fork() == 0) {
		/* Child */
		execvp(argv[0], argv);
		exit(1);
	}
	return;
}

int wait_for_child(int options) {
	id_t id_ignored = 0;
	siginfo_t infop;

	infop.si_pid = 0;
	waitid(P_ALL, id_ignored, &infop, WEXITED | options);
	if (infop.si_pid == 0)
		return -1; /* Nothing to wait for */
	if (infop.si_code == CLD_EXITED)
		return infop.si_status;
	return 1;
}

int main(int argc, char **argv) {
	int maxjobs = -1;
	int curjobs = 0;
	int maxload = -1;
	int opt;
	char **command = calloc(sizeof(char*), argc);
	char **arguments = NULL;
	int argidx = 0;
	int cidx = 0;
	int returncode = 0;
	int replace_cb = 0;
	char *t;

	while ((opt = getopt(argc, argv, "+hij:l:")) != -1) {
		switch (opt) {
		case 'h':
			usage();
			break;
		case 'i':
			replace_cb = 1;
			break;
		case 'j':
			errno = 0;
			maxjobs = strtoul(optarg, &t, 0);
			if (errno != 0 || (t-optarg) != strlen(optarg)) {
				fprintf(stderr, "option '%s' is not a number\n",
					optarg);
				exit(2);
			}
			break;
		case 'l':
			errno = 0;
			maxload = strtoul(optarg, &t, 0);
			if (errno != 0 || (t-optarg) != strlen(optarg)) {
				fprintf(stderr, "option '%s' is not a number\n",
					optarg);
				exit(2);
			}
			break;
		default: /* ’?’ */
			usage();
			break;
		}
	}

	if (maxjobs < 0 && maxload < 0) {
		maxjobs = 1; /* XXX: Maybe we should try to autodetect
				number of CPUs? */
	}

	while (optind < argc) {
		if (strcmp(argv[optind], "--") == 0) {
			int i;
			size_t mem = (sizeof (char*)) * (argc - optind);

			arguments = malloc(mem);
			if (! arguments) {
				exit(1);
			}
			memset(arguments, 0, mem);
			optind++; /* Skip the -- separator, skip after
				   * malloc so we have a trailing
				   * null */

			for (i = 0; i < argc - optind; i++) {
				arguments[i] = strdup(argv[optind + i]);
			}
			optind += i;
		}
		else {
			command[cidx] = strdup(argv[optind]);
			cidx++;
		}
		optind++;
	}

	while (arguments[argidx] != 0) {
		double load;

		getloadavg(&load, 1);

		if ((maxjobs > 0 && curjobs < maxjobs) ||
		    (maxload > 0 && load < maxload)) {
			exec_child(command, arguments[argidx], replace_cb);
			argidx++;
			curjobs++;
		}

		if (maxjobs > 0 && curjobs == maxjobs) {
			returncode |= wait_for_child(0);
			curjobs--;
		}

		if (maxload > 0 && load > maxload) {
			int r;
			sleep(1); /* XXX We should have a better
				   * heurestic than this */
			r = wait_for_child(WNOHANG);
			if (r > 0) {
				returncode |= r;
				curjobs--;
			}
		}
	}
	while (curjobs > 0) {
		returncode |= wait_for_child(0);
		curjobs--;
	}

	return returncode;
}