Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems 7 - memory

Similar presentations


Presentation on theme: "Operating Systems 7 - memory"— Presentation transcript:

1 Operating Systems 7 - memory
PIETER HARTEL

2 Memory management requirements
Logical organisation Physical organisation Relocation Protection Sharing OS+HW cooperation Store OS itself Logical: mapping modules onto linear address space so that modules can be written, compiled, linked and loaded individually Physical: primary memory is faster, secondary memory is larger, optimise the use of both means swapping Relocation: The OS must be able to fit code and data at arbitrary places in the memory and data may grow (stack, heap) Modules, stack, heap, data must be accessible to the “owning” process, and not to others Communication requires sharing, and read only code (libraries) should also be sharable Protection requires hardware support (i.e. testing bounds registers) for performance reasons

3 Linking and loading of user processes
Resolving symbolic references (why?) Collecting the modules at different times (why?) Compilers produce code with relative addresses Linker resolves external references from modules + libraries Loader prepares the image for the linear address space DLL’s are easier to share, and may save space and time if they are not needed Flexibility & performance

4 Memory partitioning techniques
Process loaded up front Static/dynamic partitioning: whole process in whole partition Segmentation: process divided into segments and memory divided into dynamic partitions Process loaded incrementally Paging: process and memory divided in equal size pages Algorithms for Placement and or compaction to reduce fragmentation Swapping when the memory is getting full More sophisticated Unequal fixed size static partitioning of the memory for whole process is simple but inflexible Variable size dynamic partitioning better but requires relocation and compaction to deal with fragmentation Modern systems use paging

5 Logical addresses vs physical addresses
Partitioning: physical address = relative address + base register Paging: page table per process mapping logical pages to physical frames Sharing? Protection? gcc Getpagesize.c ./a.out Mapping the same frame onto different pages for sharing Setting bits in the page table for protection (e.g. read only) Power of 2 for convenience int main() { printf("pagesize=%d\n", getpagesize()); return 0; }

6 ProcessLayout.c void *tproc(void *ptr) {
printf("tid=%p\n", pthread_self()); fflush(stdout); sleep(1); } int main(int argc, char * argv[]) { int i ; pthread_t thread; printf("pid=%d, tid=%p\n", getpid(), pthread_self()); for(i = 0; i < N; i++) { void *p = sbrk(M) ; pthread_create(&thread, NULL, &tproc, NULL); printf("sbrk %p\n", p); return 0; ProcessLayout.c gcc ProcessLayout.c -lpthread ./a.out ./a.out pid=27682, tid=0x7ff2d5e17700 sbrk 0x639000 sbrk 0x75a000 tid=0x7ff2d5e15700 stack=0x7ff2d5e14ea8 sbrk 0x85a000 tid=0x7ff2d stack=0x7ff2d5413ea8 sbrk 0x95a000 tid=0x7ff2d stack=0x7ff2d4011ea8 sbrk 0xa5a000 tid=0x7ff2d4a13700 stack=0x7ff2d4a12ea8 sbrk 0xb5a000 tid=0x7ff2d stack=0x7ff2d3610ea8 tid=0x7ff2d2c10700 stack=0x7ff2d2c0fea8 sbrk 0xc5a000 tid=0x7ff2d220f700 stack=0x7ff2d220eea8 sbrk 0xd5a000 tid=0x7ff2d180e700 stack=0x7ff2d180dea8 27682: ./a.out K r-x-- /home/pieter/teaching/OS/a.out K rw--- /home/pieter/teaching/OS/a.out K rw [ anon ] e K r-x-- /lib64/ld-2.12.so e21f K r---- /lib64/ld-2.12.so e K rw--- /lib64/ld-2.12.so e K rw [ anon ] e K r-x-- /lib64/libc-2.12.so e98a K /lib64/libc-2.12.so eb K r---- /lib64/libc-2.12.so eb8d K rw--- /lib64/libc-2.12.so eb8e K rw [ anon ] f K r-x-- /lib64/libpthread-2.12.so f K /lib64/libpthread-2.12.so f K r---- /lib64/libpthread-2.12.so f K rw--- /lib64/libpthread-2.12.so f K rw [ anon ] 00007ff2d0e0e K [ anon ] 00007ff2d0e0f K rw [ anon ] 00007ff2d180f K [ anon ] 00007ff2d K rw [ anon ] 00007ff2d K [ anon ] 00007ff2d K rw [ anon ] 00007ff2d2c K [ anon ] 00007ff2d2c K rw [ anon ] 00007ff2d K [ anon ] 00007ff2d K rw [ anon ] 00007ff2d K [ anon ] 00007ff2d K rw [ anon ] 00007ff2d4a K [ anon ] 00007ff2d4a K rw [ anon ] 00007ff2d K [ anon ] 00007ff2d K rw [ anon ] 00007ff2d5e K rw [ anon ] 00007fffb094f K rw [ stack ] 00007fffb09ff K r-x-- [ anon ] ffffffffff K r-x-- [ anon ] total K

7 StackLayout.c #define N 4 int *q; void bar(int e, int f) {
Low #define N 4 int *q; void bar(int e, int f) { int g = 0x1010, h = 0x0101; int *p; for(p=&f-N;p<=q;p++){ printf("%p\t%0x\n", p, *p); } void foo(int c, int d) { bar(0xeeee,0xffff); int main(int argc, char * argv[], char *envp[]) { int a = 0xaaaa, b = 0xbbbb; q = &a+N; foo(0xcccc,0xdddd); return 0; StackLayout.c stack bar f ffff e eeee p h 0101 g RBP RA High foo d dddd c cccc RBP RA Main envp argv padding argc 1 RBP RA ? $ a.out 0x7fff9a42e 0x7fff9a42e55c 0x7fff9a42e 0x7fff9a42e 0x7fff9a42e568:f ffff 0x7fff9a42e56c:e eeee 0x7fff9a42e570:p 9a42e570 0x7fff9a42e fff 0x7fff9a42e578:h 0x7fff9a42e57c:g 0x7fff9a42e a42e5a0 0x7fff9a42e fff 0x7fff9a42e 0x7fff9a42e58c 0x7fff9a42e a42e5d8 0x7fff9a42e fff 0x7fff9a42e598:d dddd 0x7fff9a42e59c:c cccc 0x7fff9a42e5a a42e5e0 0x7fff9a42e5a fff 0x7fff9a42e5a 0x7fff9a42e5ac 0x7fff9a42e5b ec96b0 0x7fff9a42e5b f11 0x7fff9a42e5b8:envp 9a42e6c8 0x7fff9a42e5bc fff 0x7fff9a42e5c0:argv 9a42e6b8 0x7fff9a42e5c fff 0x7fff9a42e5c 0x7fff9a42e5cc:argc 1 0x7fff9a42e5d a42e6b0 0x7fff9a42e5d fff 0x7fff9a42e5d8:b bbbb 0x7fff9a42e5dc:a aaaa 0x7fff9a42e5e 0x7fff9a42e5e 0x7fff9a42e5e ed8586 0x7fff9a42e5ec f11 0x40053c:bar 0x4005cc:foo 0x400602:main 0x601030:q

8 Summary Memory is a resource that can be partitioned
Requires hardware support As many processes in the memory as possible Paging divides a process image in fixed side pages, segmentation in variable sized segments. Can be combined Linux uses paging


Download ppt "Operating Systems 7 - memory"

Similar presentations


Ads by Google