a software view
play

A software view User Interface Computer Systems MTSU CSCI 3240 - PDF document

A software view User Interface Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll Materials from CMU and Dr. Butler How it works The Compilation system gcc is the compiler driver hello.c program gcc invokes several other


  1. A software view User Interface Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll Materials from CMU and Dr. Butler How it works The Compilation system gcc is the compiler driver hello.c program gcc invokes several other compilation phases Preprocessor 1) #include <stdio.h> int main() Compiler 2) { Assembler 3) printf(“hello, world\n”); Linker 4) } What does each one do? What are their outputs? printf.o Pre- hello.c hello.i Compiler hello.s Assembler hello.o Linker hello processor ( cc1 ) ( as ) ( ld ) ( cpp ) Source Assembly Modified Relocatable Executable program program source object object (text) (text) program programs program (text) (binary) (binary) 1. Preprocessor (cpp) 1. Preprocesser Included files: First, gcc compiler driver invokes cpp to generate expanded C source #include <foo.h> #include “bar.h” ! cpp just does text substitution ! Converts the C source file to another C source file Defined constants: ! Expands #defines, #includes, etc. #define MAXVAL 40000000 ! Output is another C source By convention, all capitals tells us it’s a constant, not a variable. Macros: #define MIN(x,y) ((x)<(y) ? (x):(y)) #define RIDX(i, j, n) ((i) * (n) + (j))

  2. 1. Preprocesser 2. Compiler (cc1) Conditional compilation: Next, gcc compiler driver invokes cc1 to generate assembly code #ifdef … or #if defined( … ) ‏ #endif ! Translates high-level C code into assembly ! Code you think you may need again (e.g. debug print " Variable abstraction mapped to memory locations and registers statements) ‏ " Logical and arithmetic functions mapped to underlying machine opcodes " Include or exclude code based on #define/#ifdef " More readable than commenting code out ! Portability 3. Assembler (as) " Compilers have “built in” constants defined " Operating system specific code Next, gcc compiler driver invokes as to generate object » #if defined(__i386__) || defined(WIN32) || … " Compiler-specific code code » #if defined(__INTEL_COMPILER) ! Translates assembly code into binary object code that can " Processor-specific code be directly executed by CPU » #if defined(__SSE__) 4. Linker (ld) Summary of compilation process Finally, gcc compiler driver calls linker (ld) to generate Compiler driver (cc or gcc) coordinates all steps executable ! Invokes preprocessor ( cpp ), compiler ( cc1 ), assembler ( as ), and linker ( ld ). ! Links together object code and static libraries to form final executable ! Passes command line arguments to appropriate phases a.o Libraries m.o libc.a Linker (ld) ‏ printf.o p This is the executable program Pre- hello.c hello.i Compiler hello.s Assembler hello.o Linker hello processor ( cc1 ) ( as ) ( ld ) ( cpp ) Source Assembly Modified Relocatable Executable program program source object object (text) (text) program programs program (text) (binary) (binary) Operating system functions The run-time system Protection Program runs on top of an operating system that ! Protects the hardware/itself from user programs implements ! Protects user programs from each other ! File system ! Protects files from unauthorized access ! Memory management Resource allocation ! Processes ! Memory, I/O devices, CPU time, space on disks ! Device management ! Network support ! etc.

  3. Operating system functions Unix file system Abstract view of resources Key concepts ! Files as an abstraction of storage devices ! Everything is a file ! System calls an abstraction for OS services " Keyboards, mice, CD-ROMS, disks, modems, networks, pipes, ! Virtual memory a uniform memory space abstraction for each sockets process " One abstraction for accessing most external things " Gives the illusion that each process has entire memory space ! A file is a stream of bytes with no other structure. ! A process (in conjunction with the OS) provides an abstraction for a virtual computer " on the hard disk or from an I/O device " Slices of CPU time to run in " Higher levels of structure are an application concept, not an operating system concept » No “records” (contrast with Windows/VMS) ‏ Unix file systems Process abstraction A fundamental concept of operating systems. Managed by OS on disk ! Dynamically allocates space for files A process is an instance of a program when it is running. ! A program is a file on the disk containing instructions to execute ! Implements a name space so we can find files ! A process is an instance of that program loaded in memory and ! Hides where the file lives and its physical layout on disk running ! Provides an illusion of sequential storage " Like you baking the cookies, following the instructions A process includes All we have to know to find a file is its name ! Code and data in memory, CPU state, open files, thread of execution How does a program get executed? Where are programs loaded in memory? The operating system creates a process. To start with, imagine a primitive operating system. # Single tasking. ! Including among other things, a virtual memory space # Physical memory addresses go from zero to N. System loader reads program from file system and The problem of loading is simple loads its code into memory # Load the program starting at address zero ! Program includes any statically linked libraries # Use as much memory as it takes. ! Done via DMA (direct memory access) ‏ # Linker binds the program to absolute addresses System loader loads dynamic shared objects/libraries Code starts at zero # into memory # Data concatenated after that Then it starts the thread of execution running # etc. ! Note: the program binary in file system remains and can be executed again

  4. Where are programs loaded, cont’d Where are programs loaded, cont’d Next imagine a multi-tasking operating system on a primitive Next, imagine a multi-tasking operating system on a computer. modern computer, with hardware-assisted virtual ! Physical memory space, from zero to N. memory ! Applications share space The OS creates a virtual memory space for each user’s ! Memory allocated at load time in unused space program. ! Linker does not know where the program will be loaded ! As though there is a single user with the whole memory all to ! Binds together all the modules, but keeps them relocatable itself. How does the operating system load this program? Now we’re back to the simple model ! Not a pretty solution, must find contiguous unused blocks ! The linker statically binds the program to virtual addresses How does the operating system provide protection? ! At load time, the operating system allocates memory, creates ! Not pretty either a virtual address space, and loads the code and data. ! Binaries are simply virtual memory snapshots of programs Example memory map The memory hierarchy Nothing is left relocatable, no relocation at load time Operating system and CPU memory management unit gives each process the “illusion” of a uniform, 0xffffffff kernel virtual memory memory dedicated memory space (code, data, heap, stack) invisible to user code 0xc0000000 ! i.e. 0x0 – 0xFFFFFFFF for IA32 user stack (created at runtime) ! Allows multitasking %esp (stack pointer) ‏ ! Hides underlying non-uniform memory hierarchy memory mapped region for shared libraries 0x40000000 brk run-time heap (managed by malloc) read/write segment (.data, .bss) loaded from the read-only segment executable file (.init, .text, .rodata) 0x08048000 unused 0 Memory heirarchy motivation The memory heirarchy In 1980 ! CPUs ran at around 1 mhz. Smaller L0 ! A memory access took about as long as a CPU instruction Registers Faster ! Memory was not a bottleneck to performance More Expensive L1 Level 1 Cache On Chip Today ! CPUs are about 3000 times faster than in 1980 L2 Level 2 Cache (off chip) ‏ ! DRAM Memory is about 10 times faster than in 1980 Larger Slower L3 We need a small amount of faster, more expensive Main Memory Cheaper memory for stuff we’ll need in the near future ! How do you know what you’ll need in the future? L4 Local Secondary Storage ! Locality ! L1, L2, L3 caches L5 Remote Secondary Storage

Recommend


More recommend