【问题标题】:Getting return from execlp()从 execlp() 获取返回
【发布时间】:2014-02-18 05:35:49
【问题描述】:

我有一个程序,我想从子进程对文件中的第一列进行排序,并将输出返回给父进程。如何从 execlp 检索响应并打印它?这是我目前所拥有的:

#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


    }
}

【问题讨论】:

    标签: c process exec pipe


    【解决方案1】:

    调用execlp()后,当前进程的内存映像将被调用的程序替换,因此无法通过返回值得到你想要的。你可以做的就是让子进程把它的结果写到某个地方,比如一个临时文件或者一个管道,而父进程从这个地方读取结果。

    在正确设置管道以在父子进程之间进行通信后,您可以将子进程的结果写入其标准输出,并从其标准输入读取父进程的结果。

    类似这样的:

    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);
        }
    }
    

    这里是完整的代码:

    #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;
    }
    

    和测试文件temp.txt:

    $ cat temp.txt
    a
    e
    b
    d
    f
    c
    

    测试运行结果:

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

    【讨论】:

    • 感谢您的回复。我将如何编写子进程的结果?
    • 谢谢!我应该是什么?
    • @kirax 请参考我最新答案中的完整代码。
    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多