diff options
-rw-r--r-- | debian/changelog | 2 | ||||
-rw-r--r-- | parallel.c | 44 | ||||
-rw-r--r-- | parallel.docbook | 21 |
3 files changed, 50 insertions, 17 deletions
diff --git a/debian/changelog b/debian/changelog index 548d301..00fc011 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,6 +2,8 @@ moreutils (0.38) UNRELEASED; urgency=low * Description improvements. Closes: #549450 (Thanks, Justin B Rye) + * parallel: Allow running independent commands, + like `parallel -j3 -- ls df "echo hi"` -- Joey Hess <joeyh@debian.org> Mon, 05 Oct 2009 13:33:07 -0400 @@ -33,8 +33,9 @@ #include <unistd.h> void usage() { - printf("parallel [OPTIONS] command -- arguments: for each argument, " - "run command with argument\n"); + printf("parallel [OPTIONS] command -- arguments\n\tfor each argument, " + "run command with argument, in parallel\n"); + printf("parallel [OPTIONS] -- commands\n\trun specified commands in parallel\n"); exit(1); } @@ -49,17 +50,24 @@ void exec_child(char **command, char **arguments, int replace_cb, int nargs) { if (replace_cb == 0) argc++; argv = calloc(sizeof(char*), argc + nargs); - for (i = 0; i < argc; i++) { - argv[i] = command[i]; - if (replace_cb && (strcmp(argv[i], "{}") == 0)) - argv[i] = arguments[0]; + if (command[0]) { + for (i = 0; i < argc; i++) { + argv[i] = command[i]; + if (replace_cb && (strcmp(argv[i], "{}") == 0)) + argv[i] = arguments[0]; + } + if (replace_cb == 0) + memcpy(argv + i - 1, arguments, nargs * sizeof(char *)); + if (fork() == 0) { + /* Child */ + execvp(argv[0], argv); + exit(1); + } } - if (replace_cb == 0) - memcpy(argv + i - 1, arguments, nargs * sizeof(char *)); - if (fork() == 0) { - /* Child */ - execvp(argv[0], argv); - exit(1); + else { + if (fork() == 0) { + exit(system(arguments[0])); + } } return; } @@ -92,7 +100,8 @@ int main(int argc, char **argv) { int replace_cb = 0; char *t; - while ((opt = getopt(argc, argv, "+hij:l:n:")) != -1) { + while ((argv[optind] && strcmp(argv[optind], "--") != 0) && + (opt = getopt(argc, argv, "+hij:l:n:")) != -1) { switch (opt) { case 'h': usage(); @@ -132,7 +141,7 @@ int main(int argc, char **argv) { break; } } - + if (replace_cb && argsatonce > 1) { fprintf(stderr, "options -i and -n are incomaptible\n"); exit(2); @@ -146,7 +155,7 @@ int main(int argc, char **argv) { maxjobs = 1; #endif } - + while (optind < argc) { if (strcmp(argv[optind], "--") == 0) { int i; @@ -170,6 +179,11 @@ int main(int argc, char **argv) { optind++; } + if (argsatonce > 1 && ! command[0]) { + fprintf(stderr, "option -n cannot be used without a command\n"); + exit(2); + } + while (argidx < arglen) { double load; diff --git a/parallel.docbook b/parallel.docbook index 046bb6c..b09350d 100644 --- a/parallel.docbook +++ b/parallel.docbook @@ -37,8 +37,14 @@ Written by Joey Hess <command>parallel</command> <arg>options</arg> <arg>command</arg> - <arg>--</arg> - <arg>arguments</arg> + <command>--</command> + <arg>argument ...</arg> + </cmdsynopsis> + <cmdsynopsis> + <command>parallel</command> + <arg>options</arg> + <command>--</command> + <arg>command ...</arg> </cmdsynopsis> </refsynopsisdiv> @@ -50,6 +56,9 @@ Written by Joey Hess repeated for each argument. Jobs may be run in parallel. The default is to run one job per CPU.</para> + <para>If no command is specified before the --, + the commands after it are instead run in parallel.</para> + </refsect1> <refsect1> @@ -122,6 +131,14 @@ Written by Joey Hess <para>This runs three ufraw processes at the same time until all of the NEF files have been processed. </para> + + <para> + <cmdsynopsis> + <command>parallel -j 3 -- ls df "echo hi"</command> + </cmdsynopsis> + </para> + + <para>This runs three independent commands in parallel.</para> </refsect1> |