For the past few days I have been attempting to write my own shell implementation but I seem to have gotten stuck on getting pipes to work properly. I am able to parse a li
Here are the notes on pipe from the Systems Programming class I took last semester.
When I needed to do a similar shell some years ago, I used the book Practical Unix Programming.
It is really useful for examples on many IPC topics. I still have a copy on my desk that I reference from time to time. For $2 - $9 used, it's a pretty good value for what you get.
For what it's worth, just thought I'd mention it.
First suggestion: Symbolic constants are better than magic numbers.
const int PIPE_READ = 0;
const int PIPE_WRITE = 1;
int fd[2];
pipe(fd);
// Now you can refer to fd[PIPE_READ] and fd[PIPE_WRITE].
Second suggestion: Take a step back and think about what you're trying to accomplish.
You want to spawn two processes, with the first process's stdout connected to the second process's stdin. Right?
So, in C, this means that you need to take call pipe
, pass fd[PIPE_WRITE]
to the first child process, which will dup2
it to 1, and pass fd[PIPE_READ]
to the second child process, which will dup2
it to 0.
Simply looking at forkAndExecute'
s prototype shows that it can't do that:
void forkAndExecute( char* arrayOfWords[] , vector *vectorOfPIDs ,
bool hasNextCmd , bool hasPrevCmd);
It only handles a single command, and from looking at that argument list, unless it resorts to evil global variables, there's no way for it to receive a file descriptor from its PrevCmd or receive a file descriptor from its NextCmd.
Think about how to manage the file descriptors that you need, and redesign forkAndExecute
to be able to use these.
ok This is working for me. Hope this helps you:
/************************
function: void pipeCommand(char** cmd1, char** cmd2)
comment: This pipes the output of cmd1 into cmd2.
**************************/
void pipeCommand(char** cmd1, char** cmd2) {
int fds[2]; // file descriptors
pipe(fds);
// child process #1
if (fork() == 0) {
// Reassign stdin to fds[0] end of pipe.
dup2(fds[0], STDIN_FILENO);
close(fds[1]);
close(fds[0]);
// Execute the second command.
// child process #2
if (fork() == 0) {
// Reassign stdout to fds[1] end of pipe.
dup2(fds[1], STDOUT_FILENO);
close(fds[0]);
close(fds[1]);
// Execute the first command.
execvp(cmd1[0], cmd1);
}
wait(NULL);
execvp(cmd2[0], cmd2);
}
close(fds[1]);
close(fds[0]);
wait(NULL);
}
You are connecting each program's input to its own output. You probably wanted to connect each program's output to the next one's input instead.
Instead of going for the general case of n processes in a pipeline, you should start with a basis of two and expand from there. You'll gain a better understanding of the way the file descriptors are plugged into each other if you proceed by extending working code instead of shooting directly for the complex structure.
Here's a tutorial on UNIX pipes, specifically about how to construct piplines in a shell-like architecture:
http://www.cse.ohio-state.edu/~mamrak/CIS762/pipes_lab_notes.html
Not much fully-written code, but it describes the concepts pretty well.
You could also download source code for virtually any shell, such as bash, tcsh, zsh, etc.