简体   繁体   English

从execlp()获得回报

[英]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? 如何从execlp检索响应并进行打印? 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. 调用execlp() ,当前进程的内存映像将调用的progame 替换 ,因此您无法通过返回值获得所需的内容。 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. 正确设置在父进程和子进程之间进行通信的管道后,您可以在其stdout中写入子进程的结果,并从其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 : 并测试文件temp.txt

$ cat temp.txt
a
e
b
d
f
c

Result of a test run: 测试结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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