programs and processes jeff chase duke university
TRANSCRIPT
![Page 1: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/1.jpg)
Programs and Processes
Jeff ChaseDuke University
![Page 2: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/2.jpg)
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
![Page 3: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/3.jpg)
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?
![Page 4: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/4.jpg)
A simple C program
int main(){
}
![Page 5: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/5.jpg)
What’s in a program?
![Page 6: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/6.jpg)
What’s in a program?
codeinstructions (“text”)procedures
dataglobal variables (“static”)constants (“immutable”)
symbols (import/export)Namesinterfacesreferences
data
![Page 7: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/7.jpg)
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
![Page 8: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/8.jpg)
Calling the module
#include <stdio.h>
extern int p1();extern int p2();
int main(){ int i;
i = p2(); printf("%d\n", i);}
Program
P1()
P2()
P3()
P4()
state
interfacesignatures
(prototypes)
![Page 9: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/9.jpg)
.section__TEXT,__text,regular,pure_instructions
.globl _p1
.align 4, 0x90_p1: ## @p1
.cfi_startproc## BB#0:
pushq %rbpLtmp2:
.cfi_def_cfa_offset 16Ltmp3:
.cfi_offset %rbp, -16movq %rsp, %rbp
Ltmp4:.cfi_def_cfa_register %rbpmovl $1, %eaxmovq %rdi, -8(%rbp)popq %rbpret.cfi_endproc
.globl _p2
.align 4, 0x90_p2: ## @p2
.cfi_startproc….
ret.cfi_endproc
.section__TEXT,__cstring,cstring_literalsL_.str: ## @.str
.asciz "hello\n"
.comm _val,4,2 ## @val
.subsections_via_symbols
![Page 10: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/10.jpg)
Global data (“static”)
int g;int g0 = 0;int g1 = 1;
.globl _g0 ## @g0.zerofill __DATA,__common,_g0,4,2
.section__DATA,__data
.globl _g1 ## @g1
.align 2_g1:
.long 1 ## 0x1
.comm _g,4,2 ## @g
![Page 11: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/11.jpg)
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
datadatadata
libraries and other
objectfiles or
archives
header files
![Page 12: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/12.jpg)
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
dataidata
wdata
header
symboltable
relocationrecords
program instructionsp
immutable data (constants)“hello\n”
writable global/static dataj, s
j, s ,p,sbuf
Header “magic number”indicates type of file/image.
Section table an arrayof (offset, len, startVA)
sections
Used by linker; may be removed after final link step and strip. Also includes info for debugger.
![Page 13: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/13.jpg)
http://www.media-art-online.org/java/help/how-it-works.html
But Java programs are interpretedThey run on an “abstract machine” (e.g., JVM) implemented in software.
”bytecode”
![Page 14: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/14.jpg)
![Page 15: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/15.jpg)
http://forensics.spreitzenbarth.de/2012/08/27/comparison-of-dalvik-and-java-bytecode/
![Page 16: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/16.jpg)
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.
![Page 17: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/17.jpg)
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
![Page 18: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/18.jpg)
0x0
0x7fffffff
Static data
Dynamic data(heap/BSS)
Text(code)
Stack
ReservedVAS 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
![Page 19: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/19.jpg)
“Classic Linux Address Space”
http://duartes.org/gustavo/blog/category/linux
N
![Page 20: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/20.jpg)
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
![Page 21: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/21.jpg)
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
![Page 22: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/22.jpg)
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
![Page 23: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/23.jpg)
const1=1const2=0main
tmp=1RA=0x804838cA
RA=0x8048361B
const=0RA=0x8048354C
tmp=0RA=0x8048347A
0xfffffff
0x0
Memory
void C () { A (0);}
void B () { C ();}
void A (int tmp){ if (tmp) B ();}
int main () { A (1); return 0;}
0x8048347
0x8048354
0x8048361
0x804838c
Code Stack
…
SP
SP
SP
SP
SP
![Page 24: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/24.jpg)
const1=3const2=0main
bnd=3RA=0x804838cA
bnd=2RA=0x8048361A
bnd=1RA=0x8048361A
bnd=0RA=0x8048361A
0xfffffff
0x0
Memory
void A (int bnd){ if (bnd) A (bnd-1);}
int main () { A (3); return 0;}
0x8048361
0x804838c
Code Stack
…
SP
SP
SP
SP
SP
How can recursion go wrong?Can overflow the stack …Keep adding frame after frame
…
![Page 25: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/25.jpg)
wrd[3]wrd[2]wrd[1]wrd[0]
const2=0
main
b= 0x00234RA=0x804838ccap
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;}
0x8048361
0x804838c
Code Stack
…SP
SP
0x00234What can go wrong?Can overflow wrd variable …Overwrite cap’s RA
…
![Page 26: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/26.jpg)
Assembler directives: quick peekFrom 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.
![Page 27: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/27.jpg)
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.
![Page 28: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/28.jpg)
Program
Running a program
Can a program launch multiple running instances on the same platform?
![Page 29: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/29.jpg)
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.
![Page 30: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/30.jpg)
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.
![Page 31: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/31.jpg)
Interface and abstraction
![Page 32: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/32.jpg)
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
![Page 33: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/33.jpg)
Standards, wrappers, adapters
“Plug-ins”“Plug-compatible”
Another layer of software can overcome superficial or syntactic differences if the fundamental are right.
![Page 34: Programs and Processes Jeff Chase Duke University](https://reader035.vdocuments.net/reader035/viewer/2022081514/56649e7b5503460f94b7bb8c/html5/thumbnails/34.jpg)
Virtualization?