(02) add Makefile and fix code convention

This commit is contained in:
rick 2022-12-08 18:46:55 +01:00
parent 6013fdb234
commit 1da4836936
Signed by: Rick
GPG Key ID: 4A6223D66294EB20
2 changed files with 17 additions and 5 deletions

9
02-fork-chimera/Makefile Normal file
View File

@ -0,0 +1,9 @@
CC = gcc
CFLAGS = -ansi -pedantic -pedantic-errors -Wall -Werror -Wextra
SRC = main.c
all:
$(CC) $(CFLAGS) $(SRC) -o clone
clean:
rm clone

View File

@ -41,15 +41,15 @@ void help(char *name)
int main(int argc, char *argv[])
{
int flags, c_pid;
char *mode, *stack;
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
@ -58,6 +58,8 @@ int main(int argc, char *argv[])
* Pas besoin de notifier le père pour la chimère, on met directement le
* flag pour partager la zone mémoire.
*/
mode = argv[1];
if (!strcmp(mode, "fork")) flags = SIGCHLD;
else if (!strcmp(mode, "chimera")) flags = CLONE_VM;
else if (!strcmp(mode, "thread")) flags = CLONE_VM | CLONE_THREAD
@ -68,8 +70,7 @@ int main(int argc, char *argv[])
return 0;
}
char *stack = (char *) malloc(STACK_SIZE);
int c_pid;
stack = (char *) malloc(STACK_SIZE);
printf("Coucou depuis le parent\n");
printf("parent id: %d\n", getppid());
@ -82,6 +83,8 @@ int main(int argc, char *argv[])
*/
c_pid = clone(child, (stack + STACK_SIZE - 1), flags, NULL);
printf("[Parent] PID enfant: %d\n", c_pid);
while (counter < 4)
{
printf("[Parent] Counter: %d\n", counter);