1063a092fSIngo Weinhold#include <errno.h>
2063a092fSIngo Weinhold#include <stdio.h>
3063a092fSIngo Weinhold#include <string.h>
4f9eba888SStefano Ceccherini#include <stdlib.h>
5063a092fSIngo Weinhold#include <sys/wait.h>
6063a092fSIngo Weinhold#include <unistd.h>
7063a092fSIngo Weinhold
8063a092fSIngo Weinhold#include <OS.h>
9063a092fSIngo Weinhold
10063a092fSIngo Weinhold
11063a092fSIngo Weinholdstatic const int kAreaPagesCount = 4 * 1024;	// 16 MB
12063a092fSIngo Weinhold
13063a092fSIngo Weinhold
14063a092fSIngo Weinholdint
15063a092fSIngo Weinholdmain()
16063a092fSIngo Weinhold{
17063a092fSIngo Weinhold	while (true) {
18063a092fSIngo Weinhold		uint8* address;
19063a092fSIngo Weinhold		area_id area = create_area("test area", (void**)&address, B_ANY_ADDRESS,
20063a092fSIngo Weinhold			kAreaPagesCount * B_PAGE_SIZE, B_NO_LOCK,
21063a092fSIngo Weinhold			B_READ_AREA | B_WRITE_AREA);
22063a092fSIngo Weinhold		if (area < 0) {
23063a092fSIngo Weinhold			fprintf(stderr, "Creating the area failed: %s", strerror(area));
24063a092fSIngo Weinhold			exit(1);
25063a092fSIngo Weinhold		}
26063a092fSIngo Weinhold
27063a092fSIngo Weinhold		// touch half of the pages
28063a092fSIngo Weinhold		for (int i = 0; i < kAreaPagesCount / 2; i++)
29063a092fSIngo Weinhold			address[i * B_PAGE_SIZE] = 42;
30063a092fSIngo Weinhold
31063a092fSIngo Weinhold		// fork
32063a092fSIngo Weinhold		pid_t child = fork();
33063a092fSIngo Weinhold		if (child < 0) {
34063a092fSIngo Weinhold			perror("fork() failed");
35063a092fSIngo Weinhold			exit(1);
36063a092fSIngo Weinhold		}
37063a092fSIngo Weinhold
38063a092fSIngo Weinhold		if (child == 0) {
39063a092fSIngo Weinhold			// child
40063a092fSIngo Weinhold
41063a092fSIngo Weinhold			// delete the copied area
42063a092fSIngo Weinhold			delete_area(area_for(address));
43063a092fSIngo Weinhold
44063a092fSIngo Weinhold			exit(0);
45063a092fSIngo Weinhold		}
46063a092fSIngo Weinhold
47063a092fSIngo Weinhold		// parent
48063a092fSIngo Weinhold
49063a092fSIngo Weinhold		// touch the other half of the pages
50063a092fSIngo Weinhold		for (int i = kAreaPagesCount / 2; i < kAreaPagesCount; i++)
51063a092fSIngo Weinhold			address[i * B_PAGE_SIZE] = 42;
52063a092fSIngo Weinhold
53063a092fSIngo Weinhold		int status;
54063a092fSIngo Weinhold		while (wait(&status) != child);
55063a092fSIngo Weinhold
56063a092fSIngo Weinhold		delete_area(area);
57063a092fSIngo Weinhold	}
58063a092fSIngo Weinhold
59063a092fSIngo Weinhold	return 0;
60063a092fSIngo Weinhold}
61