使用mmap和memcpy时遇到问题

9
尝试使用这些函数复制文件,一切都很顺利,直到程序遇到memcpy函数,它会产生总线错误并终止进程。
void copy_mmap(char* in, char* out){

int input_fd, output_fd;

input_fd = open (in, O_RDONLY);
if (input_fd == -1) {
        printf("Error opening input file.\n");
        exit(2);
}

output_fd = open(out, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
if(output_fd == -1){
    printf("Error opening output file.\n");
    exit(3);
}

struct stat st;
fstat(input_fd, &st);

char* target;
target=mmap(0, st.st_size+1, PROT_READ, MAP_SHARED, input_fd, 0);
if (target==(void*) -1){
    printf("Error mapping target with errno: %d.\n", errno);
    exit(6);
}


char* destination;
destination=mmap(0, st.st_size+1, PROT_READ | PROT_WRITE, MAP_SHARED, output_fd, 0);
if (destination==(void*) -1){
    printf("Error mapping destination with errno: %d.\n", errno);
    exit(5);
}

memcpy(destination, target, st.st_size);
munmap(destination, st.st_size);



}

无法确定问题出在哪里,因为“总线错误”不是一个描述性的错误消息,并且互联网上关于这个问题的资料非常少。

3个回答

22
当你将目标文件创建为一个新文件时,它的大小为0字节。memcpy会崩溃,因为它试图写入超出文件末尾的数据。

mmap()之前,您可以通过使用ftruncate()来预先调整目标文件的大小与源文件相同以使其工作。
此外,应将st.st_size作为mmap的第二个参数传递,而不是st.st_size+1st.st_size+1 尝试映射一个大于文件大小的范围,这是无效的。

如果您非常关注速度并且不介意编写特定于操作系统的代码,那么您可以使用存在于Unix(类似)操作系统上的不同sendfile变体。此文件在内核空间中执行整个复制操作(没有数据来回复制到用户空间):http://linux.die.net/man/2/sendfile http://www.freebsd.org/cgi/man.cgi?query=sendfile&sektion=2 https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/sendfile.2.html - panzi

1
你也可以使用 lseek 函数将文件指针定位到文件末尾(off_t fileposition= lseek(output_fd, st.st_size-1, SEEK_SET)),并向 output_fd 写入一个空字符。

是的!在使用mmap()之前,请执行以下操作:lseek(d, size - 1, SEEK_SET);write(d, &d, 1);其中d是您的文件描述符(使用open()函数获取)。 - vytaute

0

或许尝试使用memmove?你是不是从同一块内存/文件/设备位置读写数据?在这种情况下,memcpy会失败。


1
只有当源数组和目标数组重叠时,memcpy()才会出现问题(可以通过使用memmove()来解决)。由于源和目标是通过两个独立的、成功调用mmap()并将空指针作为第一个参数分配的,它们不可能重叠。 - Keith Thompson
@KeithThompson 我怀疑没有通过mmap分配任何内存,它只是创建到指定位置的映射。如果这些是相同的文件,我仍然相信memmove可能会有所帮助。 - Dariusz
1
无论你是否称之为分配,它都会为程序提供一系列(虚拟)内存地址。由两个对mmap()的调用所提供的两个范围不能重叠(如果mmap()没有出问题的话),因此memmove()是无法帮助的。更进一步的证据是:楼主尝试了它但没有帮助,而Celada的答案有效。 - Keith Thompson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接