diff --git a/serie1/ex6/.gitignore b/serie1/ex6/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..37a89e94098c33ea9f8b72e0109f506a4eab9bef --- /dev/null +++ b/serie1/ex6/.gitignore @@ -0,0 +1,2 @@ +*.o +child_list diff --git a/serie1/ex6/Makefile b/serie1/ex6/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..5a28c6c1b676fe26e1f40267677152d9b32293e7 --- /dev/null +++ b/serie1/ex6/Makefile @@ -0,0 +1,23 @@ +CC := clang +CFLAGS := -g -pedantic -Wall -Wextra -std=c2x +LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm +TARGET := child_list + +all: $(TARGET) + +$(TARGET): child_list.o + @printf "=================== Building executable ===================\n" + $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) + @printf "\n" + +%.o: %.c + @printf "================== Building object files ==================\n" + $(CC) $(CFLAGS) -c $< + @printf "\n" + +.PHONY: clean + +clean: + rm -f *.o $(TARGET) + + diff --git a/serie1/ex6/child_list.c b/serie1/ex6/child_list.c new file mode 100644 index 0000000000000000000000000000000000000000..374753f41505b2b149ce46dabf2754e7f9194000 --- /dev/null +++ b/serie1/ex6/child_list.c @@ -0,0 +1,23 @@ +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#define NB_PROCESSES 10 + +int main(void) { + pid_t pid = -1; + pid = fork(); + if (pid > 0) { + for (int i = 0; i < NB_PROCESSES; i++) { + fork(); + } + } else if (pid == 0) { + fprintf(stdout, "Child (%d) \t Parent(%d)\n", getpid(), getppid()); + exit(EXIT_SUCCESS); + } else { + perror("fork"); + } + return EXIT_SUCCESS; +}