DOC PREVIEW
Berkeley COMPSCI 162 - Lecture 6 Synchronization

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Page 1 CS162 Operating Systems and Systems Programming Lecture 6 Synchronization February 4, 2010 Ion Stoica http://inst.eecs.berkeley.edu/~cs162 Lec 6.2 2/4/10 CS162 ©UCB Fall 2009 Goals for Today • Concurrency examples • Need for synchronization • Examples of valid synchronization Note: Some slides and/or pictures in the following are adapted from slides ©2005 Silberschatz, Galvin, and Gagne Note: Some slides and/or pictures in the following are adapted from slides ©2005 Silberschatz, Galvin, and Gagne. Many slides generated from my lecture notes by Kubiatowicz. Lec 6.3 2/4/10 CS162 ©UCB Fall 2009 Review: Why allow cooperating threads? • 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 » What if ATMs were only updated at night? – Embedded systems (robot control: coordinate arm & hand) • Advantage 2: Speedup – Overlap I/O and computation » Many different file systems do read-ahead – Multiprocessors – chop up program into parallel pieces • Advantage 3: Modularity – More important than you might think – Chop large problem up into simpler pieces » To compile, for instance, gcc calls cpp | cc1 | cc2 | as | ld » Makes system easier to extend Lec 6.4 2/4/10 CS162 ©UCB Fall 2009 Review: Threaded Web Server • Multithreaded version: serverLoop() { connection = AcceptCon(); ThreadFork(ServiceWebPage(),connection); } • Advantages of threaded version: – 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?Page 2 Lec 6.5 2/4/10 CS162 ©UCB Fall 2009 Review: Thread Pools • 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 master() { allocThreads(slave,queue); while(TRUE) { con=AcceptCon(); Enqueue(queue,con); wakeUp(queue); } } slave(queue) { while(TRUE) { con=Dequeue(queue); if (con==null) sleepOn(queue); else ServiceWebPage(con); } } Master Thread Thread Pool queue Lec 6.6 2/4/10 CS162 ©UCB Fall 2009 ATM Bank Server • ATM server problem: – Service a set of requests – Do so without corrupting database – Don’t hand out too much money Lec 6.7 2/4/10 CS162 ©UCB Fall 2009 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, 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 */ } • 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) Lec 6.8 2/4/10 CS162 ©UCB Fall 2009 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 event-driven 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 is used for graphical programmingPage 3 Lec 6.9 2/4/10 CS162 ©UCB Fall 2009 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 */ } • Unfortunately, shared state can get corrupted: Thread 1 Thread 2 load r1, acct->balance load r1, acct->balance add r1, amount2 store r1, acct->balance add r1, amount1 store r1, acct->balance Lec 6.10 2/4/10 CS162 ©UCB Fall 2009 Administrivia • Should be working on first project – 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 • Design Document: – Information up on the Nachos page – Important inclusion: Testing methodology! » Give us a strategy for testing your code » We will be grading your methodology in the document • If you need to know more about synchronization primitives before I get to them, use book! – Chapter 6 (in 7th/8th edition) are all about synchronization Lec 6.11 2/4/10 CS162 ©UCB Fall 2009 Review: Multiprocessing vs Multiprogramming • What does it mean to run two threads “concurrently”? – 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 • Hyperthreading – Possible to interleave threads on a per-instruction basis – Keep this in mind for our examples (like multiprocessing) A B C B A A C B C B Multiprogramming A B C Multiprocessing Lec 6.12 2/4/10 CS162 ©UCB Fall 2009 Problem is at the lowest level • Most of the time, threads are working on separate data, so scheduling doesn’t matter: Thread A Thread B x = 1; y = 2; • However, What about (Initially, y = 12): Thread A Thread B x = 1; y = 2; x = y+1; y = y*2; – What are the possible values of x? • Or, what


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
Download Lecture 6 Synchronization
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?