OSX 10.6 上的 pwrite() 不支持 64 位偏移量

发布于 2024-12-18 14:17:06 字数 1752 浏览 9 评论 0原文

当尝试写入超过 2GB 的偏移量时,我在 OSX 10.6.8 上的 pwrite() 似乎遇到了一个奇怪的问题。我不断捕获 SIGXFSZ 信号,这意味着已超出文件大小限制。忽略该信号并没有帮助,因为 pwrite() 将简单地返回 EFBIG

OSX 似乎不支持显式的 open64()pwrite64() 函数。看来 sizeof(off_t) 的大小是正确的 8 字节,这意味着 pwrite() 应该采用 64 位偏移量。调用 open() 时是否缺少一个标志,或者我应该将文件描述符传递给 fcntl() 以启用大型- 文件支持?

最后,当我使用 RLIMIT_FSIZE 选项检查 getrlimit() 时,它表示当前文件大小限制和最大文件大小限制均为 9223372036854775807字节。所以这似乎并没有阻止我通过 pwrite() 写入大文件。

还有其他人在 64 位 OSX 下遇到过 pwrite() 问题吗?


编辑:根据请求,我添加调用pwrite()的代码...请注意,此代码位于写入线程内:

for (int i=0; i < data->iterations; i++)
{
    unsigned char* ptr = data->buffer;
    int temp_buff_size = data->buff_size;
    int offset = i * data->buff_size;

    while(temp_buff_size > 0)
    {
        int temp_bytes_written = pwrite(data->fd, ptr, temp_buff_size, offset);

        if (temp_bytes_written >= 0)
        {
            temp_buff_size -= temp_bytes_written;
            ptr += temp_bytes_written;
            offset += temp_bytes_written;
        }
        else if (errno == EINTR)
        {
            continue;
        }
        else
        {
            perror("Write thread exiting");
            write_thread_finished = 1;
                return (void*)-1;
        }
    }
}

我在内部调用它一个循环,因为据我了解,不能保证 pwrite() 写入所请求的所有数据,因此我需要确保我请求写入的数据实际上已写入磁盘,并且如果写入的字节数较少,然后我正确地偏移到我正在写入的缓冲区中将缓冲区的其余部分写入磁盘。 data->iterations 的作用只是向线程传递有关将缓冲区写入磁盘多少次的信息......这是带宽测试的一部分,所以我尝试编写一个大文件,看看它写入磁盘的速度有多快。唯一的问题是我无法将大于 2GB 的偏移量传递给 pwrite()

I seem to be having an odd problem with pwrite() on OSX 10.6.8 when attempting to write to an offset beyond 2GB. I keep catching a SIGXFSZ signal, meaning that the file-size limit has been exceeded. Ignoring the signal doesn't help because pwrite() will then simply return EFBIG.

It seems that OSX does not support the explicit open64() and pwrite64() functions. It also seems that sizeof(off_t) is a proper 8-bytes in size, meaning that pwrite() should take a 64-bit offset. Is there a flag I'm missing when calling open(), or some OSX-specific setting I should be passing for my file-descriptor to fcntl() to enable large-file support?

Finally, when I check getrlimit(), with the RLIMIT_FSIZE option, it says that both the current and the max file-size limit is 9223372036854775807 bytes. So that doesn't seem to be preventing me from writing large files via pwrite().

Has anyone else had problems with pwrite() under 64-bit OSX?


EDIT: Per request, I'm adding the code that calls pwrite() ... note that this code is inside a write-thread:

for (int i=0; i < data->iterations; i++)
{
    unsigned char* ptr = data->buffer;
    int temp_buff_size = data->buff_size;
    int offset = i * data->buff_size;

    while(temp_buff_size > 0)
    {
        int temp_bytes_written = pwrite(data->fd, ptr, temp_buff_size, offset);

        if (temp_bytes_written >= 0)
        {
            temp_buff_size -= temp_bytes_written;
            ptr += temp_bytes_written;
            offset += temp_bytes_written;
        }
        else if (errno == EINTR)
        {
            continue;
        }
        else
        {
            perror("Write thread exiting");
            write_thread_finished = 1;
                return (void*)-1;
        }
    }
}

I'm calling it inside a loop since it's my understanding that pwrite() is not guaranteed to write all the data requested, and so I need to make sure that the data I am requesting be written is actually written to disk, and if less bytes are written, then I properly offset into the buffer I'm writing to get the rest of the buffer onto the disk. The role of data->iterations is just passing to the thread information on how many times to write the buffer to disk ... this is part of a bandwidth test, so I'm trying to write a large file to see how fast it can be written to disk. The only problem is I'm not able to pass an offset larger than 2GB to pwrite().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2024-12-25 14:17:06

在 OSX 上,即使在 64 位系统上,int 仍然保持为 32 位。因此,当有符号整数翻转为负数时,您尝试以负偏移量写入。

编辑:根据手册,正确的类型是 off_t,无论底层操作系统是 32 位还是 64 位,它都应该具有正确的大小和符号。

On OSX int is still kept at 32 bits even on 64-bit systems. So when the signed integer rolls over to negative, you try to write at a negative offset.

Edit: The correct type, as per the manual, is off_t which should be of correct size and signedness whether the underlying OS is 32 or 64 bits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文