Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

Similar documents
Shared Memory. By Oren Kalinsky

Shared Memory Memory mapped files

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

ECE 650 Systems Programming & Engineering. Spring 2018

CSCI 4061: Virtual Memory

경희대학교컴퓨터공학과 조진성. UNIX System Programming

Virtual Memory: Systems

Introduction to Linux, for Embedded Engineers Tutorial on Virtual Memory. Feb. 22, 2007 Tetsuyuki Kobayashi Aplix Corporation. [translated by ikoma]

Signal Example 1. Signal Example 2

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Lab 09 - Virtual Memory

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

Processes and Threads

W4118 Operating Systems. Junfeng Yang

OS Lab Tutorial 1. Spawning processes Shared memory

Lecture 13: Address Translation

Operating systems. Lecture 9

Inter-process communication (IPC)

COMP 3100 Operating Systems

File Systems: Consistency Issues

Applications of. Virtual Memory in. OS Design

Operating Systems 2010/2011

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

Operating System Labs. Yuanbin Wu

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Reminder. COP4600 Discussion 8 Segmentation, File System, Fork() Implementation. Question-1. Discussion 7 Recap. Question-1(cont) Question-1

Processes COMPSCI 386

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Virtual to physical address translation

CS 550 Operating Systems Spring Inter Process Communication

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Recap: Memory Management

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Programmation Système Cours 5 Memory Mapping

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Interprocess Communication

Introduction to File Systems. CSE 120 Winter 2001

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

CS399 New Beginnings. Jonathan Walpole

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Virtual Memory: Systems

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

UNIT III- INTER PROCESS COMMUNICATIONS Part A

Foundations of Computer Systems

CS162 - Operating Systems and Systems Programming. Address Translation => Paging"

Virtual Memory. Alan L. Cox Some slides adapted from CMU slides

CIS Operating Systems Memory Management Address Translation for Paging. Professor Qiang Zeng Spring 2018

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

CSE 120 Principles of Operating Systems

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2017

ECEN 449 Microprocessor System Design. Review of C Programming

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

Programming with Shared Memory PART I. HPC Fall 2010 Prof. Robert van Engelen

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

CSI Module 2: Processes

CIS Operating Systems Memory Management Cache and Demand Paging. Professor Qiang Zeng Spring 2018

Start of Lecture on January 20, Chapter 3: Processes

OPERATING SYSTEMS: Lesson 2: Operating System Services

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Lecture 24: Filesystems: FAT, FFS, NTFS

Virtual Memory 1. Virtual Memory

Virtual Memory 1. Virtual Memory

Virtual Memory Virtual memory first used to relive programmers from the burden of managing overlays.

CSE 120 Principles of Operating Systems Spring 2017

Operating Systems, Fall

Operating Systems, Fall

CS 3733 Operating Systems:

Chapter 8: Main Memory

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

Part II Processes and Threads Process Basics

Memory management. Johan Montelius KTH

CMSC 412 Project #4 Virtual Memory Due Friday April 11, 2014, at 5:00pm

Chapter 8: Memory-Management Strategies

Process Address Spaces and Binary Formats

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 8: Main Memory

Process. Program Vs. process. During execution, the process may be in one of the following states

Memory management. the CPU cannot be divided into smaller pieces and has to be given to a process as one unit (sharing is solely temporal).

Virtual Memory. CS 351: Systems Programming Michael Saelee

CMPSC 311 Exam 2. March 27, 2015

Main Memory: Address Translation

Gabrielle Evaristo CSE 460. Lab Shared Memory

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

CS 318 Principles of Operating Systems

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Main Memory. CISC3595, Spring 2015 X. Zhang Fordham University

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Goals of Memory Management

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

Topics: Virtual Memory (SGG, Chapter 09) CS 3733 Operating Systems

Virtual Memory. 11/8/16 (election day) Vote!

Registers Cache Main memory Magnetic disk Magnetic tape

VEOS high level design. Revision 2.1 NEC

Memory System Case Studies Oct. 13, 2008

Optimizing Dynamic Memory Management

a process may be swapped in and out of main memory such that it occupies different regions

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Transcription:

Memory management Single process All memory assigned to the process Addresses defined at compile time Multiple processes. How to: assign memory manage addresses? manage relocation? manage program grow?

manage program grow? overlay Statically defined memory zone loaded on demand By the program may be replaced Program resident memory Overlay 1 Overlay 2 Overlay 3

Memory management

