Professional Documents
Culture Documents
Lec07 Synchronization
Lec07 Synchronization
queue
name, child_pid);
Master
return 0;
Thread
} else {
printf(“I am the parent %s. My child is %d\n”,
name, child_pid); Thread Pool
return 0;
master() { worker(queue) {
}
} allocThreads(worker,queue); while(TRUE) {
_______________________________ while(TRUE) { con=Dequeue(queue);
% ./forktest con=AcceptCon(); if (con==null)
Child of forktest sees PID of 0 Enqueue(queue,con); sleepOn(queue);
I am the parent forktest. My child is 486 wakeUp(queue); else
} ServiceWebPage(con);
}
}
}
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.3 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.4
Recall: Common Notions of Thread Creation Tips for Programming in a Project Team
• cobegin/coend • Statements in block may run in parallel • Big projects require more than one
cobegin
job1(a1);
• cobegins may be nested person (or long, long, long time)
job2(a2); • Scoped, so you cannot have a missing coend – Big OS: thousands of person-years!
• It’s very hard to make software
coend
• fork/join
tid1 = fork(job1, a1);
• Forked procedure runs in parallel
project teams work correctly
• Wait at join point if it’s not finished
job2(a2);
join tid1; – Doesn’t seem to be as true of big
• future construction projects
• Future possibly evaluated in parallel
v = future(job1(a1)); » Empire state building finished in
• Attempt to use return value will wait
… = …v…; one year: staging iron production
thousands of miles away
• forall • Separate thread launched for each iteration » Or the Hoover dam: built towns to
hold workers
forall(I from 1 to N)
C[I] = A[I] + B[I] • Implicit join at end
end
“You just have – Is it OK to miss deadlines?
to get your » We make it free (slip days)
• Threads expressed in the code may not turn into independent
computations synchronization right!” » Reality: they’re very expensive as
– Only create threads if processors idle time-to-market is one of the most
important things!
– Example: Thread-stealing runtimes such as cilk
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.5 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.6
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.9 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.10
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.11 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.12
Use Software Tools Test Continuously
• Source revision control software • Integration tests all the time, not at 11pm
on due date!
– Git – check in frequently. Tag working code – Utilize Cucumber features to test frequently!
– Easy to go back and see history/undo mistakes – Write dummy stubs with simple functionality
– Work on independent branches for each feature » Let’s people test continuously, but more work
» Merge working features into your master branch – Schedule periodic integration tests
» Consider using “rebase” as well » Get everyone in the same room, check out code, build, and
– Communicates changes to everyone test.
» Don’t wait until it is too late!
• Redmine » This is exactly what the autograder does!
– Make use of Bug reporting and tracking features! • Testing types:
– Use Wiki for communication between teams – Integration tests: Use of Cucumber with BDD
– Also, consider setting up a forum to leave information – Unit tests: check each module in isolation (CPP Unit)
for one another about the current state of the design – Daemons: subject code to exceptional cases
• Use automated testing tools – Random testing: Subject code to random timing changes
– Rebuild from sources frequently • Test early, test later, test again
– Run Cucumber tests frequently – Tendency is to test once and forget; what if something
» Use tagging features to run subsets of tests! changes in some other part of the code?
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.13 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.14
• How did the first design document go? • One thread per transaction, each running:
• How did design reviews go? Deposit(acctId, amount) {
acct = GetAccount(actId); /* May use disk I/O */
• Recall: Midterm I: Three weeks from today! acct->balance += amount;
StoreAccount(acct); /* Involves disk I/O */
– Wednesday 3/13 }
– Intention is a 1.5 hour exam over 3 hours • Unfortunately, shared state can get corrupted:
– No class on day of exam! Thread 1 Thread 2
• Midterm Timing: load r1, acct->balance
load r1, acct->balance
– Listed as 6:00-9:00PM add r1, amount2
– Could also be: 4:00-7:00pm store r1, acct->balance
add r1, amount1
– Preferences? (I need to get a room for the exam) store r1, acct->balance
• Topics: everything up to the previous Monday • Atomic Operation: an operation that always runs to
– OS Structure, BDD, Process support, Synchronization, completion or not at all
Scheduling, Memory Management, I/O – It is indivisible: it cannot be stopped in the middle and state
cannot be modified by someone else in the middle
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.15 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.16
Review: Ways of entering the kernel/
changing the flow of control Interrupt Controller
• The Timer Interrupt:
Interrupt Mask
Priority Encoder
– Callbacks scheduled to be called when timer expires IntID
– Cause of scheduler events – change which process of thread is CPU
running
Interrupt Int Disable
Timer
• System Calls
– Controlled function call into kernel from user space
– User-level code stops, kernel-level code
– What about asynchronous system calls? Software Control
Interrupt NMI
• Normal Interrupts Network
– Entered via hardware signal • Interrupts invoked with interrupt lines from devices
– Typically Asynchronous to the instruction stream • Interrupt controller chooses interrupt request to honor
– Often structured in some sort of hierarchy (some interrupts – Mask enables/disables interrupts
higher priority than others
– Priority encoder picks highest enabled interrupt
• Exceptions:
– Software Interrupt Set/Cleared by Software
– Instruction execution fails for some reason
– Interrupt identity specified with ID line
– Typically Synchronous to the instruction stream
• CPU can disable all interrupts with internal flag
• Non-maskable interrupt line (NMI) can’t be disabled
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.17 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.18
• No more global cli()! • Key idea: maintain a lock variable and impose mutual
exclusion only during operations on that variable
– Used to be that cli()/sti() could be used to enable and
disable interrupts on all processors
– First deprecated (2.5), then removed (2.6) int value = FREE;
» Could serialize device drivers across all processors!
» Just a bad idea Acquire() { Release() {
disable interrupts; disable interrupts;
– Better option?
if (value == BUSY) { if (anyone on wait queue) {
» Fine-grained spin-locks between processors (more later) put thread on wait queue; take thread off wait queue
» Local interrupt control for local processor Go to sleep(); Place on ready queue;
} else {
• Disabling specific interrupt (nestable) // Enable interrupts?
value = FREE;
disable_irq(irq); // Wait current handlers } else {
}
disable_irq_nosync(irq); // Don’t waitcurrent handler value = BUSY; enable interrupts;
enable_irq(irq); // Reenable line } }
synchronize_irq(irq); // Wait for current handler enable interrupts;
}
– Not great for buses with multiple interrupts per line, such as
PCI! More when we get into device drivers.
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.21 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.22
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.29 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.30
2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.31 2/20/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 7.32
Summary (Synchronization)
• Important concept: Atomic Operations
– An operation that runs to completion or not at all
– These are the primitives on which to construct various
synchronization primitives
• Talked about hardware atomicity primitives:
– Disabling of Interrupts, test&set, swap, comp&swap,
load-linked/store conditional
• Showed several constructions of Locks
– Must be very careful not to waste/tie up machine
resources
» Shouldn’t disable interrupts for long
» Shouldn’t spin wait for long
– Key idea: Separate lock variable, use hardware
mechanisms to protect modifications of that variable
• Started talking abut higher level constructs that are
harder to “screw up”