More on Programs and Processes Jeff Chase Duke University.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Management
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
1 Computer Architecture MIPS Simulator and Assembly language.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Process, Pointers, and Heap Manager COMPSCI210 Recitation 31 Aug 2012 Vamsi Thummala.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Processes CSCI 444/544 Operating Systems Fall 2008.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
MEMORY MANAGEMENT By KUNAL KADAKIA RISHIT SHAH. Memory Memory is a large array of words or bytes, each with its own address. It is a repository of quickly.
Lecture 9: SHELL PROGRAMMING (continued) Creating shell scripts!
CS 350 Operating Systems & Programming Languages Ethan Race Oren Rasekh Christopher Roberts Christopher Rogers Anthony Simon Benjamin Ramos.
System Calls 1.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Implementing Processes. Review: Threads vs. Processes 1. The process is a kernel abstraction for an independent executing program. includes at least one.
Programs and Processes Jeff Chase Duke University.
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 6 System Calls OS System.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Memory Management Requirements of memory management system to provide the memory space to enable several processes to execute concurrently to provide.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Memory Management Operating Systems CS550. Memory Manager Memory manager - manages allocation and de-allocation of main memory Plays significant impact.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
More on Programs and Processes Jeff Chase Duke University.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
Programs and Processes Jeff Chase Duke University.
Programs and Processes. The Virtual Address Space A typical process VAS space includes: user regions in the lower half V->P mappings specific to each.
Notes on Heap Manager in C Jeff Chase Duke University.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Some abstractions and a peek at their implementation Jeff Chase Duke University.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
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” –
Introduction to Operating Systems Concepts
Dynamic Storage Allocation
Processes and threads.
Virtualization Virtualize hardware resources through abstraction CPU
Protection of System Resources
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Memory Management © 2004, D. J. Foreman.
Checking Memory Management
The Hardware/Software Interface CSE351 Winter 2013
Instructors: Haryadi Gunawi
Main Memory Management
Processes in Unix, Linux, and Windows
The Operating System Memory Manager
CS399 New Beginnings Jonathan Walpole.
Memory Allocation CS 217.
Lecture 3: Main Memory.
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
COMP755 Advanced Operating Systems
Makefiles, GDB, Valgrind
Presentation transcript:

More on Programs and Processes Jeff Chase Duke University

OS Platform Kernel: same for all applications Libraries: shared by multiple applications Applications OS platform mediates access to shared resources. [RAD Lab]

A simple module P1() P2() P3() P4() state int val = 0; int p1(char *s) { return 1; } int p2() { char *s; int i; s = "hello\n"; i = p1(s); return(i); } E.g., a library API

Calling the module #include extern int p1(); extern int p2(); int main() { int i; i = p2(); printf("%d\n", i); } Program P1() P2() P3() P4() state interface signatures

The Birth of a Program (C/Ux) int j; char* s = “hello\n”; int p() { j = write(1, s, 6); return(j); } myprogram.c compiler ….. p: store this store that push jsr _write ret etc. myprogram.s assembler data myprogram.o linker object file data program (executable file) myprogram data libraries and other object files or archives header files

Program Running a program When a program launches, the OS creates an execution context (process) to run it, with a thread to run the program, and a virtual memory to store the running program’s code and data. data code constants initialized data imports/exports symbols types/interfaces Process

0x0 0x7fffffff Static data Dynamic data (heap/BSS) Text (code) Stack Reserved VAS example (32-bit) The program uses virtual memory through its process’ Virtual Address Space: An addressable array of bytes… Containing every instruction the process thread can execute… And every piece of data those instructions can read/write… – i.e., read/write == load/store on memory Partitioned into logical segments with distinct purpose and use. Every memory reference by a thread is interpreted in the context of its VAS. – Resolves to a location in machine memory

Heap: dynamic memory Allocated heap blocks for structs or objects. Align! A contiguous chunk of virtual memory obtained from OS kernel. E.g., with Unix sbrk() system call. A runtime library obtains the block and manages it as a “heap” for use by the programming language environment, to store dynamic objects. E.g., with Unix malloc and free library calls. Free block

