Process Memory COMP 40: Machine Structure and

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Memory Management and Protection Part 3:Virtual memory, mode switching,
Advertisements

Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Copyright 2014 – Noah Mendelsohn Assemblers, Macros and The UM Macro Assembler (UMASM) Noah Mendelsohn Tufts University
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
User-Level Memory Management in Linux Programming
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Memory Management Questions answered in this lecture: How do processes share memory? What is static relocation? What is dynamic relocation? What is segmentation?
11/15/2005Comp 120 Fall November Seven Classes to Go! Questions! VM and Making Programs Go.
Processes CSCI 444/544 Operating Systems Fall 2008.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
C and Data Structures Baojian Hua
Memory Layout C and Data Structures Baojian Hua
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Lecture Topics: 11/3 Address spaces Memory protection Creating a process –NT style –Unix style.
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
System Calls 1.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Computer Hardware and Software Chapter 1. Overview Brief History of Computers Hardware of a Computer Binary and Hexadecimal Numbers Compiling vs. Interpreting.
Section 3.1: Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random.
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Topic 2d High-Level languages and Systems Software
PA0 due 60 hours. Lecture 4 Memory Management OSTEP Virtualization CPU: illusion of private CPU RAM: illusion of private memory Concurrency Persistence.
Chapter 4 Memory Management Virtual Memory.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web:
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Copyright 2014 – Noah Mendelsohn The UM Macro Assembler (UMASM) Implementation Noah Mendelsohn Tufts University
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
The Universal Machine (UM) Implementing the UM Noah Mendelsohn Tufts University Web:
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Introduction to Operating Systems
Processes and threads.
Virtualization Virtualize hardware resources through abstraction CPU
Segmentation COMP 755.
Session 3 Memory Management
Programs – Loading and Running an Executable
Modeling Page Replacement Algorithms
Program Execution in Linux
Swapping Segmented paging allows us to have non-contiguous allocations
Software Development with uMPS
Instructors: Haryadi Gunawi
Introduction to Operating Systems
Processes in Unix, Linux, and Windows
Memory Allocation CS 217.
Modeling Page Replacement Algorithms
Virtual Memory.
CSCE 313 – Introduction to UNIx process
System Calls David Ferry CSCI 3500 – Operating Systems
Virtual Memory: Systems CSCI 380: Operating Systems
Program Execution in Linux
Computer System Structures
CS703 - Advanced Operating Systems
CSE 153 Design of Operating Systems Winter 2019
Virtual Memory and Paging
Advanced OS COMP 755.
Presentation transcript:

Process Memory COMP 40: Machine Structure and Assembly Language Programming (Fall 2014) Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah

Sharing the Computer

Operating systems do two main things for us: They make the computer easier to use The facilitate sharing of the computer by multiple programs and users

Sharing the Computer

All programs share memory Sharing Memory Multiple Programs Running at once MAIN MEMORY CPU Angry Birds Play Video Browser OPERATING SYSTEM

CPU is shared…can only do one thing at a time* Sharing the CPU Multiple Programs Running at once CPU is shared…can only do one thing at a time* MAIN MEMORY CPU Angry Birds Play Video Browser OPERATING SYSTEM *Actually, modern CPUs can do a few things at a time, but for now assume a simple, traditional computer

Processes The Fundamental Unit of Work

There is one OS “process” for each running copy of a program Processes The operating switches rapidly between them…giving the illusion they are all running at once Angry Birds Angry Birds Browser MEMORY CPU Printer Keyboard, mouse,display Disk

CPU Processes The operating system uses special virtual memory hardware to give each process the illusion of it’s own private memory Angry Birds Angry Birds Browser MEMORY CPU Keyboard, mouse,display Disk Printer

Process Memory

The process memory illusion Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives Now we’ll learn how your program uses these!

The process memory illusion Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives (BSS)

The process memory illusion Our systems use 48 bit addressing, but it turns out the “top” half of memory is for the operating system. Therefore the highest address you’ll see in your program is (in binary): 11111111111111111111111111111111111111111111111 Yes, that’s 47 ‘1’ bits…however The process memory illusion Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives (BSS)

