C++ pipe and fork

风格不统一 提交于 2021-01-29 03:09:14

问题


I'm working on a sample program to learn how pipes and forking works. In my very basic implementation, in my child process, i closed 0 and duplicated the read end of the pipe so that file descriptor 0 is now the read end of my pipe.

From my parent process, I write out a string, and in my child process, I read the string using cin as cin essentially is my read end of the pipe and what I observe is the complete string does not print out and I can't seem to understand why!

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TEST_STRING "Hello, Pipe!"
int main(int argc, char const *argv[]) {
  int fd[2];
  pipe(fd);

  pid_t pid;
  if ((pid = fork()) == 0) {
    //Child
    close(0);
    close(fd[1]);
    int myCin = dup(fd[0]);
    char buf[sizeof(TEST_STRING)];

    // int x;
    // std::cin >> x;
    // std::cout << x << std::endl;
    // read(myCin, buf, sizeof(TEST_STRING));
    std::cin >> buf;

    std::cout << buf << std::endl;

  }
  else {
    //parent
    write(fd[1], TEST_STRING, sizeof(TEST_STRING));
    close(fd[1]);
    waitpid(pid, NULL, 0);
  }
  return 0;
}

Here's my strace as well:

clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fd895adaa10) = 1904
strace: Process 1904 attached
[pid  1903] write(4, "Hello, Pipe!\0", 13) = 13
[pid  1903] close(4)                    = 0
[pid  1903] wait4(1904,  <unfinished ...>
[pid  1904] close(0)                    = 0
[pid  1904] close(4)                    = 0
[pid  1904] dup(3)                      = 0
[pid  1904] fstat(0, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
[pid  1904] read(0, "Hello, Pipe!\0", 4096) = 13
[pid  1904] fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid  1904] write(1, "Hello,\n", 7)     = 7
[pid  1904] read(0, "", 4096)           = 0
[pid  1904] exit_group(0)               = ?
[pid  1904] +++ exited with 0 +++

回答1:


When you read from cin that way it will discard leading whitespace and then stop at the next whitespace character. So that's why it only returned what it did. Try std:getline.

You shouldn't count on dup() choosing FD 0 for you. Use dup2() so you can specify what descriptor to use.

I also doubt changing the FD from under cin is safe. You could get buffered data from before the FD was duped.



来源:https://stackoverflow.com/questions/42311299/c-pipe-and-fork

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!