简体   繁体   English

C ++捕获分叉的子项

[英]C++ capture forked child cout

I have the following code snippet: 我有以下代码片段:

stringstream userOut;
streambuf* old = cout.rdbuf(userOut.rdbuf());

pid_t pid;

if (!(pid = fork())) {

    cout << "hello from child!" << endl;
    exit(0);


} else {
    int status;

    // wait for child to finish
    wait(&status);

    // give cout old buffer back
    cout.rdbuf(old);

    // prints nothing!
    cout << "child content: " << userOut.str() << endl;
}

I want to be able to capture and redirect cout from the child process to be used in the parent process, but so far the redirected output it always empty. 我希望能够从子流程中捕获并重定向cout以便在父流程中使用,但是到目前为止,重定向输出始终为空。 What could be causing this and are there any available solutions? 是什么原因造成的,有没有可用的解决方案?

As mentioned in the comments, the forked process gets a copy of the stream. 如评论中所述,分叉进程获取流的副本。 So its writing to a copy of the output stream. 因此将其写入输出流的副本。

Usually, you want to use pipes for that kind a problems. 通常,您希望使用管道解决此类问题。 If you consult man 2 pipe , you will find this example at the end : 如果咨询man 2 pipe ,您将在最后找到此示例:

The following program creates a pipe, and then fork(2)s to create a child process; 下面的程序创建一个管道,然后fork(2)创建一个子进程。 the child inherits a duplicate set of file descriptors that refer to the same pipe. 子级继承了引用同一管道的重复的文件描述符集。 After the fork(2), each process closes the file descriptors that it doesn't need for the pipe (see pipe(7)). 在fork(2)之后,每个进程都会关闭管道不需要的文件描述符(请参阅pipe(7))。 The parent then writes the string contained in the program's command- line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. 然后,父级将程序的命令行参数中包含的字符串写入管道,而子级一次从管道读取该字符串一个字节,并在标准输出中回显它。

   #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 (argc != 2) {
           fprintf(stderr, "Usage: %s <string>\n", argv[0]);
           exit(EXIT_FAILURE);
       }

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

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

       if (cpid == 0) {    /* Child reads from pipe */
           close(pipefd[1]);          /* Close unused write end */

           while (read(pipefd[0], &buf, 1) > 0)
               write(STDOUT_FILENO, &buf, 1);

           write(STDOUT_FILENO, "\n", 1);
           close(pipefd[0]);
           _exit(EXIT_SUCCESS);

       } else {            /* Parent writes argv[1] to pipe */
           close(pipefd[0]);          /* Close unused read end */
           write(pipefd[1], argv[1], strlen(argv[1]));
           close(pipefd[1]);          /* Reader will see EOF */
           wait(NULL);                /* Wait for child */
           exit(EXIT_SUCCESS);
       }
   }

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

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