diff --git a/serie1/ex1.md b/processus/serie1/ex1.md similarity index 100% rename from serie1/ex1.md rename to processus/serie1/ex1.md diff --git a/serie1/ex2/.gitignore b/processus/serie1/ex2/.gitignore similarity index 100% rename from serie1/ex2/.gitignore rename to processus/serie1/ex2/.gitignore diff --git a/serie1/ex2/Makefile b/processus/serie1/ex2/Makefile similarity index 100% rename from serie1/ex2/Makefile rename to processus/serie1/ex2/Makefile diff --git a/serie1/ex2/ex2.md b/processus/serie1/ex2/ex2.md similarity index 100% rename from serie1/ex2/ex2.md rename to processus/serie1/ex2/ex2.md diff --git a/serie1/ex2/prog.c b/processus/serie1/ex2/prog.c similarity index 100% rename from serie1/ex2/prog.c rename to processus/serie1/ex2/prog.c diff --git a/serie1/ex3/.gitignore b/processus/serie1/ex3/.gitignore similarity index 100% rename from serie1/ex3/.gitignore rename to processus/serie1/ex3/.gitignore diff --git a/serie1/ex3/orphan/.gitignore b/processus/serie1/ex3/orphan/.gitignore similarity index 100% rename from serie1/ex3/orphan/.gitignore rename to processus/serie1/ex3/orphan/.gitignore diff --git a/serie1/ex3/orphan/Makefile b/processus/serie1/ex3/orphan/Makefile similarity index 100% rename from serie1/ex3/orphan/Makefile rename to processus/serie1/ex3/orphan/Makefile diff --git a/serie1/ex3/orphan/orphan.c b/processus/serie1/ex3/orphan/orphan.c similarity index 100% rename from serie1/ex3/orphan/orphan.c rename to processus/serie1/ex3/orphan/orphan.c diff --git a/serie1/ex3/zombie/.gitignore b/processus/serie1/ex3/zombie/.gitignore similarity index 100% rename from serie1/ex3/zombie/.gitignore rename to processus/serie1/ex3/zombie/.gitignore diff --git a/serie1/ex3/zombie/Makefile b/processus/serie1/ex3/zombie/Makefile similarity index 100% rename from serie1/ex3/zombie/Makefile rename to processus/serie1/ex3/zombie/Makefile diff --git a/serie1/ex3/zombie/figs/zombie.png b/processus/serie1/ex3/zombie/figs/zombie.png similarity index 100% rename from serie1/ex3/zombie/figs/zombie.png rename to processus/serie1/ex3/zombie/figs/zombie.png diff --git a/serie1/ex3/zombie/zombie.c b/processus/serie1/ex3/zombie/zombie.c similarity index 100% rename from serie1/ex3/zombie/zombie.c rename to processus/serie1/ex3/zombie/zombie.c diff --git a/serie1/ex3/zombie/zombie.md b/processus/serie1/ex3/zombie/zombie.md similarity index 100% rename from serie1/ex3/zombie/zombie.md rename to processus/serie1/ex3/zombie/zombie.md diff --git a/serie1/ex4/.gitignore b/processus/serie1/ex4/.gitignore similarity index 100% rename from serie1/ex4/.gitignore rename to processus/serie1/ex4/.gitignore diff --git a/serie1/ex4/Makefile b/processus/serie1/ex4/Makefile similarity index 100% rename from serie1/ex4/Makefile rename to processus/serie1/ex4/Makefile diff --git a/serie1/ex4/multi_process.c b/processus/serie1/ex4/multi_process.c similarity index 100% rename from serie1/ex4/multi_process.c rename to processus/serie1/ex4/multi_process.c diff --git a/serie1/ex5/.gitignore b/processus/serie1/ex5/.gitignore similarity index 100% rename from serie1/ex5/.gitignore rename to processus/serie1/ex5/.gitignore diff --git a/serie1/ex5/Makefile b/processus/serie1/ex5/Makefile similarity index 100% rename from serie1/ex5/Makefile rename to processus/serie1/ex5/Makefile diff --git a/serie1/ex5/multi_process.c b/processus/serie1/ex5/multi_process.c similarity index 100% rename from serie1/ex5/multi_process.c rename to processus/serie1/ex5/multi_process.c diff --git a/serie1/ex6/.gitignore b/processus/serie1/ex6/.gitignore similarity index 100% rename from serie1/ex6/.gitignore rename to processus/serie1/ex6/.gitignore diff --git a/serie1/ex6/Makefile b/processus/serie1/ex6/Makefile similarity index 100% rename from serie1/ex6/Makefile rename to processus/serie1/ex6/Makefile diff --git a/processus/serie1/ex6/child_list.c b/processus/serie1/ex6/child_list.c new file mode 100644 index 0000000000000000000000000000000000000000..bfd150cfb58202fe8311ec587d6484a40290502b --- /dev/null +++ b/processus/serie1/ex6/child_list.c @@ -0,0 +1,27 @@ +#include <signal.h> +#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) { + for (int i = 0; i < NB_PROCESSES - 1; i++) { + pid_t pid = fork(); + if (pid == 0) { + if (i == NB_PROCESSES - 2) { + fprintf(stdout, "last child (%d) waiting ...\n", getpid()); + break; + } + fprintf(stdout, "Child (%d)\tParent (%d)\n", getpid(), getppid()); + } else if (pid > 0) { + wait(NULL); + break; + } else { + perror("fork"); + } + } + return EXIT_SUCCESS; +} diff --git a/processus/serie1/ex7/.gitignore b/processus/serie1/ex7/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/processus/serie1/ex7/Makefile b/processus/serie1/ex7/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..1467e1a1a095bc85f9910ceaaa1f78fb23b29a58 --- /dev/null +++ b/processus/serie1/ex7/Makefile @@ -0,0 +1,23 @@ +CC := clang +CFLAGS := -g -pedantic -Wall -Wextra -std=c2x +LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm +TARGET := child_tree + +all: $(TARGET) + +$(TARGET): child_tree.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/processus/serie1/ex7/child_tree b/processus/serie1/ex7/child_tree new file mode 100755 index 0000000000000000000000000000000000000000..553223ee4d7d6fde276201ee717b5244f582dda1 Binary files /dev/null and b/processus/serie1/ex7/child_tree differ diff --git a/processus/serie1/ex7/child_tree.c b/processus/serie1/ex7/child_tree.c new file mode 100644 index 0000000000000000000000000000000000000000..c0aba57a928e1ddb8b0cf7da56a5abe159114143 --- /dev/null +++ b/processus/serie1/ex7/child_tree.c @@ -0,0 +1,31 @@ +#include <signal.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <time.h> +#include <unistd.h> +#define NB_PROCESSES 10 + +int main(void) { + srand(time(NULL)); + fprintf(stdout, "parent (%d)\n", getpid()); + for (int i = 0; i < NB_PROCESSES - 1; i++) { + pid_t pid = fork(); + if (pid == 0) { + fprintf(stdout, "Child (%d)\tParent (%d)\n", getpid(), getppid()); + exit(rand() % 9 + 1); + } else if (pid > 0) { + pid_t wstatus; + if (wait(&wstatus) == -1) { + fprintf(stdout, "child %d completed\n", i); + } + } else { + perror("fork"); + } + } + fprintf(stdout, "parent waiting on its children...\n"); + + return EXIT_SUCCESS; +} diff --git a/processus/serie1/ex7/child_tree.o b/processus/serie1/ex7/child_tree.o new file mode 100644 index 0000000000000000000000000000000000000000..558a34e7dbda9da00998390bab96f57370fe6076 Binary files /dev/null and b/processus/serie1/ex7/child_tree.o differ diff --git a/processus/serie2/ex1.md b/processus/serie2/ex1.md new file mode 100644 index 0000000000000000000000000000000000000000..eca55c5db9c3381b2d151e33e7ac66b79f076e90 --- /dev/null +++ b/processus/serie2/ex1.md @@ -0,0 +1,37 @@ +# Exercice 1 + +## Code + +```C +#include <unistd.h> + +int main() { + for (;;) { + fork(); + } +} +``` + +## Que va-t-il se produire si vous exécutez ce programme ? + +La table des processus sera très rapidement remplie, par conséquent le PC +ralentira jusqu'à ne plus réagir aux entrées de l'utilisateur. Il faudra +redémarrer la machine. + +## Comment pouvez-vous configurer votre système afin d'empêcher qu'un programme de ce genre mette l'OS à genoux ? + +Afin d'y remédier, il est possible de modifier la valeur maximale défini par l'OS +pour le nombre de processus actifs. Afin d'obtenir la value actuelle, il faut +exécuter la commande suivante : + +```bash +ulimit -u +``` + +Si l'on spécifie un entier après le flag `-u` on limitera le nombre de processus +actifs en même temps à ce nombre (ex.: on limite à 30 processus) : + + +```bash +ulimit -u 30 +``` diff --git a/processus/serie2/ex2/.gitignore b/processus/serie2/ex2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b6de771d2fff3a218dbc1b3de5b6b6cc1dc54e08 --- /dev/null +++ b/processus/serie2/ex2/.gitignore @@ -0,0 +1,2 @@ +*.o +prog diff --git a/processus/serie2/ex2/Makefile b/processus/serie2/ex2/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..df35d6384b9e9f177057b82986b6a1858455e9da --- /dev/null +++ b/processus/serie2/ex2/Makefile @@ -0,0 +1,24 @@ +CC := clang +CFLAGS := -g -pedantic -Wall -Wextra -std=c11 +LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm -lpthread +TARGET := prog + +all: $(TARGET) + +$(TARGET): prog.o + @printf "=================== Building executable ===================\n" + $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) + +%.o: %.c + @printf "=================== Building sources ===================\n" + $(CC) $(CFLAGS) -c $< + +.PHONY: clean + +clean: + @printf "=================== Cleaning up... ===================\n" + rm -f *.o $(TARGET) + +.PHONY: rebuild + +rebuild: clean all diff --git a/processus/serie2/ex2/prog.c b/processus/serie2/ex2/prog.c new file mode 100644 index 0000000000000000000000000000000000000000..ecb0e65222dbff3ae2c5a78223fc54a3fed0b309 --- /dev/null +++ b/processus/serie2/ex2/prog.c @@ -0,0 +1,48 @@ +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +int main(void) { + int status; + pid_t pid = -1; + char *prog_path = "/usr/bin/uname"; + + /** + * char *argv[]: + * argv[0] => always the executable! + * in-between => args to the executable + * last value => alwas NULL! + */ + char *argv[] = {prog_path, "-a", NULL}; + + pid = fork(); + if (pid == 0) { + fprintf(stdout, "I'm the child (%d)\n", getpid()); + + int exit_code = execve(prog_path, argv, NULL); + + if (exit_code == -1) { + perror("execve"); + } + + exit(exit_code); + } else if (pid > 0) { + pid = wait(&status); + + if (pid == -1) { + perror("wait"); + } + + if (WIFEXITED(status)) { + fprintf(stdout, "%s completed with exit code %d\n", + basename(prog_path), WEXITSTATUS(status)); + } + } else { + perror("fork"); + } + + return EXIT_SUCCESS; +} diff --git a/serie1/ex6/child_list.c b/serie1/ex6/child_list.c deleted file mode 100644 index 374753f41505b2b149ce46dabf2754e7f9194000..0000000000000000000000000000000000000000 --- a/serie1/ex6/child_list.c +++ /dev/null @@ -1,23 +0,0 @@ -#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; -} diff --git a/serie4/ex1/Makefile b/serie4/ex1/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..1bf17d7df759b5b1cd51158be1d5cdcae99ce471 --- /dev/null +++ b/serie4/ex1/Makefile @@ -0,0 +1,21 @@ +CC := clang +CFLAGS := -g -pedantic -Wall -Wextra +LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm +TARGET := prog + +all: $(TARGET) + +$(TARGET): prog.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/serie4/ex1/prog b/serie4/ex1/prog new file mode 100755 index 0000000000000000000000000000000000000000..765290600b0aede63ea3c1ae69e8beeb0c0accdc Binary files /dev/null and b/serie4/ex1/prog differ diff --git a/serie4/ex1/prog.c b/serie4/ex1/prog.c new file mode 100644 index 0000000000000000000000000000000000000000..32ceaf96ec6130320eeb9178689ba50e6384ef15 --- /dev/null +++ b/serie4/ex1/prog.c @@ -0,0 +1,29 @@ +// #define _GNU_SOURCE_ +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +static void handler(int signum) { + fprintf(stdout, "Signal %d ignored\n", signum); +} + +int main(void) { + struct sigaction act; + act.sa_handler = handler; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + + if (sigaction(69420, &act, NULL) == -1) { + perror(" sigaction "); + exit(EXIT_FAILURE); + } + + while (true) { + fprintf(stdout, "hello world\n"); + sleep(1); + } + + return EXIT_SUCCESS; +} diff --git a/serie4/ex1/prog.o b/serie4/ex1/prog.o new file mode 100644 index 0000000000000000000000000000000000000000..30cb93e03c40950e54ab45d0b935fb9a53195f0e Binary files /dev/null and b/serie4/ex1/prog.o differ