From 0387e2156f10130148a44d9389393ec537e3b134 Mon Sep 17 00:00:00 2001 From: Frans Kaashoek Date: Wed, 24 Jul 2019 08:37:26 -0400 Subject: Add a few sbrktest for lazy allocatioin lab --- user/usertests.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'user') diff --git a/user/usertests.c b/user/usertests.c index 42065f4..3d8786c 100644 --- a/user/usertests.c +++ b/user/usertests.c @@ -1577,6 +1577,8 @@ sbrktest(void) int i, fds[2], pids[10], pid, ppid; char *c, *oldbrk, scratch, *a, *b, *lastaddr, *p; uint64 amt; + int fd; + int n; #define BIG (100*1024*1024) printf(stdout, "sbrk test\n"); @@ -1707,6 +1709,50 @@ sbrktest(void) exit(); } + // test running fork with the above allocated page + ppid = getpid(); + pid = fork(); + if(pid < 0){ + printf(stdout, "fork failed\n"); + exit(); + } + + // test out of memory during sbrk + if(pid == 0){ + // allocate a lot of memory + a = sbrk(0); + sbrk(10*BIG); + int n = 0; + for (i = 0; i < 10*BIG; i += 4096) { + n += *(a+i); + } + printf(stdout, "allocate a lot of memory succeeded %d\n", n); + kill(ppid); + exit(); + } + wait(); + + // test reads from allocated memory + a = sbrk(4096); + fd = open("sbrk", O_CREATE|O_WRONLY); + unlink("sbrk"); + if(fd < 0) { + printf(stdout, "open sbrk failed\n"); + exit(); + } + if ((n = write(fd, a, 10)) < 0) { + printf(stdout, "write sbrk failed\n"); + exit(); + } + close(fd); + + // test writes to allocated memory + a = sbrk(4096); + if(pipe((int *) a) != 0){ + printf(1, "pipe() failed\n"); + exit(); + } + if(sbrk(0) > oldbrk) sbrk(-(sbrk(0) - oldbrk)); -- cgit v1.2.3