diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-13 12:16:35 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-13 12:16:35 +0200 |
commit | 00dff9b1d9c3a20ce4b091e092d38f913479a5ac (patch) | |
tree | 4bc5a1af07c4e96d4354ce13a6a7fc62633a6b12 /02_exercise/beispiele/fork_signal/fork.c | |
parent | a2ecab285195ff72fc2955b74f48751ab998809b (diff) | |
download | betriebssysteme-00dff9b1d9c3a20ce4b091e092d38f913479a5ac.tar.gz betriebssysteme-00dff9b1d9c3a20ce4b091e092d38f913479a5ac.zip |
Added all given material for exercise 2
Diffstat (limited to '02_exercise/beispiele/fork_signal/fork.c')
-rw-r--r-- | 02_exercise/beispiele/fork_signal/fork.c | 39 |
1 files changed, 39 insertions, 0 deletions
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 <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> + +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; +} |