Skip to content

Instantly share code, notes, and snippets.

@marcoarment
Last active August 12, 2024 08:09
Show Gist options
  • Save marcoarment/5509762 to your computer and use it in GitHub Desktop.
Save marcoarment/5509762 to your computer and use it in GitHub Desktop.
A simple shell command parallelizer.
/* parallelize: reads commands from stdin and executes them in parallel.
The sole argument is the number of simultaneous processes (optional) to
run. If omitted, the number of logical CPUs available will be used.
Build: gcc -pthread parallelize.c -o parallelize
Demo: (for i in {1..10}; do echo "echo $i ; sleep 5" ; done ) | ./parallelize
By Marco Arment, released into the public domain with no guarantees.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
pthread_mutex_t g_input_mutex;
void *thread_main(void *_)
{
while (1) {
while (pthread_mutex_lock(&g_input_mutex)) { }
size_t length = 0;
char *line = NULL;
int retval = getline(&line, &length, stdin);
pthread_mutex_unlock(&g_input_mutex);
if (retval == -1) break;
pid_t child_pid = fork();
if (child_pid) {
waitpid(child_pid, NULL, 0);
} else {
system(line);
exit(0);
}
free(line);
}
pthread_exit(NULL);
}
int main(int argc, const char **argv)
{
int thread_count = argc < 2 ? sysconf(_SC_NPROCESSORS_ONLN) : atoi(argv[1]);
if (thread_count < 1) {
fprintf(stderr, "Usage: parallelize {thread_count}\n");
exit(1);
}
pthread_mutex_init(&g_input_mutex, NULL);
pthread_t *threads = malloc(thread_count * sizeof(pthread_t));
for (int i = 0; i < thread_count; i++) {
if (pthread_create(&threads[i], NULL, thread_main, NULL)) {
fprintf(stderr, "parallelize: cannot create worker thread %d\n", i);
exit(-1);
}
}
pthread_exit(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment