0

I am running commands in my microshell. The single commands seem to be working fine , but when giving a pipe commands like ls || sort, it doesn't give output immediately. When I exit the program the output of all the pipe commands is printed. I am not sure how to rectify this. Below is my code for pipe commands.

pid = fork(); /* Creating  child   */

if(pid==0) /*Inside child  */
{
    dup2(fd[1],STDOUT_FILENO); /* Redirecting the stdout to the pipe  using the  dup2 */
    close(fd[0]); /*Closing the read descriptor of pipe */
    execvp(commandExecute1[0],commandExecute1); /* Execute the first command */
}
else
{  
    /*Inside  Parent */ 
    if((pid=fork())==0) /*second child */
    {
        dup2(fd[0],STDIN_FILENO); /* Redirecting standard input to the read descriptor of pipe */
        dup2(STDOUT_FILENO,fd[1]);  /* Redirecting the pipe descriptor o/p to STDOUT */
        close(fd[1]);  /*Close write file descriptor of pipe */
        execvp(commandExecute2[0],commandExecute2); /* Running the second command*/
    }
}  

Did I miss anything?

wallyk
  • 56,922
  • 16
  • 83
  • 148
Sumanth
  • 363
  • 3
  • 17
  • can anyone help me on this. – Sumanth Oct 10 '16 at 21:07
  • On a single child you are piping from child to parent `child | parent`. On double child, you should be piping from child to child and from child to parent `child | child | parent`. However, you cannot use a single pair of pipes. You must use 2 pairs of pipes. – alvits Oct 11 '16 at 00:35
  • Will try that alvits – Sumanth Oct 11 '16 at 01:39
  • I am getting output now , but all the second process runs all the output – Sumanth Oct 11 '16 at 02:17
  • Most likely you wired the pipes incorrectly. The first pair of pipes are strictly between the 2 children. The second pair of pipes are strictly between the 2nd child and the parent process. Parent can then read from the read side of the pipe. – alvits Oct 11 '16 at 02:20
  • Can you help me with that , i can paste my code – Sumanth Oct 11 '16 at 03:53
  • You haven't pasted your code yet. Create 2 pairs of pipes, `pipe(c2c)` and `pipe(c2p)`. Use `c2c[0]` and `c2c[1]` between the 2 children. Use `c2p[0]` and `[c2p[1]` between the second child and the parent. – alvits Oct 11 '16 at 21:00

0 Answers0