Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-15-2004, 07:05 PM
|
#1
|
LQ Newbie
Registered: Jul 2004
Posts: 9
Rep:
|
Need help w/ shell handling 3 commands/2 pipes
I'm trying to write a shell in C that will take 3 commands linked together with 2 pipes. Here's what I have so far but the middle function sees a bad descriptor. Can anyone help?
PHP Code:
int runPipes3(cmd_type *cmd1, cmd_type *cmd2, cmd_type *cmd3)
{
int fds1[2];
int fds2[2];
int pid1;
int pid2;
int pid3;
int status;
char *pathname1 = getPath(cmd1->argv[0]);
char *pathname2 = getPath(cmd2->argv[0]);
char *pathname3 = getPath(cmd3->argv[0]);
if(pipe(fds1) == -1)
{
printf("ERROR: pipe()\n");
return 1;
}
if(pipe(fds2) == -1)
{
printf("ERROR: pipe()\n");
return 1;
}
if((pid1 = fork()) < 0)
{
printf("ERROR: pipe()\n");
return 1;
}
if(pid1 == 0)
{
//3rd process
close(fds1[1]);
dup2(fds1[0], 0);
// close(fds1[0]);
execv(pathname3, cmd3->argv);
printf("ERROR: execv()\n");
return 1;
}
else
{
//parent process
//need to fork again, so the shell isn't replaced
if((pid2 = fork()) < 0)
{
printf("ERROR: fork()\n");
return 1;
}
if(pid2 == 0)
{
// close(fds2[0]);
// close(fds1[1]);
dup2(fds2[1], 0);
dup2(fds1[0], 1);
execv(pathname2, cmd2->argv);
printf("ERROR: fork()\n");
return 1;
}
else
{
if((pid3 = fork()) < 0)
{
printf("ERROR: fork()\n");
return 1;
}
if(pid3 == 0)
{
//1st process
close(fds2[0]);
dup2(fds2[1], 1);
// close(fds2[1]);
execv(pathname1, cmd1->argv);
printf("ERROR: fork()\n");
return 1;
}
else
{
//parent process (the shell)
close(fds1[0]);
close(fds1[1]);
close(fds2[0]);
close(fds2[1]);
if(cmd3->bckgnd == 0)
while(wait(&status) != pid3);
if(cmd2->bckgnd == 0)
while(wait(&status) != pid2);
if(cmd1->bckgnd == 0)
while(wait(&status) != pid1);
}
}
}
return 0;
}
|
|
|
07-15-2004, 08:26 PM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Which is the "middle function"?
|
|
|
07-15-2004, 09:01 PM
|
#3
|
LQ Newbie
Registered: Jul 2004
Posts: 9
Original Poster
Rep:
|
ls | cat | wc, cat would be the middle function. Sorry, I should've said process.
Last edited by sptchamp; 07-15-2004 at 09:14 PM.
|
|
|
07-15-2004, 09:37 PM
|
#4
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
you should try a an approach that works on any number of pipelined processes. you should have an array of pipes, pipes[NPIPES][2]. after u have a command line, go thru and for each pipe fork a child, duplicate descriptors, and execute the command. to control when to duplicate descriptors, well you know that every child after the first will have its standard input duplicated, and every command except the last has its output duplicated. something that is very informative is
$ strace -vf /bin/sh ls | sort | grep -v bla
|
|
|
All times are GMT -5. The time now is 11:35 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|