Download presentation
Presentation is loading. Please wait.
1
Talking to Threads
2
Administriva Grade summary today
3
Grade Summ: Histogram
4
Project trends
5
Main routine: Dijkstra’s function dijkstraSSP( G, w, s ): // G : complete graph // w : weight function // s : source node (start location) initializeSSPDataStructs(); Set S=new Set(); Queue Q=new PriorityQueue( G. V, d ); while (!Q.isEmpty()) { Vertex u =Q.extractMin(); S.add( u ); for (Iterator i= u.neighbors();i.hasNext();) { Vertex v =i.next(); relax( u, v, w ); }
6
Group questions: Give an example of this alg in action on a (small) map from JCiv How long does this take to run? What real data structs should you use for S and Q? Can you take any shortcuts? What’s wrong w/ negative-weight edges? Is this guaranteed to get the correct shortest path? Why? (Proof!) Show that d[v] is the correct shortest path when v is inserted in Set S and that d[v] never changes thereafter. Hint: think proof by contradiction...
7
Group questions: Give an example of this alg in action on a (small) map from JCiv How long does this take to run? What real data structs should you use for S and Q? Can you take any shortcuts? What’s wrong w/ negative-weight edges? Is this guaranteed to get the correct shortest path? Why? (Proof!) Show that d[v] is the correct shortest path when v is inserted in Set S and that d[v] never changes thereafter. Hint: think proof by contradiction...
8
Back to Threads
9
wait()ing for Godot... sleep(n) is good if you know how long you want to delay What if a thread wants to wait indefinately, or for some other thread to do something? Check out Object API: Object o.wait() -- wait until some other object tells thread to wake up o.notify() -- wake up one (random) thread that is waiting on o o.notifyAll() -- wake up every thread that is waiting on o.
10
More BtS Java Every Object in Java has a (single) “wait list”. When a thread adds itself to an Object’s wait list, the thread suspends Some other thread of execution can then call that Object’s notify() method One thread wakes up and can now take action Does not generate an exception; does not change the status of a sleep()ing thread Must be synchronized on target object to wait() or notify() Prevents collisions while messing w/ wait list
11
Use of wait lists DataBucket b; Thread t=new Thread(new Runnable(b) { public Runnable(DataBucket buck) { _bucket=b; } public void run() { while (!bored) { while (_bucket.isEmpty()) { synchronized(_bucket) { _bucket.wait(); } _bucket.getData(); } }}); // elsewhere... b.addData(data); synchronized(b) { b.notify(); }
12
Race Cond. & Security Atomicity failures can sometimes be exploited to break security on multiprocessing systems One of the top 10 classes of exploits since... mid-1980’s, at least 100’s (or more) of reported vulnerabilities Independent of language: Java will not save you! Hostile program grabs a shared resource (e.g., file) before it is secured Beware when writing privileged code! N.b.: Sometimes your never-intended-to- be- secure code will be run in privileged context!
13
Basic Race Cond. Exploit priv proc
14
Basic Race Cond. Exploit priv proc file /tmp/foo write() read() close() unlink() open(“/tmp/foo”, O_RDWR | O_CREAT);
15
Basic Race Cond. Exploit priv proc open(“/tmp/foo”, O_RDWR | O_CREAT); file /tmp/foo write() read() close() unlink() hostile proc open(...) read()
16
Basic Race Cond. Exploit priv proc open(“/tmp/foo”, O_RDWR | O_CREAT); file /tmp/foo write() read() close() unlink() hostile proc chmod()
17
Basic Race Cond. Exploit priv proc open(“/tmp/foo”, O_RDWR | O_CREAT); file /tmp/foo write() read() close() unlink() hostile proc chmod() open(...)
18
Basic Race Cond. Exploit priv proc open(“/tmp/foo”, O_RDWR | O_CREAT); file /tmp/foo write() read() close() unlink() hostile proc umask()
19
Basic Race Cond. Exploit priv proc open(“/tmp/foo”, O_RDWR | O_CREAT); file /tmp/foo write() read() close() unlink() hostile proc umask() open(...) read()
20
Basic Race Cond. Exploit priv proc open(“/tmp/foo”, O_RDWR | O_CREAT); file /tmp/foo write() read() close() unlink() hostile proc umask() symlink(“/tmp/foo”, “/etc/passwd”)
21
Basic Race Cond. Exploit priv proc stat(“/tmp/foo”); if (!exists) { open(“/tmp/foo”, O_RDWR | O_CREAT); } else { error(); } file /tmp/foo write() read() close() unlink() hostile proc umask()
22
Basic Race Cond. Exploit priv proc stat(“/tmp/foo”); if (!exists) { open(“/tmp/foo”, O_RDWR | O_CREAT); } else { error(); } file /tmp/foo write() read() close() unlink() hostile proc umask() symlink(“/tmp/foo”, “/etc/passwd”)
23
Preventing FS Race Conds Could create “foo” in dir owned/writable only by owner of proc Can be hard to ensure this Still have to watch out for filename collisions Could make file names hard to predict (e.g., picked randomly) Exploit still possible; hard to make fnames really random Ultimate answer: use OS atomicity facilities open(“/tmp/foo”, O_RDWR | O_CREAT | O_EXCL) Always be on guard!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.