Last active
November 7, 2017 21:45
-
-
Save Triskae/532bd524fdf26eb12feafcc2d45282e0 to your computer and use it in GitHub Desktop.
p_pip_arg corrigé
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************/ | |
/* */ | |
/* p_pipe_arg : - Demande de descripteurs de pipe-line, */ | |
/* - Demande de fork, */ | |
/* - PERE : recupere le deuxieme parametre qui*/ | |
/* correspond a la commande qui doit */ | |
/* s'executer depuis le pipe-line */ | |
/* - FILS : recupere le premier parametre qui */ | |
/* correspond a la commande qui doit */ | |
/* s'executer vers le pipe-line. */ | |
/* EXEMPLE : p_pipe_arg who wc <====> who | wc */ | |
/************************************************************/ | |
/* - Version 1 : Cf. Ci-dessus */ | |
/* - Version 2 : Le comportement du pere (ci-dessus) est */ | |
/* est fait dans un deuxieme fils. */ | |
/* */ | |
/* Ne pas utiliser d'initialisation dynamique de tableau */ | |
/************************************************************/ | |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> | |
#define READ 0# define WRITE 1# define ENTREE 0# define SORTIE 1 | |
int | |
main(int argc, char * argv[]) { | |
int res; | |
int tab[2]; | |
if (argc != 3) { | |
fprintf(stderr, "SYNTAXE : %s commande1 commande2\n", argv[0]); | |
} else { | |
/**************** PERE ******************/ | |
/* PARTIE A COMPLETER */ | |
/****************************************/ | |
res = pipe(tab); | |
if (res < 0) | |
printf("Erreur dans le pipe"); | |
res = fork(); | |
if (res < 0) { | |
perror("ERREUR FORK"); | |
exit(2); | |
} else { | |
if (res == 0) | |
{ | |
/**************** FILS ******************/ | |
/* PARTIE A COMPLETER */ | |
/****************************************/ | |
close(1); | |
res = dup(tab[1]); | |
close(tab[0]); | |
if (res < 0) | |
perror("Erreur dans le dup"); | |
else | |
execlp(argv[1], argv[1], NULL); | |
perror("probleme execlp"); | |
} else { | |
/*************** FILS 2 ******************/ | |
/* PARTIE A COMPLETER */ | |
/****************************************/ | |
res = fork(); | |
if (res == 0) { | |
close(0); | |
dup(tab[0]); | |
close(tab[1]); | |
if (res < 0) | |
perror("Erreur dans le dup"); | |
else | |
execlp(argv[2], argv[2], NULL); | |
perror("execlp probleme"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment