Top Banner
Talking to Threads
23

Talking to Threads. Administriva Grade summary today.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Talking to Threads. Administriva Grade summary today.

Talking to Threads

Page 2: Talking to Threads. Administriva Grade summary today.

Administriva•Grade summary today

Page 3: Talking to Threads. Administriva Grade summary today.

Grade Summ: Histogram

Page 4: Talking to Threads. Administriva Grade summary today.

Project trends

Page 5: Talking to Threads. Administriva Grade summary today.

Main routine: Dijkstra’sfunction 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);}

}

Page 6: Talking to Threads. Administriva Grade summary today.

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...

Page 7: Talking to Threads. Administriva Grade summary today.

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...

Page 8: Talking to Threads. Administriva Grade summary today.

Back to Threads

Page 9: Talking to Threads. Administriva Grade summary today.

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.

Page 10: Talking to Threads. Administriva Grade summary today.

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

Page 11: Talking to Threads. Administriva Grade summary today.

Use of wait listsDataBucket 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(); }

Page 12: Talking to Threads. Administriva Grade summary today.

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!

Page 13: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

Page 14: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

file/tmp/foo

write()

read()

close()

unlink()

open(“/tmp/foo”, O_RDWR | O_CREAT);

Page 15: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

open(“/tmp/foo”, O_RDWR | O_CREAT);

file/tmp/foo

write()

read()

close()

unlink()

hostile proc

open(...)

read()

Page 16: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

open(“/tmp/foo”, O_RDWR | O_CREAT);

file/tmp/foo

write()

read()

close()

unlink()

hostile proc

chmod()

Page 17: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

open(“/tmp/foo”, O_RDWR | O_CREAT);

file/tmp/foo

write()

read()

close()

unlink()

hostile proc

chmod()

open(...)

Page 18: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

open(“/tmp/foo”, O_RDWR | O_CREAT);

file/tmp/foo

write()

read()

close()

unlink()

hostile procumask()

Page 19: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

open(“/tmp/foo”, O_RDWR | O_CREAT);

file/tmp/foo

write()

read()

close()

unlink()

hostile procumask() open(...)

read()

Page 20: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

open(“/tmp/foo”, O_RDWR | O_CREAT);

file/tmp/foo

write()

read()

close()

unlink()

hostile procumask()

symlink(“/tmp/foo”, “/etc/passwd”)

Page 21: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

stat(“/tmp/foo”);if (!exists) {

open(“/tmp/foo”, O_RDWR | O_CREAT);} else { error(); }

file/tmp/foo

write()

read()

close()

unlink()

hostile procumask()

Page 22: Talking to Threads. Administriva Grade summary today.

Basic Race Cond. Exploitpriv proc

stat(“/tmp/foo”);if (!exists) {

open(“/tmp/foo”, O_RDWR | O_CREAT);} else { error(); }

file/tmp/foo

write()

read()

close()

unlink()

hostile procumask()

symlink(“/tmp/foo”, “/etc/passwd”)

Page 23: Talking to Threads. Administriva Grade summary today.

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!