Mac中的缓冲区写入将挂起

时间:2016-02-14 14:01:25

标签: c++ linux macos

以下代码将在我的Mac中挂起;但通过较小的数据大小。有什么建议吗?我认为它与操作系统配置有关,因为它适用于其他具有相同版本的Mac。

#include <iostream>
extern "C" {
#include <stdio.h>
#include <stdlib.h>
}

using namespace std;

int main(int argc, char** argv) {

  int pipes[2];

  int rc = ::pipe(pipes);
  if (rc < 0) {
    return -1;
  }

  string data =
    "This data is much larger than BUFFERED_READ_SIZE, which means it will "
    "trigger multiple buffered async reads as a result.........";

  while (data.length() < 3 * 16 * 4096) {
    data.append(data);
  }

  size_t offset = 0;

  while (offset < data.length()) {
    ssize_t length =
      ::write(pipes[1], data.data() + offset, data.length() - offset);

    if (length < 0) {
      // TODO(benh): Handle a non-blocking fd? (EAGAIN, EWOULDBLOCK)
      if (errno == EINTR) {
        continue;
      }
      return -1;
    }

    offset += length;
  }

  return 0;
}

Mac信息:

Klauss-MacBook-Pro:linux klaus$ uname -a
Darwin Klauss-MacBook-Pro.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
Klauss-MacBook-Pro:linux klaus$ otool -L ./w
./w:
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
Klauss-MacBook-Pro:linux klaus$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

0 个答案:

没有答案