1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(void){
int i,j ;
printf("Je suis le processus %d\n",getpid()) ;
printf("Je vais afficher 10 premiers nombres Pere \n");
FILE *po = fopen("nombre","r") ;
if (po==NULL){
perror("Impossible ") ;
return -1 ;
}
for(j=0;j<10;j++){
fscanf(po,"%d",&i) ;
printf("%d\n",i) ;
}
printf("La Creation du fils \n") ;
switch(fork()){
case -1:perror("impossible de creer un fils") ;
exit(-1) ;
case 0:
printf("Je suis le processus fils %d\n",getpid()) ;
printf("Je vais afficher 20 nombres premiers fils \n");
for(j=0;j<20;j++){
printf("Je suis le processus%d ",getpid());
fscanf(po,"%d",&i) ;
printf("%d\n",i);
}
fclose(po) ;
printf("Le fils mort \n") ;
exit(0);
default:
printf("Le pere termine la lecture\n") ;
while(!feof(po)){
printf("Je suis le processus%d ",getpid());
fscanf(po,"%d",&i) ;
printf("%d\n",i) ;
}
fclose(po) ;
}
return 0;
} |
Partager