im currently implements a shell in C. My problem occurs when I try to run a command like this:
SHELL$: sort < txtFile | grep key
im works sort < txtFile in the process (child), and in the parent ie else if(pid > 0) im another command works to the right of the channel.
The program works fine, but it exits from the endless loop, which I set up mainly to receive input from the user.
How can I solve this problem?
this is the code that so far dealt with the pipe, I have not included the code that I have to deal with redirection:
c2p is the channel setup for me for this.
if(pid == 0) { if( PIPE_FLAG ) { close(c2p[0]); if(dup2(c2p[1], STDOUT_FILENO) == -1){ perror("dup2() failed"); exit(2); } } /* Execute command */ execvp(cmd_args[0], cmd_args); perror("exec failed 1. "); /* return only when exec fails */ exit(-1); } else if(pid > 0) { if(PIPE_FLAG) { close(c2p[1]); if(dup2(c2p[0], STDIN_FILENO) == -1){ perror("dup2() failed"); exit(-1); } execvp(nxt_args[0], nxt_args); perror("exec failed 2. "); exit(-1); } } else { /* error occurred */ perror("fork failed"); exit(1); }
c unix shell pipe fork
Hous3aholik
source share