(02) add chimera and thread + commentary
This commit is contained in:
parent
e0859e823b
commit
6013fdb234
1 changed files with 61 additions and 5 deletions
|
@ -1,12 +1,17 @@
|
||||||
#include <linux/sched.h>
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
const int STACK_SIZE = 4096;
|
const int STACK_SIZE = 4096;
|
||||||
|
|
||||||
|
/* volé depuis le template */
|
||||||
|
volatile char counter = 0;
|
||||||
|
|
||||||
int child()
|
int child()
|
||||||
{
|
{
|
||||||
printf("Coucou depuis l'enfant\n");
|
printf("Coucou depuis l'enfant\n");
|
||||||
|
@ -15,23 +20,74 @@ int child()
|
||||||
printf("thread id: %d\n", gettid());
|
printf("thread id: %d\n", gettid());
|
||||||
printf("user id: %d\n", getuid());
|
printf("user id: %d\n", getuid());
|
||||||
|
|
||||||
|
while (counter < 4)
|
||||||
|
{
|
||||||
|
printf("[Child] Counter: %d\n", counter);
|
||||||
|
counter += 1;
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
char *stack = (char *) malloc(STACK_SIZE);
|
char *stack = (char *) malloc(STACK_SIZE);
|
||||||
|
int c_pid;
|
||||||
|
|
||||||
printf("Coucou depuis le parent\n");
|
printf("Coucou depuis le parent\n");
|
||||||
printf("parent id: %d\n", getppid());
|
printf("parent id: %d\n", getppid());
|
||||||
printf("process id: %d\n", getpid());
|
printf("process id: %d\n", getpid());
|
||||||
printf("thread id: %d\n", gettid());
|
printf("thread id: %d\n", gettid());
|
||||||
printf("user id: %d\n", getuid());
|
printf("user id: %d\n\n", getuid());
|
||||||
|
|
||||||
/* bizarre la stack ? regarder dans le manuel pourquoi on fait ça, c'est
|
/*
|
||||||
* logique.
|
* On passe la dernière adresse de la stack, cf. le manuel.
|
||||||
*/
|
*/
|
||||||
int pid = clone(child, (stack + STACK_SIZE - 1), SIGCHLD);
|
c_pid = clone(child, (stack + STACK_SIZE - 1), flags, NULL);
|
||||||
|
|
||||||
|
while (counter < 4)
|
||||||
|
{
|
||||||
|
printf("[Parent] Counter: %d\n", counter);
|
||||||
|
counter += 1;
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
free(stack);
|
free(stack);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue