简体   繁体   中英

Getting return from execlp()

I have a program which I would like to sort the first column in a file, from a child process, and return the output to the parent process. How can I retrieve the response from the execlp and print it? Here is what I have so far:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define WRITE 1
#define READ 0

int main(int argc, char **argv)
{
    int i, k;
    int p1[2], p2[2];

    int p1[2], p2[2];
    pid_t childID;

    if (pipe(p1) < 0 || pipe(p2) < 0) {
        perror("pipe");
        exit(0);
    }

    childID = fork();

    if (childID < 0) {      
        perror("fork");
        exit(0);
    }
    else if (childID == 0){                                 
        close(p1[WRITE]);
        close(p2[READ]);

        dup2(p1[READ], STDIN_FILENO);
        close(p1[READ]);
        dup2(p2[WRITE], STDOUT_FILENO);
        close(p2[WRITE]);

        execlp("sort", "-k1", "-n", "temp.txt", (char *)NULL);
        perror("exec");
        exit(0);
    }
    else {                                                  
        //parent process
        //Not sure how to get response from exec


    }
}

After call execlp() , the memory image of current process will be replaced by the called progame, so you cannot get what you want through return value. What you can do is let the child process write its result to somehere, such as a temporal file or a pipe, and the parent process read the result from this place.

After proper setup a pipe to communite between parent and child processes, you can write the result of child process in its stdout, and read the result in parent processes from its stdin.

Something like this:

else if (childID == 0){                                 
    close(p1[READ]);

    dup2(p1[WRITE], STDOUT_FILENO);
    close(p1[WRITE]);

    execlp("sort", "-k1", "-n", "temp.txt", (char *)NULL);
    perror("exec");
    exit(0);
}
else {                                                  
    close(p1[WRITE]);

    dup2(p1[READ], STDIN_FILENO);
    close(p1[READ]);

    while (scanf("%ms ", &l) != EOF) {
        printf("%s\n", l);
        free(l);
    }
}

Here is full code:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define WRITE 1
#define READ 0

int main(int argc, char **argv)
{
    int p1[2];
    char *l;

    pid_t childID;

    if (pipe(p1) < 0) {
        perror("pipe");
        exit(0);
    }

    childID = fork();

    if (childID < 0) {      
        perror("fork");
        exit(0);
    }
    else if (childID == 0){                                 
        close(p1[READ]);

        dup2(p1[WRITE], STDOUT_FILENO);
        close(p1[WRITE]);

        execlp("sort", "-k1", "-n", "temp.txt", (char *)NULL);
        perror("exec");
        exit(0);
    }
    else {                                                  
        close(p1[WRITE]);

        dup2(p1[READ], STDIN_FILENO);
        close(p1[READ]);

        while (scanf("%ms ", &l) != EOF) {
            printf("%s\n", l);
            free(l);
        }
    }

    return 0;
}

And test file temp.txt :

$ cat temp.txt
a
e
b
d
f
c

Result of a test run:

$ ./a.out 
a
b
c
d
e
f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM