Last active
October 5, 2021 14:02
-
-
Save andyrudoff/87f2c42d6f12acd4e54e6c663091a328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* example calling mmap on a file starting at offset 4096. the | |
* size mapped is the size of the file minus the 4096 bytes that | |
* were skipped at the beginning. | |
*/ | |
#include <sys/mman.h> | |
#include <sys/stat.h> | |
#include <err.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
int fd; | |
struct stat stbuf; | |
char *pmaddr; | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s filename\n", argv[0]); | |
exit(1); | |
} | |
if ((fd = open(argv[1], O_RDWR)) < 0) | |
err(1, "open: %s", argv[1]); | |
if (fstat(fd, &stbuf) < 0) | |
err(1, "fstat: %s", argv[1]); | |
/* start mapping at offset 4k to force 4k pages */ | |
if ((pmaddr = (char *)mmap(NULL, stbuf.st_size - 4096, | |
PROT_READ|PROT_WRITE, | |
MAP_SHARED, fd, 4096)) == MAP_FAILED) | |
err(1, "mmap: %s", argv[1]); | |
close(fd); | |
/* now pmaddr points to the mapped area... */ | |
printf("done.\n"); | |
exit(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Makefile for force_4k example | |
# | |
PROGS = force_4k | |
CFLAGS = -g -Wall -Werror -std=gnu99 | |
all: $(PROGS) | |
force_4k: force_4k.o | |
$(CC) -o $@ $(CFLAGS) $^ $(LIBS) | |
clean: | |
$(RM) *.o a.out core | |
clobber: clean | |
$(RM) $(PROGS) force_4k | |
.PHONY: all clean clobber |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment