PKUOS - Pintos
Pintos source browser for PKU Operating System course
mmap-write.c
Go to the documentation of this file.
1/** Writes to a file through a mapping, and unmaps the file,
2 then reads the data in the file back using the read system
3 call to verify. */
4
5#include <string.h>
6#include <syscall.h>
7#include "tests/vm/sample.inc"
8#include "tests/lib.h"
9#include "tests/main.h"
10
11#define ACTUAL ((void *) 0x10000000)
12
13void
15{
16 int handle;
17 mapid_t map;
18 char buf[1024];
19
20 /* Write file via mmap. */
21 CHECK (create ("sample.txt", strlen (sample)), "create \"sample.txt\"");
22 CHECK ((handle = open ("sample.txt")) > 1, "open \"sample.txt\"");
23 CHECK ((map = mmap (handle, ACTUAL)) != MAP_FAILED, "mmap \"sample.txt\"");
24 memcpy (ACTUAL, sample, strlen (sample));
25 munmap (map);
26
27 /* Read back via read(). */
28 read (handle, buf, strlen (sample));
29 CHECK (!memcmp (buf, sample, strlen (sample)),
30 "compare read data against written data");
31 close (handle);
32}
static char buf[BUF_SIZE]
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
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
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
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
#define ACTUAL
Writes to a file through a mapping, and unmaps the file, then reads the data in the file back using t...
Definition: mmap-write.c:11
void test_main(void)
tests/main.h
Definition: mmap-write.c:14
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
void * memcpy(void *dst_, const void *src_, size_t size)
Copies SIZE bytes from SRC to DST, which must not overlap.
Definition: string.c:7