forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_clone.c
48 lines (40 loc) · 992 Bytes
/
test_clone.c
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
41
42
43
44
45
46
47
48
/* Call clone and spin until child thread has changed global variable. Verifies that address space is shared. */
#include "types.h"
#include "user.h"
#undef NULL
#define NULL ((void*)0)
int ppid;
#define PGSIZE (4096)
volatile int global = 1;
#define assert(x) if (x) {} else { \
printf(1, "%s: %d ", __FILE__, __LINE__); \
printf(1, "assert failed (%s)\n", # x); \
printf(1, "TEST FAILED\n"); \
kill(ppid); \
exit(); \
}
void worker(void *arg_ptr);
int
main(int argc, char *argv[])
{
ppid = getpid();
void *stack = malloc(PGSIZE*2);
assert(stack != NULL);
if((uint)stack % PGSIZE) {
stack = stack + (PGSIZE - (uint)stack % PGSIZE);
}
int clone_pid = clone(worker, 0, stack);
assert(clone_pid > 0);
while(global != 5) {
; // wait for thread worker to change global
}
printf(1, "global: %d\n", global);
printf(1, "TEST PASSED\n");
exit();
}
void
worker(void *arg_ptr) {
assert(global == 1);
global = 5;
exit();
}