Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 Lecture Apr 23rd Instructor Nathan D Mickulicz Carnegie Mellon Lecture Outline Why do we need concurrency How do we create concurrency What are some hazards associated with concurrent programming I O Multiplexing and Thread Pools Carnegie Mellon Lecture Outline Why do we need concurrency How do we create concurrency What are some hazards associated with concurrent programming I O Multiplexing and Thread Pools Carnegie Mellon Iterative Servers Consider the client server design below Carnegie Mellon Iterative Servers Lets look at the message flow in the common use case Carnegie Mellon Iterative Servers Does anyone see the problem yet Carnegie Mellon Iterative Servers Does anyone see the problem yet Carnegie Mellon Iterative Servers Only one request is served at once Slow clients can slow down the server Carnegie Mellon Iterative Servers Concurrency to the rescue Carnegie Mellon Lecture Outline Why do we need concurrency How do we create concurrency What are some hazards associated with concurrent programming I O Multiplexing and Thread Pools Carnegie Mellon In the Beginning there was fork Carnegie Mellon Concurrency with fork Clean straightforward code Carnegie Mellon Concurrency 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 Mellon Concurrency 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 Mellon Adding Threads to Processes The traditional view of a process is the process context and code data stack Carnegie Mellon Adding Threads to Processes Same objects different organization Carnegie Mellon Adding Threads to Processes Now 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 1996 Carnegie Mellon POSIX Threads pthreads A standard library of functions which allow you to manage threads from C programs Creating reaping threads pthread create pthread join pthread detach Determining thread ID pthread self Terminating threads pthread cancel pthread exit Synchronization objects Carnegie Mellon POSIX Threads Hello World Carnegie Mellon POSIX Threads Hello World How is this actually executed Carnegie Mellon Concurrent Server with Threads The beginnings of a simple Echo server Spawn a new thread for each client Pass in the file descriptor Carnegie Mellon Concurrent Server with Threads The thread procedure for the Echo server Why Detaches from parent cleanup done on exit Echoes the input back to the client Closes the file descriptor Carnegie Mellon Concurrent Server with Threads So 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 Mellon Lecture Outline Why do we need concurrency How do we create concurrency What are some hazards associated with concurrent programming I O Multiplexing and Thread Pools Carnegie Mellon Race Conditions Here s a small threaded program Carnegie Mellon Race Conditions with a really big problem Carnegie Mellon Race Conditions Let s dissect the code Carnegie Mellon Race Conditions Many different orders of execution or interleaving of threads are possible OK Carnegie Mellon Race Conditions but not all are correct BAD Carnegie Mellon Race Conditions How 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 Mellon Stack Sharing Hazards Just pass a reference to the stack variable Carnegie Mellon Pros and Cons of Threading Pros Easy to share data between threads Logging information file cache Threads much more efficient than processes Cons Much 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 Mellon Summary of Concurrency Processes Hard to share resources easy to avoid unintended sharing High overhead in adding removing clients Threads Easy 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 Multiplexing Next lecture Carnegie Mellon Questions
View Full Document