LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help w/ shell handling 3 commands/2 pipes (https://www.linuxquestions.org/questions/programming-9/need-help-w-shell-handling-3-commands-2-pipes-205574/)

sptchamp 07-15-2004 07:05 PM

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;



itsme86 07-15-2004 08:26 PM

Which is the "middle function"?

sptchamp 07-15-2004 09:01 PM

ls | cat | wc, cat would be the middle function. Sorry, I should've said process.

infamous41md 07-15-2004 09:37 PM

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 07:00 AM.