update lab net

This commit is contained in:
Ivy Wu 2024-10-10 22:49:32 -04:00
parent 10584ccdde
commit 0b94a6a45f
2 changed files with 77 additions and 0 deletions

View File

@ -50,6 +50,10 @@ def test_nettest_():
def test_nettest_():
r.match('^dns: OK$')
@test(10, "nettest: free", parent=test_nettest)
def test_nettest_():
r.match('^free: OK$')
#@test(10, "answers-net.txt")
#def test_answers():
# # just a simple sanity check, will be graded manually

View File

@ -830,6 +830,72 @@ usage()
exit(1);
}
//
// use sbrk() to count how many free physical memory pages there are.
// touches the pages to force allocation.
// because out of memory with lazy allocation results in the process
// taking a fault and being killed, fork and report back.
//
int
countfree()
{
int fds[2];
if(pipe(fds) < 0){
printf("pipe() failed in countfree()\n");
exit(1);
}
int pid = fork();
if(pid < 0){
printf("fork failed in countfree()\n");
exit(1);
}
if(pid == 0){
close(fds[0]);
while(1){
uint64 a = (uint64) sbrk(4096);
if(a == 0xffffffffffffffff){
break;
}
// modify the memory to make sure it's really allocated.
*(char *)(a + 4096 - 1) = 1;
// report back one more page.
if(write(fds[1], "x", 1) != 1){
printf("write() failed in countfree()\n");
exit(1);
}
}
exit(0);
}
close(fds[1]);
int n = 0;
while(1){
char c;
int cc = read(fds[0], &c, 1);
if(cc < 0){
printf("read() failed in countfree()\n");
exit(1);
}
if(cc == 0)
break;
n += 1;
}
close(fds[0]);
wait((int*)0);
return n;
}
int
main(int argc, char *argv[])
{
@ -856,6 +922,8 @@ main(int argc, char *argv[])
//
// "python3 nettest.py grade" must already be running...
//
int free0 = countfree();
int free1 = 0;
txone();
sleep(2);
ping0();
@ -868,6 +936,11 @@ main(int argc, char *argv[])
sleep(2);
dns();
sleep(2);
if ((free1 = countfree()) + 32 < free0) {
printf("free: FAILED -- lost too many free pages %d (out of %d)\n", free1, free0);
} else {
printf("free: OK\n");
}
} else if(strcmp(argv[1], "dns") == 0){
dns();
} else {