Get started on heap manager! To get started on heap manager, download the files and type “make”. – Provides a script to build the heap manager test programs on Linux or MacOS. This lab is just a taste of system programming in C. The classic text is CS:APP. Also see PDF “What every computer systems student should know about computers” on the course website. You may think of it as notes from CS:APP. It covers background from Computer Architecture and also some material for this class. a classic

Basic hints on using Unix Find a properly installed Unix system: linux.cs.duke.edu, or MacOS with Xcode and its command line tools will do nicely. Learn a little about the Unix shell command language: e.g., look ahead to the shell lab, Lab #2. On MacOS open the standard Terminal utility. Learn some basic commands: cd, ls, cat, grep, more/less, pwd, rm, cp, mv, and a text editor of some kind (vi, emacs, …). Spend one hour. Learn basics of make. Look at the makefile. Run “make –i” to get it to tell you what it is doing. Understand what it is doing. Wikipedia is a good source for basics. Use the man command to learn about commands (1), syscalls (2), or C libraries (3). E.g.: type “man man”. Know how to run your programs under a debugger: gdb. If it crashes you can find out where. It’s easy to set breakpoints, print variables, etc. If your program doesn’t compile, deal with errors from the top down. Try “make >out 2>out”. It puts all output in the file “out” to examine at leisure. Put source in a revision system like git or svn, but Do. Not. Share. It.

Using the heap (1) #include int main() { char* cb = (char*) malloc(14); cb[0]='h'; cb[1]='i'; cb[2]='!'; cb[3]='\0'; printf("%s\n", cb); free(cb); } chase$ cc -o heap heap.c chase$./heap hi! chase$

Using the heap (2) #include int main() { char* cb = (char*) malloc(14); cb[0]='h'; cb[1]='i'; cb[2]='!'; cb[3]='\0'; printf("%s\n", cb); int *ip = (int*)cb; printf("0x%x\n", *ip); free(cb); } chase$ cc -o heap heap.c chase$./heap hi! 0x chase$ h=0x68 i=0x69 !=0x21 0 cb ip Try:

Endianness A silly difference among machine architectures creates a need for byte swapping when unlike machines exchange data over a network. Lilliput and Blefuscu are at war over which end of a soft-boiled egg to crack. Gulliver’s Travel’s 1726

64 bytes: 3 ways p + 0x0 0x1f 0x0 0x1f 0x0 char p[] char *p int p[] int* p p char* p[] char** p Pointers (addresses) are 8 bytes on a 64-bit machine. Memory is “fungible”.

Alignment p + 0x0 0x1f 0x0 0x1f 0x0 char p[] char *p int p[] int* p p char* p[] char** p Machines desire/require that an n-byte type is aligned on an n-byte boundary. n = 2 i X X X

Using the heap (3) char* cb = (char*)malloc(14); strcpy(cb, "hi!"); free(cb); /* * Dangling reference! */ printf("%s\n", cb); int *ip = (int*)cb; printf("0x%x\n", *ip); /* * Uninitialized heap block! */ char* cb2 = (char*)malloc(14); printf("%s\n", cb2); chase$ cc -o heap2 heap2.c chase$./heap2 ??? chase$ h=0x68 i=0x69 !=0x21 0 cb ip

Using the heap (4) chase$ cc -o heap2 heap2.c chase$./heap2 hi! 0x hi! chase$ h=0x68 i=0x69 !=0x21 0 cb ip char* cb = (char*)malloc(14); strcpy(cb, "hi!"); free(cb); /* * Dangling reference! */ printf("%s\n", cb); int *ip = (int*)cb; printf("0x%x\n", *ip); /* * Uninitialized heap block! */ char* cb2 = (char*)malloc(14); printf("%s\n", cb2);

WARNING These behaviors are undefined. Any program whose behavior relies on the meaning of a dangling reference is incorrect. For example, a change in the allocation policy of the heap manager could result in different behavior. Can a program stay safe from dangling references by just never calling free?

Another way Java’s new operator calls malloc “under the hood”. – Allocate a heap block for each new object. – How big should the heap block be? Who decides? – Java initializes the block by automatically calling a type-specific constructor defined by the program. – C++ is the same. But in Java there is no free (or delete)! – No dangling references! How does Java avoid memory leaks?

