J'ai essayé de faire fourche() et la tuyauterie en main et cela fonctionne parfaitement bien, mais quand j'essaye de l'implémenter dans une fonction pour une raison quelconque je ne reçois aucune sortie, ceci est mon code:Forking avec des tuyaux
void cmd(int **pipefd,int count,int type, int last);
int main(int argc, char *argv[]) {
int pipefd[3][2];
int i, total_cmds = 3,count = 0;
int in = 1;
for(i = 0; i < total_cmds;i++){
pipe(pipefd[count++]);
cmd(pipefd,count,i,0);
}
/*Last Command*/
cmd(pipefd,count,i,1);
exit(EXIT_SUCCESS);
}
void cmd(int **pipefd,int count,int type, int last){
int child_pid,i,i2;
if ((child_pid = fork()) == 0) {
if(count == 1){
dup2(pipefd[count-1][1],1); /*first command*/
}
else if(last!=1){
dup2(pipefd[count - 2][0],0); /*middle commands*/
dup2(pipefd[count - 1][1],1);
}
else if(last == 1){
dup2(pipefd[count - 1][0],0); /*last command*/
}
for(i = 0; i < count;i++){/*close pipes*/
for(i2 = 0; i2 < 2;i2++){
close(pipefd[i][i2]);
}}
if(type == 0){
execlp("ls","ls","-al",NULL);
}
else if(type == 1){
execlp("grep","grep",".bak",NULL);
}
else if(type==2){
execl("/usr/bin/wc","wc",NULL);
}
else if(type ==3){
execl("/usr/bin/wc","wc","-l",NULL);
}
perror("exec");
exit(EXIT_FAILURE);
}
else if (child_pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
}
J'ai vérifié les descripteurs de fichiers et il ouvre les bons, pas sûr de ce que le problème pourrait être ..
Edit: je résolu le problème, mais je vais avoir des processus enfants, de quelle manière serait soyez le meilleur pour attendre l'enfant, while (wait (NULL)! = - 1); mais qui pèse
Vous ne pouvez pas passer 'pipefd [3] [2]' (un tableau bidimensionnel conventionnel) comme 'int **' voir http://c-faq.com/aryptr/ary2dfunc3.html –