Overview
Memory is one of the most important parts of a computer and without it computers would be very limited.
Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009
Some Numbers Regarding Memory Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009
Reading an integer from disk Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009
The Importance of Cache
Row major Column major 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 0 10 20 30 40 50 60 70 80 90 1 11 21 31 41 51 61 71 81 91 2 12 22 32 42 52 62 72 82 92 3 13 23 33 43 53 63 73 83 93 4 14 24 34 44 54 64 74 84 94 5 15 25 35 45 55 65 75 85 95 6 16 26 36 46 56 66 76 86 96 7 17 27 37 47 57 67 77 87 97 8 18 28 38 48 58 68 78 88 98 9 19 29 39 49 59 69 79 89 99
Row: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 etc... Col: 0 10 20 30 40 50 60 70 80 90 1 11 21 31 41 51 61 71 81 91 2 12 22 32 42 52 62 72 82 92 etc...
Access row-at-a-time Access column-at-a-time int row_major(int *array, int stride) { int i, j, index = 0, count = 0; printf("Row major:\n"); for (i = 0; i < stride; i++) { for (j = 0; j < stride; j++) { index = i * stride + j; array[index] = count++; } } return index; } int col_major(int *array, int stride) { int i, j, index = 0, count = 0; printf("Col major:\n"); for (i = 0; i < stride; i++) { for (j = 0; j < stride; j++) { index = j * stride + i; array[index] = count++; } } return index; }
With optimization (gcc -O2 mem1.c)
Access row-at-a-time Access column-at-a-time time ./mem1 30000 0 stride = 30000, total = 900000000, fn = 0 Row major: real 0m5.406s user 0m4.080s sys 0m1.290s time ./mem1 30000 1 stride = 30000, total = 900000000, fn = 1 Column major: real 0m29.360s user 0m27.850s sys 0m1.460s
Access row-at-a-time Access column-at-a-time time ./mem1 30000 0 stride = 30000, total = 900000000, fn = 0 Row major: real 0m1.500s user 0m0.340s sys 0m1.140s time ./mem1 30000 1 stride = 30000, total = 900000000, fn = 1 Column major: real 0m28.022s user 0m26.560s sys 0m1.420s
Physical vs. Logical Addresses
Physical memory
/* address.c */ #include <stdio.h> int global = 5; int main(void) { int local = 10; static int st = 20; printf("address of global is %p, value is %i\n", (void*)&global, global); printf("address of local is %p, value is %i\n", (void*)&local, local); printf("address of static is %p, value is %i\n", (void*)&st, st); getchar(); return 0; } |
Output:
3 instances running on Windows XP (32-bit) | 3 instances running on Linux (64-bit) | |
---|---|---|
address of global is 0040D000, value is 5 address of local is 0012FF74, value is 10 address of static is 0040D004, value is 20 address of global is 0040D000, value is 5 address of local is 0012FF74, value is 10 address of static is 0040D004, value is 20 address of global is 0040D000, value is 5 address of local is 0012FF74, value is 10 address of static is 0040D004, value is 20 |
address of global is 0x601028, value is 5 address of local is 0x7fff039dd77c, value is 10 address of static is 0x60102c, value is 20 address of global is 0x601028, value is 5 address of local is 0x7fff8cdabdfc, value is 10 address of static is 0x60102c, value is 20 address of global is 0x601028, value is 5 address of local is 0x7fffb193203c, value is 10 address of static is 0x60102c, value is 20 |
3 instances running on Windows 7 (64-bit) |
---|
address of global is 000000013FCF5000, value is 5 address of local is 000000000017F9E0, value is 10 address of static is 000000013FCF5004, value is 20 address of global is 000000013F175000, value is 5 address of local is 00000000001AFA80, value is 10 address of static is 000000013F175004, value is 20 address of global is 000000013F685000, value is 5 address of local is 00000000002AF7C0, value is 10 address of static is 000000013F685004, value is 20 |
Dynamic Loading
#include <stdio.h>
void printme(const char *str)
{
printf("Hello from library: %s\n", str);
}
int add(int a, int b)
{
return a + b;
}
This can easily be statically linked with main1.c
#include <stdio.h>
void printme(const char *str);
int add(int a, int b);
int main(void)
{
printme("This function is statically linked.");
printf("The sum of 5 and 10 is %i.\n", add(5, 10));
return 0;
}
Output:gcc main1.c hello.c -o hello-static
Hello from library: This function is statically linked. The sum of 5 and 10 is 15.
gcc -fPIC -c hello.c -o hello.o gcc -shared hello.o -o libhello.so gcc main.c -o main -ldl
May need the entire path to the linker (if GCC's linker is in the path first). For example:cl /c hello.c /Fohello.obj link /dll /def:hello.def hello.obj /out:hello.dll cl wmain.c /Fewmain.exe
The module definition file is a simple text file that looks like this:"C:\Program Files\Microsoft Visual Studio 10.0\vc\bin\link" /dll /def:hello.def hello.obj /out:hello.dll
EXPORTS printme add
Stack and Heap
float *foo(int count) { int i; float *array = malloc(count * sizeof(float)); for (i = 0; i < count; i++) array[i] = sqrt((float)i); return array; }
Paging
Page tablesPage tables
![]()
Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009
Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009
Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009
0 | 1234h 1 | 0234h 2 | 8234h 3 | 4234h
Virtual Memory
Virtual memory allows us to pretend that we have (almost) unlimited amounts of memory.Virtual memory space
7, 0, 1, 2, 0, 3, 0, 2, 0, 4, 0, 2
7 fault [7 ] 0 fault [7 0 ] 1 fault [7 0 1] 2 fault [0 1 2] 0 ok [0 1 2] 3 fault [1 2 3] 0 fault [2 3 0] 2 ok [2 3 0] 0 ok [2 3 0] 4 fault [3 0 4] 0 ok [3 0 4] 2 fault [0 4 2]
Operating System Internals and Desgin Principles - 5th Edition William Stallings ©2005
7, 0, 1, 2, 0, 3, 0, 2, 0, 4, 0, 2
7 fault [7+ ] 0 fault [7+ 0+ ] 1 fault [7+ 0+ 1+] 2 fault [2+ 0- 1-] 0 ok [2+ 0+ 1-] 3 fault [2+ 0- 3+] 0 ok [2+ 0+ 3+] 2 ok [2+ 0+ 3+] 0 ok [2+ 0+ 3+] 4 fault [4+ 0- 3-] 0 ok [4+ 0+ 3-] 2 fault [4+ 0- 2+]
7, 0, 1, 2, 0, 3, 0, 2, 0, 4, 0, 2
Time 7 fault [7(0) ] 0 0 fault [7(0) 0(1) ] 1 1 fault [7(0) 0(1) 1(2)] 2 2 fault [0(1) 1(2) 2(3)] 3 0 ok [1(2) 2(3) 0(4)] 4 3 fault [2(3) 0(4) 3(5)] 5 0 ok [2(3) 3(5) 0(6)] 6 2 ok [3(5) 0(6) 2(7)] 7 0 ok [3(5) 2(7) 0(8)] 8 4 fault [2(7) 0(8) 4(9)] 9 0 ok [2(7) 4(9) 0(10)] 10 2 ok [4(9) 0(10) 2(11)] 11