#define READ 0
#define WRITE 1
int main(int argc, char *args[]) {
int i,j,k;
int pid;
int valor;
int child_pid;
int status;
int **arrayPipe;
if (argc == 2) {
execlp(args[1],args[1],NULL);
} else {
//Crear pipes
arrayPipe
= (int **) malloc((argc
-2) * sizeof(int *)); for(i=0;i<argc-2;i++){
arrayPipe
[i
] = (int *) malloc(2 * sizeof(int)); valor=pipe(arrayPipe[i]);
if (valor==-1){
perror("Error al crear el pipen"); return -1;
}
}
//Crear procesos
for (i=0;i<argc-1;i++){
pid = fork();
if (pid == -1){
perror("Error al crear el hijon"); return -1;
} else if (pid == 0){ /* Child */
if(i==0){ //Si es el primer hijo
printf("SOY EL PRIMER HIJOn"); //Cierro todos los descriptes excepto los suyos
for (k=0;k<argc-2;k++){
if (k!=i){
close(arrayPipe[k][READ]);
close(arrayPipe[k][WRITE]);
}
}
printf("Voy a ejecutar: %sn",args
[i
+1]); close(arrayPipe[k][READ]); //ESTA Y LAS DOS SIGUIENTES ESTÁN MAL, EL INDICE ES i Y NO k
dup2(arrayPipe[k][WRITE],1);
close(arrayPipe[k][WRITE]);
execlp(args[i+1],args[i+1],NULL); //Esta es la instrucción que me falla
} else if (i==argc-2){ //Si es el último
printf("SOY EL ULTIMO HIJOn"); //Cierro todos los descriptes excepto los suyos
for (k=0;k<argc-2;k++){
if (k!=i-1){
close(arrayPipe[k][READ]);
close(arrayPipe[k][WRITE]);
}
}
//printf("Voy a ejecutar: %sn",args[i+1]);
close(arrayPipe[i-1][WRITE]);
dup2(arrayPipe[i-1][READ],0);
close(arrayPipe[i-1][READ]);
printf("Voy a ejecutar: %sn",args
[i
+1]); execlp(args[i+1],args[i+1],NULL);
} else { //Si es intermedio
//Aquí iria el código en el caso de ser más de dos hijos
}
} else {/* Parent */
//Cierro descriptores en el padre
if (i==0){
for (j=0;j<argc-2;j++){
close(arrayPipe[j][READ]);
close(arrayPipe[j][WRITE]);
}
}
child_pid = wait(&status);
if (WIFEXITED(status) == 0){
printf(": Ejecución incorrecta 1n"); } else {
if (WEXITSTATUS(status)){
printf(": Ejecución incorrecta 2n"); } else {
printf(": Ejecución correctan"); }
}
}
}
//Liberar memoria
for(i=0;i<argc-2;i++){
}
}
return 0;
}