Programs and Processes Jeff Chase Duke University.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Ch. 8 Functions.
1 Computer Architecture MIPS Simulator and Assembly language.
Utilizing the GDB debugger to analyze programs Background and application.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Process, Pointers, and Heap Manager COMPSCI210 Recitation 31 Aug 2012 Vamsi Thummala.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
CS 536 Spring Run-time organization Lecture 19.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run time vs. Compile time
OS Spring’03 Introduction Operating Systems Spring 2003.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Guide To UNIX Using Linux Third Edition
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
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:
chap13 Chapter 13 Programming in the Large.
CPS110: Intro to processes, threads and concurrency Author: Landon Cox.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
D u k e S y s t e m s CPS 210 Introduction to Operating Systems Spring 2013 Jeff Chase Duke University.
Programming With C.
Topic 2d High-Level languages and Systems Software
D u k e S y s t e m s CPS 210 Introduction to Operating Systems Fall 2012 Jeff Chase Duke University.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
More on Programs and Processes Jeff Chase Duke University.
Processes Introduction to Operating Systems: Module 3.
CS 346 – Chapter 2 OS services –OS user interface –System calls –System programs How to make an OS –Implementation –Structure –Virtual machines Commitment.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CSC 8505 Compiler Construction Runtime Environments.
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.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Notes on Heap Manager in C Jeff Chase Duke University.
Embedding Assembly Code in C Programs תרגול 7 שילוב קוד אסמבלי בקוד C.
CINT & Reflex – The Future CINT’s Future Layout Reflex API Work In Progress: Use Reflex to store dictionary data Smaller memory footprint First step to.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
RealTimeSystems Lab Jong-Koo, Lim
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Introduction to Operating Systems Concepts
Lecture 3 Translation.
Computer Architecture & Operations I
CASE STUDY 1: Linux and Android
Software Development with uMPS
C Basics.
Operation System Program 4
Lecture Topics: 11/1 General Operating System Concepts Processes
Computer Organization and Design Assembly & Compilation
Focus of the Course Object-Oriented Software Development
Runtime Environments What is in the memory?.
An introduction to systems programming
SPL – PS1 Introduction to C++.
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() { }

What’s in a program?

code instructions (“text”) procedures data global variables (“static”) constants (“immutable”) symbols (import/export) Names interfaces references data

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 (prototypes)

.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

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.

But Java programs are interpreted They run on an “abstract machine” (e.g., JVM) implemented in software. ”bytecode”

mparison-of-dalvik-and-java-bytecode/

What’s the point? “Program” is an abstraction There are many different representations of programs, even of executable programs. Executable programs are compiled and packaged to run on an abstract machine. Details of the program depend on the platform: the machine and system software. Abstraction(s) is/are crucial in computer systems because they help accommodate rapid change.

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 (“text”) constants initialized data Process Thread sections segments virtual memory

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 is interpreted in the context of theVAS. – Resolves to a location in machine memory

“Classic Linux Address Space” N

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 …

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.

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, diff, and an 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.

Program Running a program Can a program launch multiple running instances on the same platform?

Program Can a program launch multiple running instances on the same platform? Running a program It depends. On some platforms (e.g., Android) an app is either active or it is not.

Abstraction Separate: – Interface from internals – Specification from implementation Abstraction is a double-edged sword. – “Don’t hide power.” More than an interface… This course is (partly) about the use of abstraction(s) in complex software systems. We want abstractions that are simple, rich, efficient to implement, and long-lasting.

Interface and abstraction

Abstraction(s) A means to organize knowledge – Capture what is common and essential – Generalize and abstract away the details – Specialize as needed – Concept hierarchy A design pattern or element – Templates for building blocks – Instantiate as needed E.g.: class, subclass, and instance

Standards, wrappers, adapters “Plug-ins” “Plug-compatible” Another layer of software can overcome superficial or syntactic differences if the fundamental are right.

Virtualization?