|
Home > Archive > Unix Programming > September 2007 > Multiple pipes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| ZhukovL@gmail.com 2007-09-26, 1:34 am |
| I'm having some trouble implementing the handling of multiple pipes in
a shell I'm writing. I was hoping someone could point me in the right
direction because I really cant see where I'm going wrong. Its
supposed to work as follows: main shell forks off a child shell, the
child shell sets up the pipes and forks off n-1 other shells (where n
is the total number of commands). Each child shell sets its pipe file
descriptors then execs to whatever its supposed to be, the original
child shell is the final command which the main shell waits for it to
return.
Everything works except for the piping, below is the relevant code:
//the number of execution blocks (each will be a sperate command)
int n;
int i;
pid_t pid[n];
int pfd[n-1][2];
..
..
..
pid[i] = fork();
if(pid[i] == 0){
/*The main child shell*/
/*Create n-1 pipes*/
while(i < (n-1) ){
pipe(pfd[i]);
i++;
}
i = 1;
//call fork() for the first n-1 processes
while (i < n ){
pid[i-1] = fork();
if(pid[i-1] == 0){
break;
}
i++;
}
//then the child shells do some stuff here
//this is where I need help (the blist->pipes is just an id of the
command)
if( blist->pipes == 1 || i == n && n != 1 ){
//redirect STDIN
if(i > 1) dup2(pfd[i-2][0], STDIN);
//redirect STDOUT
if(i < n) dup2(pfd[i-1][1], STDOUT);
//close all pipe file descriptors
d = 0;
while (d < n){
close(pfd[d][0]);
close(pfd[d][1]);
d++;
}
| |
| ZhukovL@gmail.com 2007-09-26, 1:26 pm |
| Ok now I'm just totally confused. I was going though the code again
and adding some more comments because they need to see them for class
and then compiled it again, the pipes worked this time. What happened?
Why would adding comments make it work? Did it change that the order
the children get executed in or something? Can that be controlled? Any
advice is appreciated.
Also, this isn't really homework help because for class the shell just
has to be able to handle 1 pipe. That I can do just fine but I want it
to actually work so I'm going for multiple pipes.
| |
| Ivan Gotovchits 2007-09-27, 1:34 pm |
| ZhukovL@gmail.com wrote:
> I'm having some trouble implementing the handling of multiple pipes in
> a shell I'm writing. I was hoping someone could point me in the right
> direction because I really cant see where I'm going wrong. Its
> supposed to work as follows: main shell forks off a child shell, the
> child shell sets up the pipes and forks off n-1 other shells (where n
> is the total number of commands). Each child shell sets its pipe file
> descriptors then execs to whatever its supposed to be, the original
> child shell is the final command which the main shell waits for it to
> return.
>
> Everything works except for the piping, below is the relevant code:
Don't understand, why main shell must create a child shell?
The "main shell" (i.e. the parent one) must create pipe with `pipe', next it
forks a child, and now the pipe between father and child is established.
Your code is unreadable, please provide the full listing.
>
> //the number of execution blocks (each will be a sperate command)
> int n;
> int i;
> pid_t pid[n];
> int pfd[n-1][2];
> .
> .
> .
> pid[i] = fork();
what is the value of the i variable? And what is the reason of forking
before calling pipe?
> if(pid[i] == 0){
where the end of this if statement?
> /*The main child shell*/
> /*Create n-1 pipes*/
> while(i < (n-1) ){
> pipe(pfd[i]);
> i++;
> }
>
> i = 1;
> //call fork() for the first n-1 processes
> while (i < n ){
>
> pid[i-1] = fork();
> if(pid[i-1] == 0){
> break;
> }
> i++;
> }
>
Well, really your programm is unreadable, not surpising that it is buggy ))
no offence :^)
--
s/.../.gotovchits/g for email.
|
|
|
|
|