Programs and Processes Jeff Chase Duke University.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
1 Computer Architecture MIPS Simulator and Assembly language.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
User-Level Memory Management in Linux Programming
Process, Pointers, and Heap Manager COMPSCI210 Recitation 31 Aug 2012 Vamsi Thummala.
Kernighan/Ritchie: Kelley/Pohl:
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Run-time Environment and Program Organization
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
System Calls 1.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
UNIX! Landon Cox September 3, Dealing with complexity How do you reduce the complexity of large programs? Break functionality into modules Goal.
Implementing Processes. Review: Threads vs. Processes 1. The process is a kernel abstraction for an independent executing program. includes at least one.
MIPS coding. SPIM Some links can be found such as:
Programs and Processes Jeff Chase Duke University.
CPS110: Intro to processes, threads and concurrency Author: Landon Cox.
Runtime Environments Compiler Construction Chapter 7.
D u k e S y s t e m s CPS 210 Introduction to Operating Systems Spring 2013 Jeff Chase Duke University.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
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.
More on Programs and Processes Jeff Chase Duke University.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
More on Programs and Processes Jeff Chase Duke University.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CSC 8505 Compiler Construction Runtime Environments.
Chapter 13 : Symbol Management in Linking
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
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.
Virtual Memory and Address Translation. Review: the Program and the Process VAS text dataidata wdata header symbol table relocation records program text.
Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
RealTimeSystems Lab Jong-Koo, Lim
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
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” –
Lecture 3 Translation.
Stack and Heap Memory Stack resident variables include:
Computer Architecture & Operations I
Virtualization Virtualize hardware resources through abstraction CPU
Topic 2e High-Level languages and Systems Software
Lecture 5: Process Creation
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Memory Allocation CS 217.
Understanding Program Address Space
Lecture Topics: 11/1 General Operating System Concepts Processes
Processes in Unix and Windows
Runtime Environments What is in the memory?.
“Way easier than when we were students”
SPL – PS1 Introduction to C++.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Programs and Processes Jeff Chase Duke University

The Operating System An operating system: – Runs programs; sets up execution contexts for programs – Enables programs to interact with the outside world – Enforces isolation among programs – Mediates interactions among programs User Applications Operating System(s) Substrate / Architecture

Today What is a program? – A little bit of C on “classical OS” How does a program run? How are programs built? What does the computer look like to a program?

A simple C program int main() { }

Simple I/O: args and printf #include int main(int argc, char* argv[]) { int i; printf("arguments: %d\n", argc); for (i=0; i<argc; i++) { printf("%d: %s\n", i, argv[i]); }

Environment variables #include int main(int argc, char* argv[], char* envp[]) { int i; int count = atoi(argv[1]); for (i=0; i < count; i++) { printf("env %d: %s\n", i, envp[i]); }

OS Platform: A Better Model Platform: same for all applications E,g,, classical OS kernel Libraries: shared by multiple applications Applications OS platform mediates access to shared resources. [RAD Lab]

Libraries Programs may incorporate (link to) libraries packaged with the OS or written by third parties. – E.g., C standard library and standard I/O library These libraries may even define a system API that applications use. (e.g., heap manager malloc and free). Libraries run as part of the program. Any failure in the library can damage the program, and vice versa. These libraries have no more privilege or power than any other part of the program. However, some libraries are merely wrappers for system calls to the trusted operating system kernel.

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); }

.section__TEXT,__text,regular,pure_instructions.globl_p1.align4, 0x90 _p1: ## BB#0: pushq%rbp Ltmp2:.cfi_def_cfa_offset 16 Ltmp3:.cfi_offset %rbp, -16 movq%rsp, %rbp Ltmp4:.cfi_def_cfa_register %rbp movl$1, %eax movq%rdi, -8(%rbp) popq%rbp ret.cfi_endproc.globl_p2.align4, 0x90 _p2: …. ret.cfi_endproc.section __TEXT,__cstring,cstring_literals L_.str: "hello\n".comm_val,4,2

Global data (“static”) int g; int g0 = 0; int g1 = 1;.globl_g0 __DATA,__common,_g0,4,2.section__DATA,__data.globl_g1 _g1:.long1 ## 0x1.comm_g,4,2

Assembler directives: quick peek From x86 Assembly Language Reference Manual The.align directive causes the next data generated to be aligned modulo integer bytes. The.ascii directive places the characters in string into the object module at the current location but does not terminate the string with a null byte (\0). The.comm directive allocates storage in the data section. The storage is referenced by the identifier name. Size is measured in bytes and must be a positive integer. The.globl directive declares each symbol in the list to be global. Each symbol is either defined externally or defined in the input file and accessible in other files. The.long directive generates a long integer (32-bit, two's complement value) for each expression into the current section. Each expression must be a 32–bit value and must evaluate to an integer value.

Calling the module #include extern int p1(); extern int p2(); int main() { int i; i = p2(); printf("%d\n", i); }

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

What’s in an Object File or Executable? int j = 327; char* s = “hello\n”; char sbuf[512]; int p() { int k = 0; j = write(1, s, 6); return(j); } text data idata wdata header symbol table relocation records program instructions p immutable data (constants) “hello\n” writable global/static data j, s j, s,p,sbuf Header “magic number” indicates type of file/image. Section table an array of (offset, len, startVA) sections Used by linker; may be removed after final link step and strip. Also includes info for debugger.

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

A Peek Inside a Running Program 0 high code library your data heap registers CPU R0 Rn PC “memory” x x your program common runtime stack address space (virtual or physical) e.g., a virtual memory for a process SP y y

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

int P(int a){…} void C(int x){ int y=P(x); } How do C and P share information? Via a shared, in-memory stack

int P(int a){…} void C(int x){ int y=P(x); } What info is stored on the stack? C’s registers, call arguments, RA, P's local vars

Review of the stack Each stack frame contains a function’s Local variables Parameters Return address Saved values of calling function’s registers The stack enables recursion

const1=1 const2=0 const1=1 const2=0 main tmp=1 RA=0x804838c tmp=1 RA=0x804838c A RA=0x B const=0 RA=0x const=0 RA=0x C tmp=0 RA=0x tmp=0 RA=0x A 0xfffffff 0x0 Memory void C () { A (0); } void B () { C (); } void A (int tmp){ if (tmp) B (); } int main () { A (1); return 0; } 0x x x x804838c Code Stack … SP

const1=3 const2=0 const1=3 const2=0 main bnd=3 RA=0x804838c bnd=3 RA=0x804838c A bnd=2 RA=0x bnd=2 RA=0x A bnd=1 RA=0x bnd=1 RA=0x A bnd=0 RA=0x bnd=0 RA=0x A 0xfffffff 0x0 Memory void A (int bnd){ if (bnd) A (bnd-1); } int main () { A (3); return 0; } 0x x804838c Code Stack … SP How can recursion go wrong? Can overflow the stack … Keep adding frame after frame …

wrd[3] wrd[2] wrd[1] wrd[0] const2=0 wrd[3] wrd[2] wrd[1] wrd[0] const2=0 main b= 0x00234 RA=0x804838c b= 0x00234 RA=0x804838c cap 0xfffffff 0x0 Memory void cap (char* b){ for (int i=0; b[i]!=‘\0’; i++) b[i]+=32; } int main(char*arg) { char wrd[4]; strcpy(arg, wrd); cap (wrd); return 0; } 0x x804838c Code Stack … SP 0x00234 What can go wrong? Can overflow wrd variable … Overwrite cap’s RA …

Heap: dynamic memory Allocated heap blocks for structs or objects. Align! A contiguous chunk of 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.

“Classic Linux Address Space” N