LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 07-15-2004, 07:05 PM   #1
sptchamp
LQ Newbie
 
Registered: Jul 2004
Posts: 9

Rep: Reputation: 0
Question 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 *cmd1cmd_type *cmd2cmd_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(pathname3cmd3->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(pathname2cmd2->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(pathname1cmd1->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;

 
Old 07-15-2004, 08:26 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Which is the "middle function"?
 
Old 07-15-2004, 09:01 PM   #3
sptchamp
LQ Newbie
 
Registered: Jul 2004
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 07-15-2004, 09:37 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic shell commands pppaaarrrkkk Linux - Newbie 7 02-05-2007 07:31 AM
[Perl] brainstorming with pipes & commands patator Programming 3 06-25-2004 04:17 AM
Shell commands in HTML? Spooky Programming 8 01-01-2004 11:18 AM
shell commands! krishlinux General 1 10-05-2003 04:47 AM
how to delete old shell commands a_hic Linux - Newbie 4 05-01-2003 06:16 AM


All times are GMT -5. The time now is 09:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration