@@ -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 |
@@ -0,0 +1,31 @@ | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include <unistd.h> | |||||
#include <string.h> | |||||
#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]); | |||||
} | |||||
} |
@@ -0,0 +1,5 @@ | |||||
#!/bin/sh | |||||
gcc ex2.c -o ex2 | |||||
./ex2 & | |||||
vmstat 1 |
@@ -0,0 +1,5 @@ | |||||
#!/bin/sh | |||||
gcc ex2.c -o ex2 | |||||
./ex2 & | |||||
top -d 1 |
@@ -0,0 +1,26 @@ | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include <unistd.h> | |||||
#include <string.h> | |||||
#include <sys/resource.h> | |||||
#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]); | |||||
} | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex4.c -o ex4 | |||||
./ex4 |
@@ -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. |
@@ -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. |