Digression: Garbage collection Java has new but no free (or delete)! – No dangling references! Q: How does Java avoid memory leaks? A: automatic garbage collection Java is strongly typed: e.g., it tracks and controls pointers, and it knows where all of them are and what object types they reference. Java knows if an object has no references pointing to it, and it calls free for you! But Java has no pointer arithmetic: your program cannot manipulate virtual addresses as numbers, as in C.

Pointer arithmetic char* cb = (char*) malloc(14); strcpy(cb, "hi!"); unsigned long ptr = (unsigned long)cb; ptr = ptr + 2; cb = (char*)ptr; printf("%s\n", cb); free(cb); h=0x68 i=0x69 !=0x21 0 cb ptr chase$ cc -o heap3 heap3.c chase$./heap3 ??? chase$

Pointer arithmetic char* cb = (char*) malloc(14); strcpy(cb, "hi!"); unsigned long ptr = (unsigned long)cb; ptr = ptr + 2; cb = (char*)ptr; printf("%s\n", cb); free(cb); h=0x68 i=0x69 !=0x21 0 cb ptr chase$ cc -o heap3 heap3.c chase$./heap3 ! heap3(5478) malloc: *** error for object 0x7f92a9c000e2: pointer being freed was not allocated Abort trap: 6 chase$

About the heap manager The heap manager is a library module. A program can redefine it, change it, misuse it. It manages memory only within a running program. The kernel allocates memory among programs (processes) – E.g., via sbrk or mmap system calls. lib OS kernel processes prog The heap manager does not protect the program from itself! A buggy program can corrupt the heap! A buggy heap manager can crash the program! “fate sharing”

Heap manager policy The heap manager must find a suitable free block to return for each call to malloc(). – No byte can be part of two simultaneously allocated heap blocks! If any byte of memory is doubly allocated, programs will fail. We test for this! A heap manager has a policy algorithm to identify a suitable free block within the heap. – Last fit, first fit, best fit, worst fit – Choose your favorite! – Goals: be quick, and use memory efficiently – Behavior depends on workload: pattern of malloc/free requests This is an old problem in computer science, and it occurs in many settings: variable partitioning.

Variable Partitioning Variable partitioning is the strategy of parking differently sized cars along a street with no marked parking space dividers. Wasted space external fragmentation 2 3 1

Fixed Partitioning Wasted space internal fragmentation

0x0 0x7fffffff Static data Dynamic data (heap/BSS) Text (code) Stack Reserved 0x0 0x7fffffff Static data Dynamic data (heap/BSS) Text (code) Stack Reserved

cpu.c (OSTEP) int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: cpu \n"); exit(1); } char *str = argv[1]; while (1) { printf("%s\n", str); Spin(1); } return 0; } time  chase$ cc -o cpu cpu.c chase$./cpu A & chase$./cpu B & A B A B …

Foreground and background A multiprogrammed OS can run many processes concurrently / simultaneously / at the same time. When you run a program as a command to the shell (e.g., Terminal), by default the process is foreground. – The shell calls the OS to create a child process to run the program, passes control of the terminal to the child process, and waits for the process to finish (exit). You can also run a program in background with &. – & is an arbitrary syntax used in Unix since the 1960s. – The shell creates the child process and starts it running, but keeps control of the terminal to accept another command. – & allows you to run multiple concurrent processes. – The processes share the machine; they could run different programs, or the same program.

mem.c (OSTEP) int main(int argc, char *argv[]) { int *p; p = malloc(sizeof(int)); *p = atoi(argv[1]); while (1) { Spin(1); *p = *p + 1; printf("(pid:%d) value of p: %d\n”, getpid(), *p); } chase$ cc -o mem mem.c chase$./mem 21 & chase$./mem 42 & (pid:5587) value of p: 22 (pid:5587) value of p: 23 (pid:5587) value of p: 24 (pid:5588) value of p: 43 (pid:5588) value of p: 44 (pid:5587) value of p: 25 (pid:5587) value of p: 26 … data p pid: 5587pid: 5588

Operating Systems: The Classical View data Programs run as independent processes. Protected system calls...and upcalls (e.g., signals) Protected OS kernel mediates access to shared resources. Threads enter the kernel for OS services. Each process has a private virtual address space and one or more threads. The kernel code and data are protected from untrusted processes.