2022-12-08 18:42:00 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2022-12-08 17:31:20 +01:00
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-12-08 18:42:00 +01:00
|
|
|
#include <unistd.h>
|
2022-12-08 17:31:20 +01:00
|
|
|
|
|
|
|
const int STACK_SIZE = 4096;
|
|
|
|
|
2022-12-08 18:42:00 +01:00
|
|
|
/* volé depuis le template */
|
|
|
|
volatile char counter = 0;
|
|
|
|
|
2022-12-08 17:31:20 +01:00
|
|
|
int child()
|
|
|
|
{
|
|
|
|
printf("Coucou depuis l'enfant\n");
|
|
|
|
printf("parent id: %d\n", getppid());
|
|
|
|
printf("process id: %d\n", getpid());
|
|
|
|
printf("thread id: %d\n", gettid());
|
|
|
|
printf("user id: %d\n", getuid());
|
|
|
|
|
2022-12-08 18:42:00 +01:00
|
|
|
while (counter < 4)
|
|
|
|
{
|
|
|
|
printf("[Child] Counter: %d\n", counter);
|
|
|
|
counter += 1;
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:31:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:42:00 +01:00
|
|
|
void help(char *name)
|
|
|
|
{
|
|
|
|
printf("%s - Émule différent types de fork()\n", name);
|
|
|
|
printf("Usage: %s <mode>\n\n", name);
|
|
|
|
printf(" fork Fork basique.\n");
|
|
|
|
printf(" chimera Créer un processus enfant partageant le même espace mémoire.\n");
|
|
|
|
printf(" thread Créé un thread.\n");
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:31:20 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2022-12-08 18:42:00 +01:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
help(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *mode = argv[1];
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Je mets SIGCHLD quand on fait un fork basique, histoire d'avoir au moins
|
|
|
|
* un flag. En théorie, il n'y en a pas besoin vu qu'on ne souhaite pas
|
|
|
|
* gérer la mort de l'enfant.
|
|
|
|
*
|
|
|
|
* Pas besoin de notifier le père pour la chimère, on met directement le
|
|
|
|
* flag pour partager la zone mémoire.
|
|
|
|
*/
|
|
|
|
if (!strcmp(mode, "fork")) flags = SIGCHLD;
|
|
|
|
else if (!strcmp(mode, "chimera")) flags = CLONE_VM;
|
|
|
|
else if (!strcmp(mode, "thread")) flags = CLONE_VM | CLONE_THREAD
|
|
|
|
| CLONE_SIGHAND;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
help(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:31:20 +01:00
|
|
|
char *stack = (char *) malloc(STACK_SIZE);
|
2022-12-08 18:42:00 +01:00
|
|
|
int c_pid;
|
2022-12-08 17:31:20 +01:00
|
|
|
|
|
|
|
printf("Coucou depuis le parent\n");
|
|
|
|
printf("parent id: %d\n", getppid());
|
|
|
|
printf("process id: %d\n", getpid());
|
|
|
|
printf("thread id: %d\n", gettid());
|
2022-12-08 18:42:00 +01:00
|
|
|
printf("user id: %d\n\n", getuid());
|
2022-12-08 17:31:20 +01:00
|
|
|
|
2022-12-08 18:42:00 +01:00
|
|
|
/*
|
|
|
|
* On passe la dernière adresse de la stack, cf. le manuel.
|
2022-12-08 17:31:20 +01:00
|
|
|
*/
|
2022-12-08 18:42:00 +01:00
|
|
|
c_pid = clone(child, (stack + STACK_SIZE - 1), flags, NULL);
|
|
|
|
|
|
|
|
while (counter < 4)
|
|
|
|
{
|
|
|
|
printf("[Parent] Counter: %d\n", counter);
|
|
|
|
counter += 1;
|
|
|
|
sleep(1);
|
|
|
|
}
|
2022-12-08 17:31:20 +01:00
|
|
|
|
|
|
|
free(stack);
|
|
|
|
return 0;
|
|
|
|
}
|