Cygwin上的映射文件仍会继续引起Sigbus

发布于 2025-02-03 08:16:07 字数 928 浏览 2 评论 0原文

我正在尝试将页面大小的文件mmap mmap到大于文件大小的区域,并在其中一个刻度页面引起sigbus时将其ftrunce ftruncate,以使Sigbus不再发生。

这在Linux和MacOS上很有帮助,但是在Cygwin上,即使在成功增长的Fruncate之后,我仍然会得到Sigbus。

#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void perrorAndExit(char const *Ctx){ perror(Ctx); _exit(1); }
int main(){
    long pgsz = sysconf(_SC_PAGESIZE);
    int fd = open("TMPFILE", O_RDWR|O_CREAT,0640);
    if(0>fd) perrorAndExit("open");
    if(ftruncate(fd,pgsz*1)) perrorAndExit("truncate 1 pgsz");

    char *m;
    if(MAP_FAILED==(m = mmap(0,pgsz*10,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))) perrorAndExit("mmap");
    memset(m,'=',pgsz);
    strcpy(m,"hello, world\n");
    if(ftruncate(fd,pgsz*2)) perrorAndExit("truncate 2 pgsz");
    strcpy(m+pgsz,"what is up?\n"); //still SIGBUSes on Cygwin
}

除了从较大的文件开始或在第二次截断之后创建新的映射外,还有其他解决方法吗?

I'm trying to mmap a page-size-rounded file to an area larger than the file size and ftruncate it when one of the traling pages causes a SIGBUS so that the SIGBUS no longer happens.

This works great on Linux and MacOS, but on Cygwin I keep getting a SIGBUS even after a successful growing ftruncate.

#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void perrorAndExit(char const *Ctx){ perror(Ctx); _exit(1); }
int main(){
    long pgsz = sysconf(_SC_PAGESIZE);
    int fd = open("TMPFILE", O_RDWR|O_CREAT,0640);
    if(0>fd) perrorAndExit("open");
    if(ftruncate(fd,pgsz*1)) perrorAndExit("truncate 1 pgsz");

    char *m;
    if(MAP_FAILED==(m = mmap(0,pgsz*10,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))) perrorAndExit("mmap");
    memset(m,'=',pgsz);
    strcpy(m,"hello, world\n");
    if(ftruncate(fd,pgsz*2)) perrorAndExit("truncate 2 pgsz");
    strcpy(m+pgsz,"what is up?\n"); //still SIGBUSes on Cygwin
}

Are there any workarounds for this other than starting with a larger file or creating a new mapping after the second truncate?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文