(02) basic fork

This commit is contained in:
rick 2022-12-08 17:31:20 +01:00
parent 146746ddcf
commit e0859e823b
Signed by: Rick
GPG key ID: 4A6223D66294EB20

38
02-fork-chimera/main.c Normal file
View file

@ -0,0 +1,38 @@
#include <linux/sched.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int STACK_SIZE = 4096;
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());
return 0;
}
int main(int argc, char *argv[])
{
char *stack = (char *) malloc(STACK_SIZE);
printf("Coucou depuis le parent\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());
/* bizarre la stack ? regarder dans le manuel pourquoi on fait ça, c'est
* logique.
*/
int pid = clone(child, (stack + STACK_SIZE - 1), SIGCHLD);
free(stack);
return 0;
}