Programs – Loading and Running an Executable

Slides:



Advertisements
Similar presentations
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Advertisements

Algorithms and data structures
Computer Architecture CSCE 350
Linking & Loading CS-502 Operating Systems
User-Level Memory Management in Linux Programming
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
C and Data Structures Baojian Hua
Run-time Environment and Program Organization
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Memory Layout C and Data Structures Baojian Hua
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Software Development and Software Loading in Embedded Systems.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
Topic 2d High-Level languages and Systems Software
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Memory – Virtual Memory, Virtual Machines
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
CS/COE 0449 (term 2174) Jarrett Billingsley
Virtual Memory: Implementing Paging
Programs, Instructions, and Registers
CS/COE 0449 (term 2174) Jarrett Billingsley
Protecting Memory What is there to protect in memory?
User-Written Functions
CS/COE 0447 (term 2181) Jarrett Billingsley
Virtual Memory: the Page Table and Page Swapping
Process Memory COMP 40: Machine Structure and
Protecting Memory What is there to protect in memory?
Linking & Loading.
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
Run-time organization
Swapping Segmented paging allows us to have non-contiguous allocations
Programs – Dynamic Linking and Loading
CS-3013 Operating Systems C-term 2008
Processes in Unix, Linux, and Windows
C Basics.
Programs – Dynamic Linking and Loading
Processes in Unix, Linux, and Windows
CS 0449 Jarrett Billingsley
Processes in Unix, Linux, and Windows
Memory Allocation CS 217.
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Computer Organization and Design Assembly & Compilation
CSCE 313 – Introduction to UNIx process
Linking & Loading CS-502 Operating Systems
Operating System Chapter 7. Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Outline Chapter 2 (cont) OS Design OS structure
Programs – Loading and Running an Executable
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Operating Systems: A Modern Perspective, Chapter 6
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
System calls….. C-program->POSIX call
Linking & Loading CS-502 Operating Systems
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
Week 7 - Friday CS222.
Presentation transcript:

Programs – Loading and Running an Executable CS/COE 0449 (term 2174) Jarrett Billingsley

Class announcements Project 2!!!! Due Wednesday, March 1st by midnight. Fixed some slides. Check your email! Laid out the dates for the quizzes and projects. Check the schedule! Next quiz is in 2 weeks... 2/14/2017 CS/COE 0449 term 2174

Question time 2/14/2017 CS/COE 0449 term 2174

Function pointers??? 2/14/2017 CS/COE 0449 term 2174

Okay, why are they useful? Dynamically loaded libraries are one reason, but... They're not THAT common. (Though you'll have a lab to get comfortable with em.) But being able to pass functions as arguments to other functions... Now THAT is cool. Let's have a look at a small example using the qsort function from stdlib.h. qsort takes a pointer to a comparator function which must follow a set of rules to make qsort work properly. This brings up a couple small things to talk about... 2/14/2017 CS/COE 0449 term 2174

const void*? const char* s = "hello"; s = "goodbye"; // fine! A void* is a "universal pointer." It can point to anything. An int* points at ints, a FILE* points at files... A void* can be assigned any other type of pointer. You can't read from a void* without casting (next slide!). For any type T, a const T* is a read-only pointer to a T. You can read the data that it points to, but you can't write it. You can however change what the pointer points to. const char* s = "hello"; s = "goodbye"; // fine! s[3] = 'x'; // error! 2/14/2017 CS/COE 0449 term 2174

Pointer casts float f = 3.567; int* p = (int*)&f; // points to f... A cast looks like (type)value. You've used them before to do stuff like: double fstop = (double)numerator / denominator; Since all pointers are memory addresses, they're all the same size. You can cast one pointer type to another. float f = 3.567; int* p = (int*)&f; // points to f... printf("%x\n", *p); // interprets f as an int! The computer does not care what bits represent. We do! If you have a void*, you must cast it to another type if you want to use the data that it points to. 2/14/2017 CS/COE 0449 term 2174

Executable files 2/14/2017 CS/COE 0449 term 2174

