简体   繁体   English

将stdout馈送到将对execv()进行排序的子进程

[英]Feeding stdout to a child process which will execv() sort

I am trying to find out how I can send output of one process into a child process. 我试图找出如何将一个进程的输出发送到子进程中。 I have gone down a journey learning of file descriptors and pipes. 我开始学习文件描述符和管道的过程。 I think I am almost there but am missing a key component. 我想我快要到了,但缺少关键要素。

This is what I have so far: 这是我到目前为止的内容:

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

int main(int argc, char *argv[]) {
    int fd[2];
    pid_t sort_pid;


    /* Create the pipe */
    if(pipe(fd) == -1) {
        fprintf(stderr, "Pipe failed\n");
        exit(EXIT_FAILURE);
    }
    /* create child process that will sort */
    sort_pid = fork();
    if(sort_pid < 0) { // failed to fork
        fprintf(stderr, "Child Fork failed\n");
        exit(EXIT_FAILURE);
    }
    else if(sort_pid == 0) { // child process
        close(0);   // close stdin
        dup2(fd[0], 0); // make stdin same as fd[0]
        close(fd[1]); // don't need this end of the pipe
        execlp("D:/Cygwin/bin/sort", "sort", NULL);
    }
    else { // parent process
        close(1); // close stdout
        dup2(fd[1], 1); // make stdout same as fd[1]
        close(fd[0]); // don't need this end of the pipe
        printf("Hello\n");
        printf("Bye\n");
        printf("Hi\n");
        printf("G'day\n");
        printf("It Works!\n");
        wait(NULL);
    }

    return EXIT_SUCCESS;
}

This doesn't work, as it seems to go into an endless loop or something. 这是行不通的,因为它似乎陷入了无休止的循环。 I tried combinations of the wait() but that doesnt help either. 我尝试了wait()的组合,但这都没有帮助。

I am doing this to learn how to apply this idea in my actual program. 我这样做是为了学习如何在我的实际程序中应用这个想法。 In my actual program I read files, parse them line by line and save the processed data to a static array of structs. 在我的实际程序中,我读取文件,逐行解析它们,并将处理后的数据保存到结构的静态数组中。 I want to be able to then generate output based on these results and use the fork() and execv() syscalls to sort the output. 我希望能够根据这些结果生成输出,并使用fork()和execv()系统调用对输出进行排序。

This is ultimately for a project in uni. 这最终是针对uni中的项目的。

These are similar examples which I dissected to get to the stage I am at so far: 这些是我目前所达到的阶段的类似示例:

Furthermore I read the manual pages on the relevant syscalls to try and understand them. 此外,我阅读了有关syscall的手册页,以尝试并理解它们。 I will admit my knowledge of pipes and using them is still basically nothing, as this is my first every try with them. 我会承认我对管道的了解,而使用管道基本上还是一无所有,因为这是我第一次尝试使用管道。

Any help is appreciated, even further sources of information I could look into myself. 感谢您提供任何帮助,甚至我可以进一步了解自己的信息来源。 I seem to have exhausted most of the useful stuff a google search give me. 我似乎已经用尽了Google搜索给我的大部分有用信息。

sort will read until it encounters end-of-file. sort将一直读取,直到遇到文件末尾。 You therefore have to close the write-end of the pipe if you want it to complete. 因此,如果要完成,必须关闭管道的写端。 Because of the dup2 , you have two copies of the open file description , so you need 由于dup2 ,您具有打开文件描述的两个副本,因此您需要

  1. close(fd[1]); anytime after the call to dup2 调用dup2之后的任何时间
  2. close(1); after you're done writing to (the new) stdout 写完(新) stdout

Make sure to fflush(stdout) before the second of these to ensure that all your data actually made it into the pipe. 确保在第二个步骤前进行fflush(stdout) ,以确保所有数据实际上都已放入管道中。

(This is a simple example of a deadlock: sort is waiting on the pipe to close, which will happen when the parent exits. But the parent won't exit until it finishes waiting on the child to exit…) (这是一个简单的死锁示例: sort正在等待管道关闭,这会在父项退出时发生。但是,父项直到完成等待子项退出后才会退出……)

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

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