Piping and redirection implementation

To implement the facility of piping and redirection I used the two commands dup, dup2, and strtok for tokenizing the command.
But when I run the command
ls|more
it is not running fine as I have developed it using the dup2 command.
the more command needs the whole buffer at once.

Please help which system call should I use to implement and maintaining the output in a buffer of one command so that it can be used as a input to other command.

Thanks

You've been told to build pipes without using pipes? Is this homework?

It is probably homework, but I'm sure you are allowed to use pipes (as in pipe(2)). No one is going to ask for an implementation the pipe system call.

I am using the pipe command but I am unable to run it successfully
The command like
ls|more

Please help how to implement that piping.

pipe() is a C system call. Use it to program "ls|more"

Ah. Here are the steps.

  • Call pipe() to create two fd's for the pipe.
  • Call fork() to create a new process.
  • Parent code:
    [list]
  • Close the reading-end of the pipe, the parent doesn't need it.
    [/list]
  • Child code:
    [list]
  • Close the writing-end of the pipe, the child doesn't need it.
  • Duplicate the reading-end over standard input.
  • Close the original reading-end of the pipe, leaving only the duplicate.
  • Call exec() to replace this process with whatever program you want to pipe to. It will keep the file descriptors you've given it, in this case, the pipe.
    [/list]

You should now be able to write to the writing end of the pipe in the original process, and have the data be fed into the standard input of the other process.

Thanks, But I got stuck when the command length is more than 3 pipes. I made that program and is running fine on 3 pipes but more than that my program is not able to handle.

Instead of building all 3 at once, could you repeat the pipe-building process in each sub-process?