DOC PREVIEW
UW CSE 303 - Linking Wrap-Up

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2007Lecture 21— Linking Wrap-Up; Concurrency Part 1Dan Grossman CSE303 Spring 2007, Lecture 21 1'&$%Where are we• Saw Java’s “very late” class-loading• In the middle of ld’s static-linking– Need to learn how arc hives (.a files) work• A bit on shared-libraries and dynamic-linkingThen concurrency:• Multiple threads of execution (call-stacks) at once!– Why, how, what goes wrong, how to c ontrol itDan Grossman CSE303 Spring 2007, Lecture 21 2'&$%LinkingIf a C file uses but does not define a function (or global variable) foo,then the .o has “unresolved references”. Declarations don’t count;only definitions.The linker takes multiple .o files and “patches them” to include thereferences. (It literally moves code and changes instructions likefunction calls.)An executable must have no unresolved references (you have seen thiserror message).What: Definitions of functions/variablesWhen: The linker creates an executableWhere: Other .o files on the command-line (and much more...)Dan Grossman CSE303 Spring 2007, Lecture 21 3'&$%More about whereThe linker and O/S don’t know anything about main or the C library.That’s why gcc “secretly” links in other things.We c an do it ourselves, but we would need to know a lot about howthe C library is organized. Get gcc to tell us:• gcc -v -static hello.c• Should be large ly understandable soon.• -static (stick with the simple “get all the code you need intoa.out story)• the secret *.o files: (they do the stuff before main gets called,which is why gcc gives errors about main not being defined).• -lc: complicated story about finding the library (a.k.a. “archive”)libc.a and including any files that provide still-unresolvedreferences.Dan Grossman CSE303 Spring 2007, Lecture 21 4'&$%ArchivesAn archive is the “.o equivalent of a .jar file” (though history is theother way around).Create with ar program (lots of features, but fundamentally take .ofiles and put them in, but order matters).The semantics of passing ld an argument like -lfoo is complicatedand often not what you want:• Look for what: file libfoo.a (ignoring shared libraries for now),when: at link-time, where: defaults, environment variables(LIBPATH ?) and the -L flags (analogous to -I).• Go through the .o files in libfoo.a in order.– If a .o defines a needed reference, include the .o.– Including a .o may add more needed references.– Continue.Dan Grossman CSE303 Spring 2007, Lecture 21 5'&$%The rulesA call to ld (or gcc for linking) has .o files and -lfoo options inleft-to-right order.• State: “Set of needed functions not defined” initially empty.• Action for .o file:– Include code in result– Remove from set any functions defined– Add to set any functions used and not yet defined• Action for .a file: For each .o in order– If it defines one or m ore functions in set, do all 3 things we dofor a .o file.– Else do nothing.• At end, if set is empty create executable, else error.Dan Grossman CSE303 Spring 2007, Lecture 21 6'&$%Library gotchas1. Position of -lfoo on command-line matters• Only resolves references for “things to the left”• So -lfoo typically put “on the right”2. Cycles• If two .o files in a .a need other other, you’ll have to link thelibrary in (at least) twice!• If two .a files need each other, you might do -lfoo -lbar-lfoo -lbar -lfoo ...• (There are command-line options to do this for you, but notthe default.)3. If you include math.h, then you’ll need -lm.Dan Grossman CSE303 Spring 2007, Lecture 21 7'&$%Another gotcha4. No repeated function names• 2 .o files in an executable can’t have (public) functions of thesame name.• Can get burned by library functions you do not know exist, butonly if you need another function from the same .o file.(Solution: 1 public function per file?!)Dan Grossman CSE303 Spring 2007, Lecture 21 8'&$%Beyond static linkingStatic linking has disadvantages:• More disk space (c opy library portions for every application)• More mem ory when programs are running (what if the O/S couldhave different processes magically share code).So we c an link later:• Shared libraries (link in when program starts executing). Savesdisk space. O/S can share actual memory behind your back(if/because code is immutable).• Dynamically linked/loaded libraries. Even later (while program isrunning). Devil is in the details.“DLL hell” – if the version of a library on a machine is not the one theprogram was tested with...Dan Grossman CSE303 Spring 2007, Lecture 21 9'&$%SummaryThings like “standard libraries” “header files” “linkers” etc. are notmagic.But since you rarely need fine-grained control, you easily forget how tocontrol typically-implicit things. (You don’t need to know any of thisuntil you need to. :) )There’s a huge difference between source code and compiled code (aheader file and an archive are quite different).The linker includes files from archives using strange rules.Dan Grossman CSE303 Spring 2007, Lecture 21 10'&$%ConcurrencyComputation whe re “multiple things happen at the same time” isinherently more com plicated than sequential computation.• Entirely new kinds of bugs and obligationsTwo forms of concurrency:• time-slicing: only one computation at a time but pre-empt toprovide responsiveness or mask I/O latency.• true parallelism: more than one CPU (e.g., the lab machines havetwo, the attu machines have 4, ...)No problem unless t he different c omputations need to communicate oruse the same resources.Dan Grossman CSE303 Spring 2007, Lecture 21 11'&$%Example: ProcessesThe O/S runs m ultiple processes “at once”.Why? (Convenience, e fficient use of resources, performance)No problem: keep their address-spaces separate.But they do communicate/share via file s (and pipes).Things c an go wrong, e.g., a race condition:echo "hi" > someFilefoo=‘cat someFile‘# assume foo holds the string hi??The O/S provides synchronization mechanisms to avoid this• See CSE451; we will focus on intraprocess concurrency.Dan Grossman CSE303 Spring 2007, Lecture 21 12'&$%The Old StoryWe s aid a running Java or C program had c ode, a heap, globalvariables, a stack, and “what is executing right now” (in assembly, aprogram counter).C, Java support parallelism similarly (other languages can be different):• One pile of code, global variables, and heap.• Multiple


View Full Document

UW CSE 303 - Linking Wrap-Up

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Linking Wrap-Up
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Linking Wrap-Up and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Linking Wrap-Up 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?