mmap为虚拟内存映射函数
#include <unistd.h>
#include <sys/mman.h>
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
例程:
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int mmapcopy(int fd, int size) {
char *buf;
buf = (char *)mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
fwrite(buf, 1, size, stdout);
return 0;
}
int main(int argc,char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
int fd;
struct stat state;
fd = open(argv[1], O_RDONLY);
if (fd == -1) {
fprintf(stderr, "open failure");
exit(EXIT_FAILURE);
}
fstat(fd, &state);
mmapcopy(fd, state.st_size);
}
~