1

The program first creates a pipe, and then creates a child process of the current process through fork().

Then each process closes the file descriptors that are not needed for the read and write pipes.

The child process executes the ls -a command under the current path, and writes the command execution output to the pipe by copying the pipe write descriptor fd[1] to standard output; the parent process reads the pipe data and displays it through fd[0].

Every process in Linux is provided with three standard file descriptor including standard input, output and error files.

By default:

  • Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
  • Standard Output is the shell window or the terminal from which the script runs, abstracted as a file to again make writing scripts & program easier
  • Standard error is the same as standard output: the shell window or terminal from which the script runs.

A file descriptor is simply a number that refers to an open file. By default, file descriptor 0 (zero) refers to the standard input & often abbreviated as stdin.

File descriptor 1 refers to standard output (stdout) and file descriptor 2 refers to standard error (stderr).

You can use dup(2) function to duplicate a file descriptor with the pipe write descriptor fd[1] by using function dup2 in order to relocate the standard output

The issue is how can I execute the command and after that I read the stdout in the child process.

I am executing it using exec function, the line after the exec function will not execute because the child process memory image is now ls -l.

    #include <sys/types.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #define BUFFER_SIZE 25
    #define READ_END 0
    #define WRITE_END 1
    
    int main(void)
    {
        char writemsg[BUFFER_SIZE] = "Greetings!";
        char readmsg[BUFFER_SIZE];
        int fd[2];
        pid_t pid;
    
        /* create the pipe */
        if (pipe(fd) == -1) {
            fprintf(stderr,"Pipe failed");
        return 1;
        }
        /* fork a child process */
        pid = fork();
        if (pid < 0) { /* error occurred */
            fprintf(stderr, "Fork Failed");
            return 1;
        }
        if (pid > 0) { /* parent process */
            /* close the unused end of the pipe */
            close(fd[READ_END]);
            /* write to the pipe */
            write(fd[WRITE_END], writemsg, strlen(writemsg)+1);
            /* close the write end of the pipe */
            close(fd[WRITE_END]);
        }
        else { /* child process */
            /* close the unused end of the pipe */
            close(fd[WRITE_END]);
            /* read from the pipe */
            read(fd[READ_END], readmsg, BUFFER_SIZE);
            printf("read %s\n",readmsg);
            /* close the write end of the pipe */
            close(fd[READ_END]);
        }
        

return 0;
}

this code is working fine. this is the simple code of pipe communication. kindly help me to solve the command execution portion and writing it in child from console

Mathieu
  • 8,840
  • 7
  • 32
  • 45
ayaz
  • 11
  • 1
  • 1
    There is almost an infinite number of examples and tutorials on how to do this, all over the Internet. Not to mention plenty of good books as well. Have you even tried to read the manual pages of the functions mentioned in the assignment (especially `dup2`)? – Some programmer dude Nov 03 '21 at 08:28
  • 1/ In your current code the parent sends a message to its child while your requirement is that the child sends a message to its parent, so you are currently using the pipe in the wrong direction. 2/ you have to close the unused end and redirect the standard output before the exec call. The pipe will be automatically closed when the `ls` command will end. – Serge Ballesta Nov 03 '21 at 08:31
  • if you know such sites or tutorial i would surely take help from it. I have tried on these but could not find any solution – ayaz Nov 03 '21 at 08:31

0 Answers0