for(i = 0; i< 1000; i++){.l3: vect[i] = c; } movl-4(%rbp), %eax cltq movzbl -5(%rbp), %edx movb %dl, -1008(%rbp,%rax) addl $1, -4(%rbp).L2: cmpl$999, -4(%rbp) jle.l3

Virtual memory Program addresses are independent on the physical location memory manager translates addresses virtual -> physical verifies permitions Address Translation

Address Translation Concept

Adress translation Process isolation Interprocess communication Shared code Program debugging Efficient I/O Memory mapped files Virtual memory Checkpointing Process migration Information flow control DSM

Shared memory Processes use regular variables/vector to communicate Available in shared memory Should be explicitly created. Each process has its own address space P1 Address space Read Shared memory P1 Address space Write 9

Shared memory Advantages Random Access you can update a small piece in the middle of a data structure, rather than the entire structure Efficiency unlike message queues and pipes, copy data between user memory kernel memory shared memory is directly accessed Shared memory resides in the user process memory Is shared among other processes 10

Process comunication 11

Shared memory Disadvantages No automatic synchronization In pipes or message queues Programmer has to provide synchronization Semaphores or signals. Pointers are only valid within a given process. Pointer offsets cannot be assumed to be valid across inter-process boundaries. This complicates the sharing of linked lists or binary trees. Variables are produced by the compiler Names can not be used to access shared memory 12

Shared memory in *NIX System V shared memory Original shared memory mechanism, still widely used Sharing between unrelated processes Shared mappings mmap Shared file mappings Sharing between unrelated processes, backed by filesystem Shared anonymous mappings Sharing between related processes only (related via fork()) POSIX shared memory Sharing between unrelated processes, without overhead of filesystem I/O Intended to be simpler and better than older APIs 13

Shared memory in *NIX Programming steps Define Shared data structure Creation of memory segment Configuration Assignment to address Access Disconnection Destruction In multiple processes 14

System V shared memory Shared memory operations shmget shmctl allows the user to receive information on a shared memory segment, set the owner, group, and permissions of a shared memory segment, destroy a segment shmat allocates a shared memory segment attaches the shared memory segment (identified by shmid) to the address space of the calling process shmdt detaches the shared memory segment (located at the address specified by shmaddr) from the address space of the calling process 15

System V shared memory int shmget(key_t key, size_t size, int shmflg); key known by all processes Flags - IPC_CREAT 0666 void *shmat(int shmid, const void *shmaddr, int shmflg); Shmid returned by shmget shmaddr NULL or other address Shmflg - SHM_EXEC SHM_RDONLY 16

System V shared memory char * shm; key = 5678; /* Create the segment */ if ((shmid = shmget(key, SHMSZ, IPC_CREAT 0666)) < 0) { perror("shmget"); exit(1); } /*Now we attach the segment to our data space.*/ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); } exit(1); Repeated on several processes Key must be known Followed by fork Shm value is shared 17

Memory mapped files Access a file contect With memory access operations Assignments e accesses 18

Memory mapped files void *mmap(void * addr, size_tlen, int prot, int flags, int fd, off_toffset); Prot Flags PROT_READ PROT_WRITE PROT_EXEC PROT_NONE MAP_SHARED MAP_PRIVATE MAP_FIXED int munmap(void * addr, size_t len); 19

Memory access After mmap a variable contains a pointer to region Programmer can access that memory as a Pointer to variable Vector Is it possible to create linked lists in shared memory? No. mmap in different processes returns different addresses 20

Memory mapped files Int * ptr; fd = open(argv[1], O_RDWR O_CREAT, FILE_MODE); ptr = Mmap(NULL, sizeof(int), PROT_READ PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); 21

Mmap and fork Open could be avoided Parent and son share the address to memory Memory is shared among processes 4.4BSD introduced anonymous memory sharing Flag MAP_SHARED MAP_ANON Fd -1 ptr = Mmap(NULL, sizeof(int), PROT_READ PROT_WRITE, MAP_SHARED MAP_ANON, -1, 0); 22

POSIX shared memory Mmap File system incurrs overhead Sharing between unrelated processes, without overhead of filesystem I/O Intended to be simpler and better than older APIs New function to create memory regions 23

POSIX shared memory Create/opens a shared memory space fd_mem = shm_open("/myregion", /*region name*/ O_CREAT O_RDWR, 0600); Memory regions are created with size 0 Assign a size ftrucate (fd_mem, sizeof(int)) If the object has already been sized by another process, you can get its size with the fstat function A global region has been created Data stored is kernel persistent But still inaccessible by processes use mmap with fd_mem 24

