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?