Introduction to Computer Systems 15-213/18-243, spring 2009 Recitation, Jan. 14thSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Carnegie MellonIntroduction to Computer Systems15-213/18-243, spring 2009Lecture, Apr. 23rdInstructor: Nathan D. MickuliczCarnegie MellonLecture OutlineWhy do we need concurrency?How do we create concurrency?What are some hazards associated with concurrent programming?I/O Multiplexing and Thread PoolsCarnegie MellonLecture OutlineWhy do we need concurrency?How do we create concurrency?What are some hazards associated with concurrent programming?I/O Multiplexing and Thread PoolsCarnegie MellonIterative ServersConsider the client/server design below:Carnegie MellonIterative ServersLets look at the message flow in the common use case:Carnegie MellonIterative ServersDoes anyone see the problem yet?Carnegie MellonIterative ServersDoes anyone see the problem yet?Carnegie MellonIterative ServersOnly one request is served at once!Slow clients can slow down the server!Carnegie MellonIterative ServersConcurrency to the rescue!Carnegie MellonLecture OutlineWhy do we need concurrency?How do we create concurrency?What are some hazards associated with concurrent programming?I/O Multiplexing and Thread PoolsCarnegie MellonIn the Beginning......there was fork().Carnegie MellonConcurrency with fork()Clean, straightforward code.Carnegie MellonConcurrency with fork()Each connection is handled in its own process.Clients can execute concurrently!File descriptors are shared, memory is not.Programming is easy.Communication between child processes is NOT.Big overhead for process management!A moderately-loaded web server could easily generate 1000 concurrent processes!Carnegie MellonConcurrency with fork()Impractical for serious concurrent programming.Idea: multiple, concurrent threads of execution inside the same process.Communication between threads is as simple as writing to memory.Avoids copying memory and other process management.Carnegie MellonAdding Threads to ProcessesThe traditional view of a process is the process context and code/data/stack.Carnegie MellonAdding Threads to ProcessesSame objects, different organization.Carnegie MellonAdding Threads to ProcessesNow we can duplicate the thread while sharing the other data.Carnegie Mellon “Ok, it seems people are experimenting with [threads]. That's nice, I had been starting to give up on it completely (I was originally hoping for a threaded X server, but that never happened so now I have to hope that somebody else comes up with a "worthwhile" threaded project).”- Linus Torvalds May 3, 1996Carnegie MellonPOSIX Threads (pthreads)A standard library of functions which allow you to manage threads from C programs.Creating/reaping threadspthread_create()pthread_join()pthread_detach()Determining thread IDpthread_self()Terminating threadspthread_cancel()pthread_exit()Synchronization objectsCarnegie MellonPOSIX Threads “Hello, World!”Carnegie MellonPOSIX Threads “Hello, World!”How is this actually executed?!Carnegie MellonConcurrent Server with ThreadsThe beginnings of a simple Echo server.Spawn a new thread for each clientPass in the file descriptor?!Carnegie MellonConcurrent Server with ThreadsThe thread procedure for the Echo server.Detaches from parent – cleanup done on exit.Echoes the input back to the client.Closes the file descriptor.!!!Why?Carnegie MellonConcurrent Server with ThreadsSo, what was up with that weird malloc/free thing?Isn't that a waste of time to run the allocator?Just pass in the file descriptor as an argument!But the argument is a pointer...and we wouldn't want to cast integers to pointers!Okay, then just pass in a reference to the stack variable!Hmm...Carnegie MellonLecture OutlineWhy do we need concurrency?How do we create concurrency?What are some hazards associated with concurrent programming?I/O Multiplexing and Thread PoolsCarnegie MellonRace ConditionsHere's a small threaded program...Carnegie MellonRace Conditions...with a really big problem.Carnegie MellonRace ConditionsLet's dissect the code.Carnegie MellonRace ConditionsMany different orders of execution, or interleaving of threads, are possible...OK!Carnegie MellonRace Conditions...but not all are correct!BAD!Carnegie MellonRace ConditionsHow to fix these problems?It's not easy!#1 source of concurrent programming headaches.The main technique is to synchronize the threads.Don't allow the threads to run concurrently while modifying shared state!This is a topic for next week.Lets go back to a more familiar context...Carnegie MellonStack-Sharing Hazards“Just pass a reference to the stack variable!”Carnegie MellonPros and Cons of ThreadingProsEasy to share data between threadsLogging information, file cache, ...Threads much more efficient than processesConsMuch harder to write programs correctly!!!Unintentional data sharing can introduce subtle and hard-to-reproduce errors!There are ways to deal with this – covered in the next lecture.Carnegie MellonSummary of ConcurrencyProcessesHard to share resources, easy to avoid unintended sharing.High overhead in adding/removing clients.ThreadsEasy to share resources – sometimes too easy!Medium overhead.Not much control over when threads are run.Difficult to debug because thread interleaving is not repeatable!I/O MultiplexingNext lecture.Carnegie
View Full Document