The process memory illusion Our systems use 48 bit addressing, but it turns out the “top” half of memory is for the operating system. Therefore the highest address you’ll see in your program is (in binary): 11111111111111111111111111111111111111111111111 Yes, that’s 47 ‘1’ bits…however The process memory illusion Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives (BSS) This is a good time for a first tutorial on “hex” numbering…

Brief interlude… …writing numbers in hex (base 16)

Hex conversion and arithmetic will be significant on the midterm! Hexadecimal You already know base 10 and base 2, hex is just base 16 Base Digits 2 0,1 10 0,1,2,3,4,5,6,7,8,9 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, A few simple examples: 16(dec) = 10(hex) 19(dec) = 13(hex) 32(dec) = 20(hex) 256(dec) = 100(hex) Hex conversion and arithmetic will be significant on the midterm! As a professional programmer you must be expert using hex!!

Conversions between hex and binary are trivial Consider the decimal number 538 Which happens to be 1000011010 (binary) and 21A (hex) 2 1 A You get hex from binary by grouping the bits …and that 48 bit address looks a lot better this way: 011111111111111111111111111111111111111111111111 (bin) 7fffffffffff (hex)

Process Memory (continued)

The process memory illusion Our systems use 48 bit addressing, but it turns out the “top” half of memory is for the operating system. Therefore the highest address you’ll see in your program is (in hex): 7fffffffffff The process memory illusion 7fffffffffff Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives (BSS)

The process memory illusion 7fffffffffff Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } (BSS)

The process memory illusion 7fffffffffff argv, environ Stack char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

The process memory illusion 7fffffffffff argv, environ Stack char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Heap (malloc’d) Static uninitialized (BSS) Program file tells how much is needed Static initialized Loaded with your program Text (code)

The process memory illusion 7fffffffffff argv, environ Stack char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argv[]) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

The process memory illusion 7fffffffffff Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives malloc/free are library functions that run in user mode to allocate space within the heap…. …when they run out of space to play with, they call the sbrk system call to ask the OS to grow the heap.

The process memory illusion 7fffffffffff argv, environ Stack char notInitialized[10000]; char initialized[] = “I love COMP 111”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

The process memory illusion 7fffffffffff argv, environ Stack char notInitialized[10000]; char initialized[] = “I love COMP 111”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

The process memory illusion 7fffffffffff argv, environ Stack char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argv[]) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

Of course, the stack enables recursion 7fffffffffff argv, environ Stack int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } n=4 n=3 n=2 n=1 Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

Of course, the stack enables recursion 7fffffffffff argv, environ Stack int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } n=4 n=3 n=2 Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

The process memory illusion 7fffffffffff In Linux, there is a system call to grow the heap, but not the stack. When the system faults on an access to the next page below the end of the stack, the OS maps more memory automatically. (up to configurable limit). Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program Process thinks it's running in a private space Separated into segments, from address 0 Stack: memory for executing subroutines Heap: memory for malloc/new Global static variables Text segment: where program lives (BSS)

The process memory illusion 7fffffffffff argv, environ Stack Process thinks it's running in a private space. Separated into segments, from address 0: Text segment: where program lives. Global variables. Heap: memory for malloc/new. Stack: memory for executing subroutines. Nothing here Memory between the stack and the heap is not mapped by the OS. As far as the process is concerned, it doesn’t exist. Access causes a segfault. Heap (malloc’d) Static uninitialized (BSS) Static initialized Text (code)

The process memory illusion Kernel The process memory illusion 7fffffffffff argv, environ Stack Process thinks it's running in a private space. Separated into segments, from address 0: Text segment: where program lives. Global variables. Heap: memory for malloc/new. Stack: memory for executing subroutines. Surprisingly: the kernel actual lives in every process space at the “top”. The maps are set so ordinary user code segfaults on access, but… …when in the kernel executing a system call, the sytem can access its own kernel segments as well as the user’s. Heap (malloc’d) Static uninitialized (BSS) Static initialized Text (code)