Review ThreadFork Create a New Thread ThreadFork is a user level procedure that creates a new thread and places it on ready queue Arguments to ThreadFork CS162 Operating Systems and Systems Programming Lecture 6 Pointer to application routine fcnPtr Pointer to array of arguments fcnArgPtr Size of stack to allocate Synchronization Implementation September 19 2005 Prof John Kubiatowicz http inst eecs berkeley edu cs162 9 19 05 Lec 6 2 ThreadRoot is the root for the thread routine Other Thread ThreadRoot DoStartupHousekeeping UserModeSwitch enter user mode Call fcnPtr fcnArgPtr ThreadFinish ThreadRoot A B while Startup Housekeeping yield run new thread New Thread switch ThreadRoot stub Includes things like recording start time of thread Other Statistics Eventually run new thread will select this TCB and return into beginning of ThreadRoot Thread Code Stack will grow and shrink Running Stack with execution of thread Final return from thread returns into ThreadRoot which calls ThreadFinish ThreadFinish wake up sleeping threads This really starts the new thread Kubiatowicz CS162 UCB Fall 2005 ThreadRoot Stack growth Stack growth Kubiatowicz CS162 UCB Fall 2005 Review What does ThreadRoot look like Review How does Thread get started 9 19 05 Sanity Check arguments Enter Kernel mode and Sanity Check arguments again Allocate new Stack and TCB Initialize TCB and place on ready list Runnable Lec 6 3 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 4 Goals for Today Threaded Web Server More concurrency examples Need for synchronization Examples of valid synchronization Multithreaded version serverLoop connection AcceptCon ThreadFork ServiceWebPage connection Advantages of threaded version Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 5 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 What if too many requests come in at once 9 19 05 Thread Pools Kubiatowicz CS162 UCB Fall 2005 Lec 6 6 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 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 7 ATM server problem Service a set of requests Do so without corrupting database Don t hand out too much money 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 8 ATM bank server example Suppose we wanted to implement a server process to handle requests from an ATM network BankServer while TRUE ReceiveRequest op acctId amount ProcessRequest op accId amount ProcessRequest op accId amount if op deposit Deposit acctId amount else if Deposit acctId amount acct GetAccount actId may use disk I O acct balance amount StoreAccount acct Involves disk I O How could we speed this up 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 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 9 Can Threads Make This Easier Threads yield overlapped I O and computation without deconstructing code into non blocking fragments 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 Event Driven Version of ATM server Suppose we only had one CPU 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 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 used for graphical programming 9 19 05 Thread 2 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 add r1 amount1 store r1 acct balance 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 11 A B C A Multiprogramming load r1 acct balance add r1 amount2 store r1 acct balance Lec 6 10 Review Multiprocessing vs Multiprogramming What does it mean to run two threads concurrently Unfortunately shared state can get corrupted Thread 1 load r1 acct balance Kubiatowicz CS162 UCB Fall 2005 A B B C A C B C B Also recall Hyperthreading Possible to interleave threads on a per instruction basis Keep this in mind for our examples like multiprocessing 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 12 Problem is at the lowest level Atomic Operations Most of the time threads are working on separate data so scheduling doesn t matter 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 state cannot be modified by someone else in the middle Fundamental building block if no atomic operations than have no way for threads to work together What are the possible values of x 13 5 3 On most machines memory references and assignments i e loads and stores of words are atomic Many instructions are not atomic Or what are the possible values of x below Thread A Thread B x 1 x 2 X could be 1 or 2 non deterministic Could even be 3 for serial processors Double precision floating point store often not atomic VAX and IBM 360 had an instruction to copy a whole array Thread A writes 0001 B writes 0010 Scheduling order ABABABBA yields 3 9 19 05 Kubiatowicz CS162 UCB Fall 2005 Lec 6 13 Administrivia Sections in this class are mandatory Make sure that you go to the section that you have been assigned Some of the things presented in section will not show up in class Should be working on first project 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 It is indivisible it cannot be stopped in the middle and Make sure to be reading Nachos code First design document due next Monday One week Set up regular meeting times with your group Let s try to get group interaction
View Full Document
Unlocking...