CanCeylan
CanCeylan

Reputation: 3010

Writing own shell in C - How to make more than two pipes?

I'm writing my own shell, and I had a problem about pipelining the processes.

I'm doing correctly to work on two processes such as:

ls -l | sort -r

But when I have three commands I do not know what to do.

I have three methods:

run_first:

  dup2(pfd[1], 1);
  close(pfd[0]);
  execvp(cmd[0], cmd);

run_mid:

  dup2(pfd[1], 1);
  dup2(pfd[0], 1);
  execvp(cmd[0], cmd);

and run_last:

  dup2(pfd[0], 0);
  close(pfd[1]);
  execvp(cmd[0], cmd);
  perror(command_list[i][0]);

These are basicly the important parts, I thing I couldnt implement my logic in these three methods,

I want the output of run_first should be the input of run_mid and I want to pass output of run_mid to the run_last.

Upvotes: 0

Views: 765

Answers (1)

Neil
Neil

Reputation: 55402

When you have three commands you will need two pipes, one to join the first command to the second and one to join the second command to the third. (After all, the command line looks like first | mid | last.)

Upvotes: 1

Related Questions