Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 28 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
28
Dung lượng
711,7 KB
Nội dung
Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology CO2018 Operating Systems Process (cont’d) Autumn, 2016 Nguyen Duc Hai Objective • Know how data are layout inside a process • Understand dynamic memory allocation mechanism Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Reading materials • Randal E Bryant, David R O’Hallaron, Computer Systems: A Programmer's Perspective, Chapter • Avi Silberschatz, Peter Baer Galvin, Greg Gagne, Operating System Concepts, Chapter Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Each process has its own address space which is a set of memory addresses that a process can access 0xffffffff • Address space is a large array of bytes starting at and going up to some large number (232-1 or 264-1) • The address space is split into multiple parts, each part holds different parts of the process • • • • Text Data Heap Stack 0x00000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Exercise: Given following program, identify the memory segments which variables (or function) belongs to • global_f • local_f • func • dynamic_f Autumn 2016 #include #include int global_f = 10; int func() { return 0; } int main() { int local_f = 100; int *dynamic_f = (int*)malloc(sizeof(int)); printf(“global_f: %p\n”, &global_f); printf(“local_f: %p\n”, &local_f); printf(“func: %p\n”, &func); printf(“dynamic_f: %p\n”, dynamic_f); } FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Exercise: Compile and run this program multiple times then identify the location of those variables and functions on address space Autumn 2016 #include #include int global_f = 10; int func() { return 0; } int main() { int local_f = 100; int *dynamic_f = (int*)malloc(sizeof(int)); printf(“global_f: %p\n”, &global_f); printf(“local_f: %p\n”, &local_f); printf(“func: %p\n”, &func); printf(“dynamic_f: %p\n”, dynamic_f); } FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Sample output: 0xffffffffffffffff global_f: 0x600984 local_f: 0x7fffd2939774 func: 0x400504 dynamic_f: 0x2499010 0x0000000000000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Sample output: 0xffffffffffffffff global_f: 0x600984 local_f: 0x7fffd2939774 func: 0x400504 dynamic_f: 0x2499010 global_f 0x0000000000000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Sample output: 0xffffffffffffffff global_f: 0x600984 local_f: 0x7fffd2939774 func: 0x400504 local_f dynamic_f: 0x2499010 global_f 0x0000000000000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Inside a process • Sample output: 0xffffffffffffffff global_f: 0x600984 local_f: 0x7fffd2939774 func: 0x400504 local_f dynamic_f: 0x2499010 global_f func 0x0000000000000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 10 Inside a process • Sample output: 0xffffffffffffffff global_f: 0x600984 local_f: 0x7fffd2939774 func: 0x400504 dynamic_f: 0x2499010 Stack local_f dynamic_f Heap Data global_f func 0x0000000000000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 14 Inside a process • Sample output: 0xffffffffffffffff global_f: 0x600984 local_f: 0x7fffd2939774 func: 0x400504 dynamic_f: 0x2499010 Stack local_f dynamic_f Heap Data global_f func Text 0x0000000000000000 Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 15 Inside a process • Check again: Open file /proc//maps to see the actual memory segments used by the process (Hint: Add sleep at the end of main function to make it run longer and add an “&” at the end of execution command to see the PID of the process) • Wait!!!! File? Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 16 Inside a process • Process address space in Linux Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 17 Heap region • The heap is placed right after data region in address space • break (program break) separates used regions and unused regions • Heap increases upward but the break cannot go through rlimit Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 18 Heap region • We could increase or decrease the size of heap region by using two system calls: • brk changes the value of break pointer • sbrk increases the value of break by a given number of bytes • We could use those system calls to implement malloc and free Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 19 Heap region • A simple implementation of malloc void *simple_malloc(size_t size) { void *p = sbrk(0); if (sbrk(size) == (void*)-1) { return NULL; } return p; } • Is there any problem with this implementation? Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 20 Heap region • Exercise: How to implement malloc so that given the pointer returned from this function, we could easily identify the size of memory regions pointed by this pointer to write free function? Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 21 Dynamic allocation • Actually, malloc does not allocate a new memory region whose size exactly equals to the size given by user It allocates a few extra bytes to hold needed information (including the size of allocated region) so that free function could easily clean this region Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 22 Dynamic allocation • How to allocate and clean memory region on heap? void *p; p = malloc(100); Used Unused // something free(p); Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 23 Dynamic allocation • How to allocate and clean memory region on heap? void *p; p = malloc(100); Used 100 bytes Unused // something free(p); Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 24 Dynamic allocation • How to allocate and clean memory region on heap? void *p; p = malloc(100); Used 100 bytes Unused // something free(p); Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 25 Dynamic allocation • How to allocate and clean memory region on heap? void *p; p = malloc(100); Used 100 bytes Unused // something free(p); Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 26 Dynamic allocation • How to allocate and clean memory region on heap? void *p; p = malloc(100); Used Unused // something free(p); Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 27 Dynamic allocation • Exercise: Implement your version of malloc and free using the technique described above Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 28 [...]... ENGINEERING HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY 18 Heap region • We could increase or decrease the size of heap region by using two system calls: • brk changes the value of break pointer • sbrk increases the value of break by a given number of bytes • We could use those system calls to implement malloc and free Autumn 2016 FACULTY OF COMPUTER SCIENCE AND ENGINEERING HO CHI MINH CITY UNIVERSITY OF