Using dup2 for piping
Asked Answered
E

2

20

How do I use dup2 to perform the following command?

ls -al | grep alpha | more
Equalitarian answered 4/9, 2010 at 14:46 Comment(0)
M
32

A Little example with the first two commands. You need to create a pipe with the pipe() function that will go between ls and grep and other pipe between grep and more. What dup2 does is copy a file descriptor into another. Pipe works by connecting the input in fd[0] to the output of fd[1]. You should read the man pages of pipe and dup2. I may try and simplify the example later if you have some other doubts.

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define READ_END 0
#define WRITE_END 1

int 
main(int argc, char* argv[]) 
{
    pid_t pid;
    int fd[2];

    pipe(fd);
    pid = fork();

    if(pid==0)
    {
        printf("i'm the child used for ls \n");
        dup2(fd[WRITE_END], STDOUT_FILENO);
        close(fd[WRITE_END]);
        execlp("ls", "ls", "-al", NULL);
    }
    else
    { 
        pid=fork();

        if(pid==0)
        {
            printf("i'm in the second child, which will be used to run grep\n");
            dup2(fd[READ_END], STDIN_FILENO);
            close(fd[READ_END]);
            execlp("grep", "grep", "alpha",NULL);
        }
    }

    return 0;
}
Marcheshvan answered 4/9, 2010 at 16:55 Comment(5)
it could be improved by using only one fork() and using the original process for either ls or grep, but well I'll leave that to you :PMarcheshvan
How should we pipe the output of the grep to the more. It is just piping two processes right ?Shifty
Shouldn't you close the WRITE_END for grep and the READ_END for ls (opposite of what you did)?Keyhole
not working when first execlp() changed with execlp("cat", "cat", "test.c", NULL); and next with execlp("tail", "tail", "-n", "3",NULL);Cloudcapped
Example is majorly flawed. You didn't check if the fork failed, and you should always close both ends of the pipe, not just one. Otherwise, the parent attempting to read until EOF will never receive EOF.Mandelbaum
S
2

You would use pipe(2,3p) as well. Create the pipe, fork, duplicate the appropriate end of the pipe onto FD 0 or FD 1 of the child, then exec.

Sympathizer answered 4/9, 2010 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.