I've created a question about this a few days. My solution is something in the lines of what was suggested in the accepted answer. However, a friend of mine came up with the following solution:
Please note that the code has been updated a few times (check the edit revisions) to reflect the suggestions in the answers below. If you intend to give a new answer, please do so with this new code in mind and not the old one which had lots of problems.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int fd[2], i, aux, std0, std1;
do {
std0 = dup(0); // backup stdin
std1 = dup(1); // backup stdout
// let's pretend I'm reading commands here in a shell prompt
READ_COMMAND_FROM_PROMPT();
for(i=1; i<argc; i++) {
// do we have a previous command?
if(i > 1) {
dup2(aux, 0);
close(aux);
}
// do we have a next command?
if(i < argc-1) {
pipe(fd);
aux = fd[0];
dup2(fd[1], 1);
close(fd[1]);
}
// last command? restore stdout...
if(i == argc-1) {
dup2(std1, 1);
close(std1);
}
if(!fork()) {
// if not last command, close all pipe ends
// (the child doesn't use them)
if(i < argc-1) {
close(std0);
close(std1);
close(fd[0]);
}
execlp(argv[i], argv[i], NULL);
exit(0);
}
}
// restore stdin to be able to keep using the shell
dup2(std0, 0);
close(std0);
}
return 0;
}
This simulates a series of commands through pipes like in bash, for instance: cmd1 | cmd2 | ... | cmd_n. I say "simulate", because, as you can see, the commands are actually read from the arguments. Just to spare time coding a simple shell prompt...
Of course there are some issues to fix and to add like error handling but that's not the point here. I think I kinda get the code but it still makes me a lot of confusing how this whole thing works.
Am I missing something or this really works and it's a nice and clean solution to solve the problem? If not, can anyone point me the crucial problems this code has?
Looks reasonable, though it really needs to fix leaking
std
andaux
to the children and after the loop, and the parent's originalstdin
is lost forever.This would probably be better with color...
foo
getsstdin=stdin
,stdout=pipe1[1]
bar
getsstdin=pipe1[0]
,stdout=pipe2[1]
baz
getsstdin=pipe2[0]
,stdout=stdout
My suggestion is different in that it avoids mangling the parent's
stdin
andstdout
, only manipulating them within the child, and never leaks any FDs. It's a bit harder to diagram, though.Edit
Your updated code does fix the previous FD leaks… but adds one: you're now leaking
std0
to the children. As Jon says, this is probably not dangerous to most programs... but you still should write a better behaved shell than this.Even if it's temporary, I would strongly recommend against mangling your own shell's standard in/out/err (0/1/2), only doing so within the child right before exec. Why? Suppose you add some
printf
debugging in the middle, or you need to bail out due to an error condition. You'll be in trouble if you don't clean up your messed-up standard file descriptors first. Please, for the sake of having things operate as expected even in unexpected scenarios, don't muck with them until you need to.Edit
As I mentioned in other comments, splitting it up into smaller parts makes it much easier to understand. This small helper should be easily understandable and bug-free:
As should this:
You can see Bash's
execute_cmd.c#execute_disk_command
being called fromexecute_cmd.c#execute_pipeline
, xsh'sprocess.c#process_run
being called fromjobs.c#job_run
, and even every single one of BusyBox's various small and minimal shells splits them up.This is my "final" code with ephemient suggestions:
Is it ok now?
It will give results, some that are not expected. It is far from a nice solution: It messes with the parent process' standard descriptors, does not recover the standard input, descriptors leak to children, etc.
If you think recursively, it may be easier to understand. Below is a correct solution, without error checking. Consider a linked-list type
command
, with it'snext
pointer and aargv
array.Call it with the first command in the linked-list, and
input
= -1. It does the rest.The key problem is that you create a bunch of pipes and don't make sure that all the ends are closed properly. If you create a pipe, you get two file descriptors; if you fork, then you have four file descriptors. If you
dup()
ordup2()
one end of the pipe to a standard descriptor, you need to close both ends of the pipe - at least one of the closes must be after the dup() or dup2() operation.Consider the file descriptors available to the first command (assuming there are at least two - something that should be handled in general (no
pipe()
or I/O redirection needed with just one command), but I recognize that the error handling is eliminated to keep the code suitable for SO):Note that because
fd[0]
is not closed in the child, the child will never get EOF on its standard input; this is usually problematic. The non-closure ofstd
is less critical.Revisiting amended code (as of 2009-06-03T20:52-07:00)...
Assume that process starts with file descriptors 0, 1, 2 (standard input, output, error) open only. Also assume we have exactly 3 commands to process. As before, this code writes out the loop with annotations.
So, all the children have the original standard input connected as file descriptor 3. This is not ideal, though it is not dreadfully traumatic; I'm hard pressed to find a circumstance where this would matter.
Closing file descriptor 4 in the parent is a mistake - the next iteration of 'read a command and process it won't work because
std1
is not initialized inside the loop.Generally, this is close to correct - but not quite correct.
Both in this question and in another (as linked in the first post), ephemient suggested me a solution to the problem without messing with the parents file descriptors as demonstrated by a possible solution in this question.
I didn't get his solution, I tried and tried to understand but I can't seem to get it. I also tried to code it without understanding but it didn't work. Probably because I've failed to understand it correctly and wasn't able to code it the it should have been coded.
Anyway, I tried to come up with my own solution using some of the things I understood from the pseudo code and came up with this:
This may not be the best and cleanest solution but it was something I could come up with and, most importantly, something I can understand. What good is to have something working that I don't understand and then I'm evaluated by my teacher and I can't explain to him what the code is doing?
Anyway, what do you think about this one?