09-27-07 06: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.
[ Post a follow-up to this message ]
|