Linux pipe, fork and execlp: how to get the value written into stream 1

fuiiii

I use a function (L) to execute another program (K) through execlp(). In the K program, the result is written into stream 1:

write(1, (char *)&o, sizeof(int));

Since after execlp(), the remaining part of L won't be executed, how could I get the result written in stream 1?

Don't ask me why I need to do it this way. It's the requirement for a project.

I followed your guys advices, but now the problem is, the way the K program get the arguments are from streams(one standard stream, one other stream), I'm using pipes to write arguments into corresponding streams (it's done by the parent).

After the child exec, in the parent part, I read from stream 0 (the K program writes its result back in stream 1). But what I could get is what the parent wrote into the stream, not what the K program write back. What's wrong? Do I need to add another pipe?

Thanks!!

merlin2011

The key insight, which Jonathan Leffler mentioned in his comment, is that you need to fork the program which is running L before you call execlp().

After the fork, the parent continues to execute the rest of L, and the child morphs into the program K by calling execlp(), which should never return unless there is an error.

Thus, the assertion that "the remaining part of L won't be executed" is incorrect. It will get executed, in the parent process if you write the function L correctly.

Update: Since the OP made his question more specific, I am appending to this answer.

If you want to retrieve what the child process wrote to stdout (fd 1), you need to create a new pipe before the fork, and copy the writing end of this pipe into the child's stdout.

Here is an example program, slightly modified from the pipe man page.

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

int
main(int argc, char *argv[])
{
    int pipefd[2];
    pid_t cpid;
    char buf;

    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    cpid = fork();
    if (cpid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (cpid == 0) {    /* Child writes into the pipe */
        close(pipefd[0]);          /* Close unused read end */

        // Copy the writing end of the pipe into STDOUT for the child.
        dup2(pipefd[1], STDOUT_FILENO);

        // Execute your program L here, and its stdout will be captured by the pipe.
        const char* message = "Child is speaking to stdout!";
        write(STDOUT_FILENO, message, strlen(message));
        write(STDOUT_FILENO, "\n", 1);


        close(pipefd[1]);
        _exit(EXIT_SUCCESS);

    } else {            /* Parent reads child's stdout  from the pipe */
        close(pipefd[1]);          /* Close unused write end */

        // Here the parent process is reading the child's stdout.
        while (read(pipefd[0], &buf, 1) > 0)
            write(STDOUT_FILENO, &buf, 1);
        wait(NULL);                /* Wait for child */
        exit(EXIT_SUCCESS);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pipe what is written to Stream 1 into Stream 2?

From Dev

How to write to the pipe fork?

From Dev

Combine fork,fifo and execlp?

From Dev

Combine fork,fifo and execlp?

From Dev

How to fork/duplicate a stream

From Dev

I do not understand how execlp() works in Linux

From Dev

Max Value search in array using fork and pipe

From Dev

Fork and execlp not executing my program?

From Dev

The size of data written to stream obtained from IO#pipe

From Dev

How to get the maximum index value of the stream

From Dev

How to get value that has been written in editable JComboBox?

From Dev

How to get the config value in Sphinx extension written by me?

From Dev

How to get all values from a variable made of value separated by a pipe ( | )?

From Dev

How do I get a value after pipe complete?

From Dev

Where does the linux kernel keep the data written to a pipe

From Dev

Linux-C: reading from pipe returns first buffer written to it

From Dev

Stuffing output of execlp through a pipe and then printing it to console

From Dev

Linux Pipe viewer, how to split the pipe

From Dev

c multiple fork and pipe

From Dev

IPC using fork() and pipe()

From Dev

How to pipe a stream using pdfkit with node js

From Dev

How to pipe YAML into EJS in a Gulp stream?

From Dev

How to pipe stream to reply in hapi.js

From Dev

How to pipe output of active stream to other commands

From Dev

How to pipe YAML into EJS in a Gulp stream?

From Dev

How to render response after stream finished pipe?

From Dev

How can you pipe a readable stream to another readable stream?

From Dev

How can you pipe a readable stream to another readable stream?

From Dev

In Azure Stream Analytics Query, How to get the first value of a group?

Related Related

  1. 1

    How to pipe what is written to Stream 1 into Stream 2?

  2. 2

    How to write to the pipe fork?

  3. 3

    Combine fork,fifo and execlp?

  4. 4

    Combine fork,fifo and execlp?

  5. 5

    How to fork/duplicate a stream

  6. 6

    I do not understand how execlp() works in Linux

  7. 7

    Max Value search in array using fork and pipe

  8. 8

    Fork and execlp not executing my program?

  9. 9

    The size of data written to stream obtained from IO#pipe

  10. 10

    How to get the maximum index value of the stream

  11. 11

    How to get value that has been written in editable JComboBox?

  12. 12

    How to get the config value in Sphinx extension written by me?

  13. 13

    How to get all values from a variable made of value separated by a pipe ( | )?

  14. 14

    How do I get a value after pipe complete?

  15. 15

    Where does the linux kernel keep the data written to a pipe

  16. 16

    Linux-C: reading from pipe returns first buffer written to it

  17. 17

    Stuffing output of execlp through a pipe and then printing it to console

  18. 18

    Linux Pipe viewer, how to split the pipe

  19. 19

    c multiple fork and pipe

  20. 20

    IPC using fork() and pipe()

  21. 21

    How to pipe a stream using pdfkit with node js

  22. 22

    How to pipe YAML into EJS in a Gulp stream?

  23. 23

    How to pipe stream to reply in hapi.js

  24. 24

    How to pipe output of active stream to other commands

  25. 25

    How to pipe YAML into EJS in a Gulp stream?

  26. 26

    How to render response after stream finished pipe?

  27. 27

    How can you pipe a readable stream to another readable stream?

  28. 28

    How can you pipe a readable stream to another readable stream?

  29. 29

    In Azure Stream Analytics Query, How to get the first value of a group?

HotTag

Archive