Skip to content

close fd before calling munmap will cause the write failure #15140

Open
@Tringbtb

Description

@Tringbtb

When using emcc to compile wasm, I try to use mmap to write the file, but if you close fd before calling munmap, the written content will be lost.

Here is the example.

int len = string.size();
int fd = open("test-shared.txt", O_RDWR | O_CREAT, 00777);
lseek(fd, len - 1, SEEK_END);
write(fd, " ", 1);
char* buffer = (char*) mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
memcpy(buffer, string.data(), len);
// close(fd); // open it will lose all content, but it's ok in the linux environment.
munmap(buffer, len);
close(fd); // it's fine.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions