PKUOS - Pintos
Pintos source browser for PKU Operating System course
mmap-remove.c
Go to the documentation of this file.
1/** Deletes and closes file that is mapped into memory
2 and verifies that it can still be read through the mapping. */
3
4#include <string.h>
5#include <syscall.h>
6#include "tests/vm/sample.inc"
7#include "tests/lib.h"
8#include "tests/main.h"
9
10void
12{
13 char *actual = (char *) 0x10000000;
14 int handle;
15 mapid_t map;
16 size_t i;
17
18 /* Map file. */
19 CHECK ((handle = open ("sample.txt")) > 1, "open \"sample.txt\"");
20 CHECK ((map = mmap (handle, actual)) != MAP_FAILED, "mmap \"sample.txt\"");
21
22 /* Close file and delete it. */
23 close (handle);
24 CHECK (remove ("sample.txt"), "remove \"sample.txt\"");
25 CHECK (open ("sample.txt") == -1, "try to open \"sample.txt\"");
26
27 /* Create a new file in hopes of overwriting data from the old
28 one, in case the file system has incorrectly freed the
29 file's data. */
30 CHECK (create ("another", 4096 * 10), "create \"another\"");
31
32 /* Check that mapped data is correct. */
33 if (memcmp (actual, sample, strlen (sample)))
34 fail ("read of mmap'd file reported bad data");
35
36 /* Verify that data is followed by zeros. */
37 for (i = strlen (sample); i < 4096; i++)
38 if (actual[i] != 0)
39 fail ("byte %zu of mmap'd region has value %02hhx (should be 0)",
40 i, actual[i]);
41
42 munmap (map);
43}
mapid_t mmap(int fd, void *addr)
Project 3 and optionally project 4.
Definition: syscall.c:145
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
bool remove(const char *file)
Definition: syscall.c:97
void munmap(mapid_t mapid)
Definition: syscall.c:151
#define MAP_FAILED
Definition: syscall.h:13
int mapid_t
Map region identifier.
Definition: syscall.h:12
void fail(const char *format,...)
Definition: lib.c:40
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
void test_main(void)
Deletes and closes file that is mapped into memory and verifies that it can still be read through the...
Definition: mmap-remove.c:11
int memcmp(const void *a_, const void *b_, size_t size)
Find the first differing byte in the two blocks of SIZE bytes at A and B.
Definition: string.c:53
size_t strlen(const char *string)
Returns the length of STRING.
Definition: string.c:293