What the heck is in an executable file? We looked inside an object file, which has more or less all the same pieces that an executable file (or shared library) has: Machine code ("text") Data Symbol table(s) OS-specific info objdump can inspect executables as well! This is because both object files and executables use almost exactly the same format. Well, on most flavors of UNIX anyway... 2/14/2017 CS/COE 0449 term 2174

Executable formats Most flavors of UNIX use ELF (executable and linkable format) executables have no extension; shared libraries are .so files objects are .o files; statically linked libraries are .a files ...with the exception of MacOS, which uses Mach-O shared libraries are .dylib files; otherwise the same as UNIX! Finally, Windows uses PE (portable executable) executables are .exe files; shared libraries are .dll files No standard object/static library format – MS compilers use their own format (based on COFF); gcc actually uses ELF. Each of these formats is broadly similar, but each operating system works differently and so can require different information. 2/14/2017 CS/COE 0449 term 2174

Load shared objects and copy into RAM Copy code/data into RAM The final countdown The last step is loading. The loader is the part of the OS that, uh, loads the executable. It sets up everything so that we can start running it! Load shared objects and copy into RAM yes Open executable file Copy code/data into RAM Dynamic libs? Set up OS resources no Start a new process! 2/14/2017 CS/COE 0449 term 2174

Processes 2/14/2017 CS/COE 0449 term 2174

All my children OS Kernel Code Memory Code Memory To manage running programs, the OS uses processes. A process is the in-memory representation of the program, all its data, its resources (like open files), and so on. The OS's main responsibility is controlling processes' access to resources. The OS wants everyone to play nicely! And if they don't... it's lights out for the process. ps and pstree let you see what processes are running! Code Memory Process 1 Code Memory Process 2 Ugh, I'm dead. HEY! NO TOUCHING! OS Kernel 2/14/2017 CS/COE 0449 term 2174

Address Spaces Each process gets its own address space: a part of memory that it alone is allowed to access. This prevents malfunctioning or malicious programs from affecting other programs or the OS itself! Older CPU architectures did this with memory segmentation. This is the origin of the term "segmentation fault" or "segfault"! On every memory access, the CPU would check if the current process was allowed to access that part of memory. Modern architectures instead use virtual memory! 2/14/2017 CS/COE 0449 term 2174

Clearly this is impossible. But the programs believe it's true. Virtual memory Virtual memory is an illusion: the OS makes every process believe that it's the only program running on the entire computer! Physical Memory Code Memory Process 1 0x8000 0xFFFF Process 2 ... Process 2's memory Process 1's memory 0x0066_7FFF 0x0066_0000 0x0040_FFFF Clearly this is impossible. But the programs believe it's true. 0x0040_8000 2/14/2017 CS/COE 0449 term 2174

One process's address space So what's a (virtual) address space look like? Kinda like this, usually. The code (or the text segment) usually resides at the bottom of memory (the lowest addresses). Not usually at address 0, though. Then comes the global data segment. Both the code and globals are more or less copied directly from the executable file. We already saw how the stack and heap grow towards each other! But how did they get there, and how do they get bigger...? Stack Heap Globals Code 2/14/2017 CS/COE 0449 term 2174

Shattered memories Physical memory is a scarce resource. Therefore, the OS manages it. It does this by splitting up memory into pages: manageable chunks that can be allocated to processes. Usually these days, each page is 4KiB. The OS maintains a page table which keeps track of which parts of memory belong to which process. The page table is actually way more complex than this, and handled by the CPU hardware... But that's CS1550/1541 stuff! Page Physical Address Owning Process ... 10 0000A000 <OS> 9 00009000 8 00008000 bash 7 00007000 6 00006000 5 00005000 ls 4 00004000 ps 3 00003000 exifview 2 00002000 1 00001000 00000000 2/14/2017 CS/COE 0449 term 2174

Thanks! Someone else can use it. Honey, I blew up the heap This is how programs can dynamically change the size of the stack and heap! When you use malloc... Your program might ask the OS for more pages to be allocated onto its heap. When you use free... Your program might give those pages back. Those mights are in there because how memory allocation works is... complicated. Stack Heap Gimme a page! OK! I'm done with this. OS Kernel Thanks! Someone else can use it. 2/14/2017 CS/COE 0449 term 2174