Je crée le processus fils dans mon code. Quand j'appelle fork(), le processus fils doit commencer son exécution à partir de l'instruction suivante, mais dans mon code, le processus enfant exécute l'instruction avant l'appel fork.Le processus enfant exécute les instructions écrites avant l'appel fork
#include<stdio.h>
int main()
{
int pid;
FILE *fp;
fp = fopen("oh.txt","w");
fprintf(fp,"i am before fork\n");
pid = fork();
if(pid == 0)
{
fprintf(fp,"i am inside child block\n");
}
else{
fprintf(fp,"i inside parent block\n");
}
fprintf(fp,"i am inside the common block to both parent and child\n");
fclose(fp);
return 0;
}
C'est la sortie que je reçois
SORTIE:
i am before fork
i inside parent block
i am inside the common block to both parent and child
i am before fork
i am inside child block
i am inside the common block to both parent and child
La ligne « je suis avant fourchette » devrait être écrit une fois dans le fichier, mais il est écrit deux fois par enfant et parent. Pourquoi c'est ainsi?
Merci.
Ajoutez un 'fflush (fp)' avant de débuter. –