Multiple pipes

Z

ZhukovL

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 = fork();
if(pid == 0){
/*The main child shell*/
/*Create n-1 pipes*/
while(i < (n-1) ){
printf("I is : %d", i);
pipe(pfd);
printf("Pipe created and put into pfd[%d]\n", 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
if( blist->pipes == 1 || i == n && n != 1 ){
printf("\nI'm supposed to pipe because __commands->pipes = %d",
blist->pipes);


//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++;
}
}
 
J

Jack Klein

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.

[snip]

The C language does not have forks and pipes. Your operating system
has such features, and they are supported by non-standard extensions
to the language.

You need to ask in a group that supports your compiler/OS combination.
Judging by the headers on your post, I'd suggest

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
S

SM Ryan

# 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

One way to simplify is to get it working correctly in the special case
of exactly two processes and one pipe.

Then make that a recursive function. If you're worried about the
inefficiency of recursion, you can convert the recursion to iterative
once you've got the recursive version working correctly.
 
W

William Pursell

[OFF TOPIC]
I'm having some trouble implementing the handling of multiple pipes in
a shell I'm writing.

Not quite topical, but there are 2 obvious problems, at least one
of which may be the problem.

First problem: (likely not the important one)
pipe(pfd);
printf("Pipe created and put into pfd[%d]\n", i);
i++;

You don't check that pipe() is succesful.

2nd problem: (very likely relevant):
Unless I'm missing something, you've not quite closed
all the fds. There is the intermediate process (the
"main child process") that still has STDIN_FILENO
and STDOUT_FILENO open. It may be that the first
process in your pipe is waiting for that pipe to
close.

I noticed that you are using STDOUT, instead of
the more standard STDOUT_FILENO defined in
<unistd.h> (the name of which emphasizes the
non-topicality of the thread...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top