summaryrefslogtreecommitdiffstats
path: root/test_mmap.c
blob: 15c962da3e8d27a05bbce8411bb66702a6592a28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[])
{
  char buffer[16384];
  char *mapping;
  int fd;

  /* Test of anonymous mappings */

  mapping = mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  mmap(mapping+8192, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 0, 0);
  strcpy(mapping+0, "Foo mani padme hum");
  strcpy(mapping+4096, "Foo mani padme hum");
  errno = 0;
  mmap(mapping+4096, 4096, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  munmap(mapping, 16384);

  /* Test of non-anonymous private mappings */
  fd = open("mmaptest.dat", O_RDWR|O_TRUNC|O_CREAT, 0600);
  memset(buffer, '#', 16384);
  write(fd, buffer, 16384);

  mapping = mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  strcpy(mapping+4096, "Foo mani padme hum");

  mmap(mapping+8192, 8192, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE, fd, 8192);

  mmap(mapping+4096, 4096, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE, fd, 4096);
  
  munmap(mapping, 16384);
  close(fd);

  return 0;
}