From 23df85221e08fe5dc1f0eb6df3c48f299f243895 Mon Sep 17 00:00:00 2001 From: scratko Date: Fri, 24 May 2024 01:37:07 +0300 Subject: Initial commit --- parallel_progs_by_args.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 parallel_progs_by_args.c diff --git a/parallel_progs_by_args.c b/parallel_progs_by_args.c new file mode 100644 index 0000000..97505ab --- /dev/null +++ b/parallel_progs_by_args.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +struct item { + char *prog_name; + int pid; + struct item *next; +}; + +static int check_delimiter(char *arg) +{ + return !strncmp(arg, ";;", 3); +} + +static void fill_in_item(char *arg, int pid, struct item **list) +{ + struct item *tmp_item = malloc(sizeof(struct item)); + tmp_item->pid = pid; + tmp_item->prog_name = arg; + tmp_item->next = *list; + *list = tmp_item; +} + +static void make_new_fork(char **begin_pos, struct item **list, + int *process_counter) +{ + int pid = fork(); + if(pid == -1) { + perror("fork error"); + exit(1); + } + if(pid) { + fill_in_item(*begin_pos, pid, list); + ++*process_counter; + } + else { + execvp(*begin_pos, begin_pos); + perror(*begin_pos); + exit(1); + } +} + +static char* find_name_by_pid(int pid, struct item *list) +{ + while(list) { + if(list->pid == pid) + return list->prog_name; + list = list->next; + } + return NULL; +} + +static void clear_list(struct item *list) +{ + struct item *tmp; + while(list) { + tmp = list; + list = list->next; + free(tmp); + } +} + +int main(int argc, char **argv) +{ + int status, pid, process_counter; + struct item *list = NULL; + process_counter = 0; + char **begin_pos, **end_pos; + if(argc == 1) { + fprintf(stderr, "too few arguments\n"); + return 1; + } + ++argv; + for(begin_pos = argv, end_pos = begin_pos + 1; *end_pos != NULL; ) { + if(check_delimiter(*end_pos)) { + *end_pos = NULL; + make_new_fork(begin_pos, &list, &process_counter); + begin_pos = end_pos + 1; + end_pos = begin_pos + 1; + } else + ++end_pos; + } + make_new_fork(begin_pos, &list, &process_counter); + for( ; process_counter; --process_counter) { + pid = wait(&status); + if(WIFEXITED(status) && WEXITSTATUS(status) == 0) + printf("%s\n", find_name_by_pid(pid, list)); + } + clear_list(list); + return 0; +} -- cgit v1.2.3