Я реализую многотрубку для своей оболочки. Проблема с кодом заключается в том, что он не выводит выходные данные моей последней команды в трубе на мой STDOUT. Может кто-нибудь помочь? Функция executePipedCommands принимает указатель на головку списка командмульти-трубопровод осуществление. не работает
Я вставляю, например, ls | more | grep s в список команд.
struct cmd_t {
int nargs, maxargs;
char **args;
struct cmd_t *next;
};
typedef struct cmd_t *Cmd;
void executePipedCommands(Cmd command) {
int numPipes = -1;
Cmd temp = command;
int status;
int i = 0;
pid_t pid;
while(command!= NULL)
{
numPipes ++;
command = command->next;
}
printf("number of pipes : %d",numPipes);
int pipefds[2*numPipes];
for(i = 0; i < (numPipes); i++){
if(pipe(pipefds + i*2) < 0) {
perror("couldn't pipe");
exit(EXIT_FAILURE);
}
}
int j = 0;
while(command)
{
pid = fork();
if(pid == 0)
{
//if not last command
if(command->next)
{
if(dup2(pipefds[j + 1], 1) < 0)
{
perror("dup2");
exit(EXIT_FAILURE);
}
}
//if not first command&& j!= 2*numPipes
if(j != 0)
{
if(dup2(pipefds[j-2], 0) < 0)
{
perror(" dup2");///j-2 0 j+1 1
exit(EXIT_FAILURE);
}
}
for(i = 0; i < 2*numPipes; i++)
{
close(pipefds[i]);
}
if(execvp(*command->args, command->args) < 0)
{
perror(*command->args);
exit(EXIT_FAILURE);
}
}
else if(pid < 0)
{
perror("error");
exit(EXIT_FAILURE);
}
command = command->next;
j+=2;
}
/**Parent closes the pipes and wait for children*/
for(i = 0; i < 2 * numPipes; i++){
close(pipefds[i]);
}
for(i = 0; i < numPipes + 1; i++)
wait(&status);
}