int fd = shm_open(memname, O_CREAT O_TRUNC O_RDWR, 0666); if (fd == -1) error_and_die("shm_open"); int r = ftruncate(fd, sizeof(int)); if (r!= 0) error_and_die("ftruncate"); int *v_int = mmap(0, sizeof(int), PROT_READ PROT_WRITE, MAP_SHARED, fd, 0); 25

Shutdown Close the shared memory object Unmap the shared memory object: munmap (pointer, SHM_SIZE); The address become free to be used. At this stage if other processes open and map a memory region close(fd_mem) They can access data previously written To remove permanently the shared memory object: shm_unlink (SHARED_MEMORY_NAME); The object is effectively deleted after the last call to shm_unlink 26

27

Address Translation Flexible Address Translation Base and bound Segmentation Paging Multilevel translation Efficient Address Translation Translation Lookaside Buffers Virtually and physically addressed caches

Address Translation Goals Memory protection Memory sharing Shared libraries, interprocess communication Sparse addresses Multiple regions of dynamic allocation (heaps/stacks) Efficiency Memory placement Runtime lookup Compact translation tables Portability

Virtually Addressed Base and Bounds

Virtually Addressed Base and Bounds Simple and fast coarse-grained protection at the process levels impossible to prevent self overriding of code Difficult to share memory regions Memory needs to be continuous hard to implement dynamic memory stack heap,...

Segmentation Evolution of base and bound one process -> multiple bases+bounds one process -> multiple segments Segment: base + bound multiple sizes each segment stored continuously multiple access permissions execute-only (code) or read-write (data)

Segmentation

Segmentation Segmented memory has gaps program memory is not a single continuous regions each segment start at a different base Access outside segments HW generates interrupt Kernel generates segmentation fault Addressable space depends on the segment+offset length

Segmentation

Segmentation Total memory depends base + offset Kernel should manage placement of each segment on physical memory permission of access

Segmentation Overhead of managing Large number of segments Segments of variable size Dynamically growing segments Creation of a segment Virtual address (easy) Memory location (difficult) Fragmentation Segments grows

Paged memory Alternative to segmentation Fixed sized chunks Address translation is similar to segmentation Page frames Segment table page table Table entry pomnts to page base No need for bound Process still views memory as linear Linear memory (virtula) accesses Multiple accessed pages

Paged memory Big advantage over segmentation Free-space allocation is straighforward Physical memory represetation Bitmap (one bit per physical page) Memory sharing Setting of pointer in page table

Optimizations Segmentation+paged memory Copy on write Zero on reference Data breakpoints Stop program when variable gets changed Paged memory Program can execute without all code/data Pages can be loaded while program is executing

Paged memory Downsides Virtual address management is more difficult Stack and heap should be continuous Compiler should guarantee this How to make it work with multiple threads?

Paged Memory

Segmentation Paged Memroy

How to define virtual addresses? Segmentation Virtual address segment# + offset Max size of the segment offset Max memory per process segment table size Max real memory base + offset

Where are the tables In memory Special page/segment Multiple organizations Page segmentation Multi-level paging Multi-level paged segmentation

Page segmentation 32 bit address 4Kb pages 10 bits 10 bits Page number 12 bits Segment number Page offset 1 page table per segment 4 bytes 1K entries

Multi-level paging Virtual address has 4 components 3 indexes Offset Each tabel fits into one page

Multi-level paged segmentation (X86)

32 bit - X86 2 level page table within a segment 10 bits page directory 10 bit second level page table 12 bit offset 4kb page frame Number of 2nd level page table Depends on length of segment Also has permissions

64 bit - X86 64 bits maximum address length Optimization addresses only use 48 bit 128 terabytes 4 level page table within a segment Optimizations Elimination of two levels of page tables 4kb page frame 4th level pages 2Mb Can be overwriden if OS allocates 2Mb contiguos 3rd level pages 1Gb Can be overwriden if OS allocates 1Gb contiguos Good for server with two terabytes

Efficient Address translation Conversion from virtual to real address Requires extra memory accesses Memory accesses are sequential Repeated accesses to same page Why not cache last translations? Translation look-aside buffers Small HW table caching recent address translation

Translation look-aside buffers

TLB Extra HW Big savings in memory accesses Multiple levels of TLBs Requires extra care Multiple processes Different voirtual address space Different permissions Multiprocessors Consistency of multiple TLB one per CPU