From 04576dc2a3f761eb041b808b56f13a58052e7655 Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Sun, 24 May 2020 12:19:52 +0200 Subject: Moved back to 02_exercise --- 02_exercise/beispiele/fork_signal/fork.c | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 02_exercise/beispiele/fork_signal/fork.c (limited to '02_exercise/beispiele/fork_signal/fork.c') diff --git a/02_exercise/beispiele/fork_signal/fork.c b/02_exercise/beispiele/fork_signal/fork.c new file mode 100644 index 0000000..b144577 --- /dev/null +++ b/02_exercise/beispiele/fork_signal/fork.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +int main(int argc, const char* argv[]) { + pid_t pid; + int h; + int count; + + if (argc < 2) { + printf("Usage: ./fork process#\n"); + return 0; + } + + count = atoi(argv[1]) - 1; + printf("Process#: %d\n", count + 1); + printf("Parent started\n"); + + pid = 1; + for (h = 0; h < count; ++h) { + if ((pid = fork()) < 0) + printf("fork error\n"); + else if (pid == 0) { + printf("Child %d started\n", h); + sleep(1); + printf("Child %d finished\n", h); + exit(0); + } + } + + printf("Parent waiting\n"); + for (h = 0; h < count; ++h) { + wait(NULL); + } + printf("Parent finished\n"); + return EXIT_SUCCESS; +} -- cgit v1.2.3-54-g00ecf