diff options
author | Robert Morris <[email protected]> | 2019-07-01 17:46:06 -0400 |
---|---|---|
committer | Robert Morris <[email protected]> | 2019-07-01 17:46:06 -0400 |
commit | abfe9999f447c15d904b3c11f32d4a22a45470a0 (patch) | |
tree | bdcf8264c9da666661b107157a6c634b86608ee2 /user | |
parent | 18e76a6c47b0f62b2458430d4357f3eb68bfd759 (diff) | |
download | xv6-labs-abfe9999f447c15d904b3c11f32d4a22a45470a0.tar.gz xv6-labs-abfe9999f447c15d904b3c11f32d4a22a45470a0.tar.bz2 xv6-labs-abfe9999f447c15d904b3c11f32d4a22a45470a0.zip |
have fork() fail, not panic, if not enough phys mem
Diffstat (limited to 'user')
-rw-r--r-- | user/cow.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/user/cow.c b/user/cow.c new file mode 100644 index 0000000..f770cc9 --- /dev/null +++ b/user/cow.c @@ -0,0 +1,54 @@ +// +// tests for copy-on-write fork() assignment. +// + +#include "kernel/types.h" +#include "kernel/memlayout.h" +#include "user/user.h" + +// allocate more than half of physical memory, +// then fork. this will fail in the default +// kernel, which does not support copy-on-write. +void +simpletest() +{ + uint64 phys_size = PHYSTOP - KERNBASE; + int sz = (phys_size / 3) * 2; + + printf(1, "simple: "); + + char *p = sbrk(sz); + if(p == (char*)0xffffffffffffffffL){ + printf(1, "sbrk(%d) failed\n", sz); + exit(); + } + + for(char *q = p; q < p + sz; q += 4096){ + *(int*)q = getpid(); + } + + int pid = fork(); + if(pid < 0){ + printf(1, "fork() failed\n"); + exit(); + } + + if(pid == 0) + exit(); + + wait(); + + if(sbrk(-sz) == (char*)0xffffffffffffffffL){ + printf(1, "sbrk(-%d) failed\n", sz); + exit(); + } + + printf(1, "simple ok\n"); +} + +int +main(int argc, char *argv[]) +{ + simpletest(); + exit(); +} |