Multiple pipes
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Multiple pipes




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Multiple pipes  
ZhukovL@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 06: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++;
}






[ Post a follow-up to this message ]



    Re: Multiple pipes  
ZhukovL@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 06: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.






[ Post a follow-up to this message ]



    Re: Multiple pipes  
Ivan Gotovchits


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:58 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register