diff --git a/week08/ex1.txt b/week08/ex1.txt new file mode 100644 index 0000000..f60f894 --- /dev/null +++ b/week08/ex1.txt @@ -0,0 +1,4 @@ + total used free shared buff/cache available +Mem: 14Gi 2.4Gi 10Gi 88Mi 2.0Gi 11Gi +Swap: 8.0Gi 0B 8.0Gi +Total: 22Gi 2.4Gi 18Gi diff --git a/week08/ex2 b/week08/ex2 new file mode 100755 index 0000000..1649abd Binary files /dev/null and b/week08/ex2 differ diff --git a/week08/ex2.c b/week08/ex2.c new file mode 100644 index 0000000..9259867 --- /dev/null +++ b/week08/ex2.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +#define TENMB 10*1024*1024 + +/* vmstat 1 + * Inconsistently, probably because timings are not ideal, + * but every second free memory decreases by approximately 10MB. + * si and so are 0, because swap memory wasn't being used. + */ + +/* top -d 1 + * By pressing M, top sorts processes by memory usage. + * Compared to vmstat, top updates total memory usage + * slower. Every second ex2 goes to the top of the 'top'. + */ + + +int main() { + char *p[10]; + for (int i = 0; i < 10; i++) { + p[i] = malloc(TENMB); + memset(p[i], 0, TENMB); + sleep(1); + } + for (int i = 0; i < 10; i++) { + free(p[i]); + } +} diff --git a/week08/ex2.sh b/week08/ex2.sh new file mode 100755 index 0000000..0e24fa6 --- /dev/null +++ b/week08/ex2.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +gcc ex2.c -o ex2 +./ex2 & +vmstat 1 diff --git a/week08/ex3.sh b/week08/ex3.sh new file mode 100755 index 0000000..430af44 --- /dev/null +++ b/week08/ex3.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +gcc ex2.c -o ex2 +./ex2 & +top -d 1 diff --git a/week08/ex4 b/week08/ex4 new file mode 100755 index 0000000..8144217 Binary files /dev/null and b/week08/ex4 differ diff --git a/week08/ex4.c b/week08/ex4.c new file mode 100644 index 0000000..cce58cb --- /dev/null +++ b/week08/ex4.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#define TENMB 10*1024*1024 + +int main() { + char *p[10]; + for (int i = 0; i < 10; i++) { + p[i] = malloc(TENMB); + memset(p[i], 0, TENMB); + struct rusage usage; + if (getrusage(RUSAGE_SELF, &usage) == 0) { + printf("Usage: %ldKB\n", usage.ru_maxrss); + } else { + perror("getrusage"); + exit(1); + } + sleep(1); + } + for (int i = 0; i < 10; i++) { + free(p[i]); + } +} diff --git a/week08/ex4.sh b/week08/ex4.sh new file mode 100755 index 0000000..8dd0309 --- /dev/null +++ b/week08/ex4.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex4.c -o ex4 +./ex4 diff --git a/week08/ex5.txt b/week08/ex5.txt new file mode 100644 index 0000000..5a9aa62 --- /dev/null +++ b/week08/ex5.txt @@ -0,0 +1,3 @@ +Physical address is the actual address in the main memory, whereas virtual address is a logical one that is generated by CPU's MMU. +Virtual address points to the virtual memory, CPU and OS will take that address and convert it to the physical one to access the actual data. +All programs running on OS use virtual addresses, since they don't have access to the physical memory. diff --git a/week08/ex6.txt b/week08/ex6.txt new file mode 100644 index 0000000..ec3abf8 --- /dev/null +++ b/week08/ex6.txt @@ -0,0 +1,4 @@ +16-bit address can access 2^16 = 65536 bytes of memory +8KB is 8192 bytes. +65536 / 8192 = 8 pages +Since there are 8 pages, there are 8 table entries.