DOC PREVIEW
UW CSE 303 - Lecture Notes

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 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 18 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 18 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 18 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 18 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 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanWinter 2006Lecture 22— Linking Wrap-up; Threads, concurrencyDan Grossman CSE303 Winter 2006, Lecture 22 1'&$%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 Winter 2006, Lecture 22 2'&$%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 Winter 2006, Lecture 22 3'&$%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 ...3. If you include math.h, then you’ll need -lm.Dan Grossman CSE303 Winter 2006, Lecture 22 4'&$%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 Winter 2006, Lecture 22 5'&$%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 Winter 2006, Lecture 22 6'&$%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 t o 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 Winter 2006, Lecture 22 7'&$%Our Old ModelSo far, a process (a running program) has:• a stack• a heap• code• global variablesOther processes have a separate address space. The O/S takes turnsrunning processes on one or more proc ess ors.Interproce ss communication happens via the file system, pipes, andthings we don’t know about.Dan Grossman CSE303 Winter 2006, Lecture 22 8'&$%Inter-process racesForgetting about other processes can lead to programming mistakes:echo "hi" > someFilefoo=‘cat someFile‘# assume foo holds the string hi??A race condition is when this might occur.Processes sharing resources must synchronize; no time today to showyou how.But enough about processes; we’ll focus on intra-process threadsinstead and how you use locks in Java.Dan Grossman CSE303 Winter 2006, Lecture 22 9'&$%“Lightweight” ThreadsOne process can have multiple threads!Each thread has its own s tack.A scheduler runs threads one-or-more at a time.The difference from multiple processes is the threads share an addressspace – same heap, same globals.“Lightweight” because it’s easier for threads to communicate (justread/write to shared data).But easier to communicate means easier to mess each other up.(Also there are tough impleme ntation issues about where to putmultiple stacks.)Dan Grossman CSE303 Winter 2006, Lecture 22 10'&$%Shared MemoryNow races can happen if two threads could access the same memoryat the same time, and at least one access is a write.class A { String s; }class C {private A a;void m1() {if(a != null) // "dangerous" racea.s = "hi";}void m2() { a = null; }...}If you naively try to code away races, you will just add other races!!!Dan Grossman CSE303 Winter 2006, Lecture 22 11'&$%Concurrency primitivesDifferent languages/libraries for multithreading provide differentfeatures, but he re are the basics you can expect these days:• A way to create a new thread– See the run method of Java’s Thread class.• Locks (a way to acquire and release them).– A lock is available or held by a thread.– Acquiring a lock m akes the acquiring thread hold it, but theacquisition blocks (does not continue!) until the lock isavailable.– Releasing a lock make s the lock available.– Advanced note : Java locks are reentrant: reacquisition doesn’tblock, instead increments a hidden counter that releasedecrements...Dan Grossman CSE303 Winter 2006, Lecture 22 12'&$%Locks in JavaJava makes eve ry object a lock and combines acquire/release into onelanguage construct:synchronized (e) { s }• Evaluate e to an object.• “Acquire” the object (blocking until available).• Execute s.• Release the lock. The implementation of locks ensures no races onacquiring and releasing.Dan Grossman CSE303 Winter 2006, Lecture 22 13'&$%Fixing our exampleIf a C object might have m1 and m2 called simultaneously, then bothmust guard their access to a with the same lock.class C {private A a;void m1() {synchronized (this) {if(a != null) // "dangerous" racea.s = "hi";}}void m2() { synchronized (this) { a = null; } }}Note: There is more convenient syntax for this.Note: What if a is public and/or there are


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?