Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:

Slides:



Advertisements
Similar presentations
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Advertisements

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.
Copyright 2014 – Noah Mendelsohn Assemblers, Macros and The UM Macro Assembler (UMASM) Noah Mendelsohn Tufts University
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
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?
Introduction to Operating Systems CS-2301 B-term Introduction to Operating Systems CS-2301, System Programming for Non-majors (Slides include materials.
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.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
C and Data Structures Baojian Hua
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
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.
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-
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.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
Memory Management 3 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
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.
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.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
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.
Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Operating Systems Lesson 5. Plan Memory Management ◦ Memory segments types ◦ Processes & Memory ◦ Virtual Memory ◦ Virtual Memory Management ◦ Swap File.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Copyright 2014 – Noah Mendelsohn The UM Macro Assembler (UMASM) Implementation Noah Mendelsohn Tufts University
Representing Negative Numbers Noah Mendelsohn Tufts University Web: COMP 40: Machine.
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.
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.
Process Memory COMP 40: Machine Structure and
Session 3 Memory Management
Programs – Loading and Running an Executable
Software Development with uMPS
Instructors: Haryadi Gunawi
Introduction to Operating Systems
Lecture 7: Introduction to Threads
Processes in Unix, Linux, and Windows
Memory Allocation CS 217.
CSCE 313 – Introduction to UNIx process
System Calls David Ferry CSCI 3500 – Operating Systems
Lecture 7: Introduction to Threads
Virtual Memory: Systems CSCI 380: Operating Systems
Program Execution in Linux
System calls….. C-program->POSIX call
CSE 153 Design of Operating Systems Winter 2019
Virtual Memory and Paging
Topic 2b ISA Support for High-Level Languages
Advanced OS COMP 755.
Presentation transcript:

Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web: COMP 40: Machine Structure and Assembly Language Programming (Fall 2014)

© 2010 Noah Mendelsohn 2 Today  Brief look at: operating system processes  How processes use memory

© 2010 Noah Mendelsohn 3 Sharing the Computer

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

© 2010 Noah Mendelsohn 5 Sharing the Computer

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

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

© 2010 Noah Mendelsohn 8 Processes The Fundamental Unit of Work

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

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

© 2010 Noah Mendelsohn 11 Process Memory

© 2010 Noah Mendelsohn 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 12 Now we’ll learn how your program uses these!

© 2010 Noah Mendelsohn 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 13 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0

© 2010 Noah Mendelsohn 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 14 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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): Yes, that’s 47 ‘1’ bits…however

© 2010 Noah Mendelsohn 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 15 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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): Yes, that’s 47 ‘1’ bits…however This is a good time for a first tutorial on “hex” numbering…

© 2010 Noah Mendelsohn 16 Brief interlude… …writing numbers in hex (base 16)

© 2010 Noah Mendelsohn Hexadecimal 17 You already know base 10 and base 2, hex is just base 16 BaseDigits 20,1 100,1,2,3,4,5,6,7,8,9 160,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) As a professional programmer you must be expert using hex!! Hex conversion and arithmetic will be significant on the midterm!

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

© 2010 Noah Mendelsohn 19 Process Memory (continued)

© 2010 Noah Mendelsohn 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 20 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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

© 2010 Noah Mendelsohn The process memory illusion 21 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion 22 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion 23 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } Program file tells how much is needed 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion 24 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } 7fffffffffff

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

© 2010 Noah Mendelsohn 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 26 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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. 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion 27 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion 28 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion 29 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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); } 7fffffffffff

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

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

© 2010 Noah Mendelsohn 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 32 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 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). 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion  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. 33 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ 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. 7fffffffffff

© 2010 Noah Mendelsohn The process memory illusion  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. 34 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Kernel 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. 7fffffffffff