// Modified from https://lwn.net/Articles/533492/ // Copyright 2013, Michael Kerrisk // Licensed under GNU General Public License v2 or later #define _GNU_SOURCE #include #include #include #include #include #include #include // A simple error-handling function: print an error message based // on the value in 'errno' and terminate the calling process #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0) // Function de départ pour le processus fils cloné static int childFunc(void *arg) { char **argv = arg; // TODO : Mise en place pour le processus fils execvp(argv[0], &argv[0]); errExit("execvp"); } // Espace mémoire réservé pour la pile du processus fils #define STACK_SIZE (1024 * 1024) static char child_stack[STACK_SIZE]; int main(int argc, char *argv[]) { int flags = 0; pid_t child_pid; // TODO : Mise en place du flag pour le PID namespace child_pid = clone(childFunc, child_stack + STACK_SIZE, flags | SIGCHLD, &argv[1]); if (child_pid == -1) errExit("clone"); printf("%s: PID of child created by clone() is %ld\n", argv[0], (long) child_pid); // Le processus parent continue ici // Attent que le processus fils se termine if (waitpid(child_pid, NULL, 0) == -1) errExit("waitpid"); printf("%s: terminating\n", argv[0]); exit(EXIT_SUCCESS); }