Unformatted text preview:

Goals for Today Concurrency examples Need for synchronization Examples of valid synchronization CS162 Operating Systems and Systems Programming Lecture 6 Synchronization February 4 2010 Ion Stoica http inst eecs berkeley edu cs162 Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Gagne Many slides generated from my lecture notes by Kubiatowicz 2 4 10 Review Why allow cooperating threads CS162 UCB Fall 2009 Lec 6 2 Review Threaded Web Server People cooperate computers help enhance people s lives so computers must cooperate By analogy the non reproducibility non determinism of people is a notable problem for carefully laid plans Advantage 1 Share resources One computer many users One bank balance many ATMs Multithreaded version serverLoop connection AcceptCon ThreadFork ServiceWebPage connection What if ATMs were only updated at night Embedded systems robot control coordinate arm hand Advantage 2 Speedup Overlap I O and computation Advantages of threaded version Many different file systems do read ahead Can share file caches kept in memory results of CGI scripts other things Threads are much cheaper to create than processes so this has a lower per request overhead Multiprocessors chop up program into parallel pieces Advantage 3 Modularity More important than you might think Chop large problem up into simpler pieces 2 4 10 What if too many requests come in at once To compile for instance gcc calls cpp cc1 cc2 as ld Makes system easier to extend CS162 UCB Fall 2009 2 4 10 Lec 6 3 Page 1 CS162 UCB Fall 2009 Lec 6 4 Review Thread Pools ATM Bank Server Problem with previous version Unbounded Threads When web site becomes too popular throughput sinks Instead allocate a bounded pool of threads representing the maximum level of multiprogramming queue Master Thread Thread Pool slave queue master while TRUE allocThreads slave queue con Dequeue queue while TRUE if con null con AcceptCon sleepOn queue Enqueue queue con else wakeUp queue ServiceWebPage con 2 4 10 CS162 UCB Fall 2009 Lec 6 5 ATM server problem Service a set of requests Do so without corrupting database Don t hand out too much money 2 4 10 ATM bank server example Suppose we wanted to implement a server process to handle requests from an ATM network Still like to overlap I O with computation Without threads we would have to rewrite in eventdriven style Example BankServer while TRUE event WaitForNextEvent if event ATMRequest StartOnRequest else if event AcctAvail ContinueRequest else if event AcctStored FinishRequest How could we speed this up What if we missed a blocking I O step What if we have to split code into hundreds of pieces which could be blocking This technique is used for graphical programming More than one request being processed at once Event driven overlap computation and I O Multiple threads multi proc or overlap comp and I O CS162 UCB Fall 2009 Lec 6 6 Event Driven Version of ATM server Suppose we only had one CPU BankServer while TRUE ReceiveRequest op acctId amount ProcessRequest op acctId amount ProcessRequest op acctId amount if op deposit Deposit acctId amount else if Deposit acctId amount acct GetAccount acctId may use disk I O acct balance amount StoreAccount acct Involves disk I O 2 4 10 CS162 UCB Fall 2009 2 4 10 Lec 6 7 Page 2 CS162 UCB Fall 2009 Lec 6 8 Can Threads Make This Easier Administrivia Threads yield overlapped I O and computation without deconstructing code into non blocking fragments Should be working on first project One thread per request Requests proceeds to completion blocking as required Deposit acctId amount acct GetAccount actId May use disk I O acct balance amount StoreAccount acct Involves disk I O Design Document Information up on the Nachos page Important inclusion Testing methodology Unfortunately shared state can get corrupted Thread 1 load r1 acct balance 2 4 10 Give us a strategy for testing your code We will be grading your methodology in the document Thread 2 If you need to know more about synchronization primitives before I get to them use book load r1 acct balance add r1 amount2 store r1 acct balance add r1 amount1 store r1 acct balance CS162 UCB Fall 2009 Chapter 6 in 7th 8th edition are all about synchronization 2 4 10 Lec 6 9 Review Multiprocessing vs Multiprogramming What does it mean to run two threads concurrently Multiprogramming A B B C A C Thread A x 1 Thread B y 2 Thread A x 1 x y 1 Thread B y 2 y y 2 However What about Initially y 12 What are the possible values of x Or what are the possible values of x below C B Lec 6 10 Most of the time threads are working on separate data so scheduling doesn t matter A B C A CS162 UCB Fall 2009 Problem is at the lowest level Scheduler is free to run threads in any order and interleaving FIFO Random Dispatcher can choose to run each thread to completion or time slice in big chunks or small chunks Multiprocessing Make sure to be reading Nachos code First design document due next Thursday One week Set up regular meeting times with your group Let s get group interaction problems solved early Thread A Thread B x 1 x 2 X could be 1 or 2 non deterministic B Hyperthreading Possible to interleave threads on a per instruction basis Keep this in mind for our examples like multiprocessing 2 4 10 CS162 UCB Fall 2009 2 4 10 Lec 6 11 Page 3 CS162 UCB Fall 2009 Lec 6 12 Atomic Operations Correctness Requirements Threaded programs must work for all interleavings of thread instruction sequences To understand a concurrent program we need to know what the underlying indivisible operations are Atomic Operation an operation that always runs to completion or not at all Cooperating threads inherently non deterministic and non reproducible Really hard to debug unless carefully designed It is indivisible it cannot be stopped in the middle and state cannot be modified by someone else in the middle Fundamental building block if no atomic operations then have no way for threads to work together Example Therac 25 Machine for radiation therapy Software control of electron accelerator and electron beam Xray production Software control of dosage On most machines memory references and assignments i e loads and stores of words are atomic Software errors caused the death of several patients Many instructions are not atomic Double precision floating point store often not atomic VAX and IBM 360 had an instruction to copy a whole array 2 4 10 CS162 UCB Fall 2009 2 4 10 Lec 6 13 Space Shuttle Example One


View Full Document

Berkeley COMPSCI 162 - Lecture 6 Synchronization

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Loading Unlocking...
Login

Join to view Lecture 6 Synchronization 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 6 Synchronization 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?