Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <errno.h>
- #include <wait.h>
- void ex4 (int tab[][2])
- {
- int status;
- pid_t pid;
- FILE *f;
- int stat;
- for( int i = 0; i < 4 ; i++ ) // pour créer 4 fils
- switch(pid=fork()) // avec fork
- {
- case -1 : // fork n'a pas marché
- perror("fork");
- exit(errno);
- case 0 : // Code du fils
- if( i > 1) //envoie les valeurs du tableau par la valeur de retour du processus
- exit( tab[1][i%2] );
- exit(tab[0][i%2]);
- break;
- default : // Code du père
- wait(&status); //attend un fils
- if ( WIFEXITED(status) )
- { //si un fils n'a pas d'erreur
- printf(" %d ", WEXITSTATUS(status)); // récupère la valeur de retour
- if( i == 1 )
- printf("\n"); //pour afficher les valeurs comme une matrice carrée
- }
- else
- {
- exit(errno);
- }
- }
- }
- int main(int argc, char **argv)
- {
- int tab[2][2] =
- { {1,2},
- {3,4}
- };
- ex4(tab);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement