如何使用mmap
将字符设备文件映射到我的应用程序?我尝试过以下代码失败,因为文件大小为零(sb.st_size == 0
)。还有另外一种方式还是一起不可能?感谢。
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int init_module(void *module_image, unsigned long len,
const char *param_values);
int main() {
int res = 0;
void *buf = 0;
struct stat sb;
int rc = 0;
int fd = open("/dev/nvidia0", O_RDWR);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
res = fstat(fd, &sb);
if (res == -1) {
perror("fstat");
exit(EXIT_FAILURE);
}
buf = mmap(0, sb.st_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
munmap(buf, sb.st_size);
close(fd);
return EXIT_SUCCESS;
}