From e0b2bcda976eb43b3976b750ce4261db6d608af8 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sat, 31 Oct 2009 10:20:08 -0400 Subject: [PATCH] parallel: Allow running independent commands, like `parallel -j3 -- ls df "echo hi"` --- debian/changelog | 2 ++ parallel.c | 44 +++++++++++++++++++++++++++++--------------- 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 Mon, 05 Oct 2009 13:33:07 -0400 diff --git a/parallel.c b/parallel.c index 99a9727..2358470 100644 --- a/parallel.c +++ b/parallel.c @@ -33,8 +33,9 @@ #include 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 parallel options command - -- - arguments + -- + argument ... + + + parallel + options + -- + command ... @@ -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. + If no command is specified before the --, + the commands after it are instead run in parallel. + @@ -122,6 +131,14 @@ Written by Joey Hess This runs three ufraw processes at the same time until all of the NEF files have been processed. + + + + parallel -j 3 -- ls df "echo hi" + + + + This runs three independent commands in parallel. -- 2.39.5