Browse Source

week8

master
RinRi 1 year ago
parent
commit
32375728b0
10 changed files with 82 additions and 0 deletions
  1. +4
    -0
      week08/ex1.txt
  2. BIN
      week08/ex2
  3. +31
    -0
      week08/ex2.c
  4. +5
    -0
      week08/ex2.sh
  5. +5
    -0
      week08/ex3.sh
  6. BIN
      week08/ex4
  7. +26
    -0
      week08/ex4.c
  8. +4
    -0
      week08/ex4.sh
  9. +3
    -0
      week08/ex5.txt
  10. +4
    -0
      week08/ex6.txt

+ 4
- 0
week08/ex1.txt View File

@@ -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

BIN
week08/ex2 View File


+ 31
- 0
week08/ex2.c View File

@@ -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]);
}
}

+ 5
- 0
week08/ex2.sh View File

@@ -0,0 +1,5 @@
#!/bin/sh

gcc ex2.c -o ex2
./ex2 &
vmstat 1

+ 5
- 0
week08/ex3.sh View File

@@ -0,0 +1,5 @@
#!/bin/sh

gcc ex2.c -o ex2
./ex2 &
top -d 1

BIN
week08/ex4 View File


+ 26
- 0
week08/ex4.c View File

@@ -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]);
}
}

+ 4
- 0
week08/ex4.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex4.c -o ex4
./ex4

+ 3
- 0
week08/ex5.txt View File

@@ -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.

+ 4
- 0
week08/ex6.txt View File

@@ -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.

Loading…
Cancel
Save