Docsity

Building Concurrent Applications - Operating Systems | CMPSCI 377, Study notes of Operating Systems

Toggle sidebar
bg1
CMPSCI 377 Operating Systems Spring 2009
Lecture 22: April 30
Lecturer: Mark Corner Scribe: Bruno Silva
22.1 Building Concurrent Applications
In building concurrent systems, we want to overlap access to different resources (e.g. CPU, disk, network)
in order to hide latency and maximize parallelism and throughput. Furthermore, we want our systems to
be relatively simple to build and maintain. There are several standard threading models, or patterns, for
building concurrent applications, including thread pools, producer-consumer, bag-of-tasks, and work queues.
We discuss these below.
22.1.1 Basic Server Model
Suppose we want to program a Web server; we could easily do so without using concurrency, as follows:
while(true)
wait connection
read from socket and parse url
look up url contents in cache
if (!in cache)
fetch from disk
put in cache
send data to client
How do we make this code work for lots and lots of clients? Since there are a lot of slow operations being
performed (e.g. connecting to the networking, accessing the disk, etc), processing only one client at a time
clearly is not scalable. Our goal then is to build a concurrent application that both minimizes latency and
maximizes parallelism.
22.1.2 New Thread for each Client
We could improve the model above using our previous knowledge of programming with threads, and just
spawn a new thread for each new connection to the server. This would work, but with the drawback of
requiring lots of thread creation operations, which can be relatively expensive.
22.1.3 Thread Pools
Another solution then would be to keep a pool of threads. Whenever a new task arrives, the system would
simply get a thread from pool and set that thread to work on the given task. Whenever the pool is empty,
the system blocks until another thread completes its task.
22-1
pf3

Partial preview of the text

Download Building Concurrent Applications - Operating Systems | CMPSCI 377 and more Study notes Operating Systems in PDF only on Docsity!

CMPSCI 377 Operating Systems Spring 2009

Lecture 22: April 30

Lecturer: Mark Corner Scribe: Bruno Silva

22.1 Building Concurrent Applications

In building concurrent systems, we want to overlap access to different resources (e.g. CPU, disk, network) in order to hide latency and maximize parallelism and throughput. Furthermore, we want our systems to be relatively simple to build and maintain. There are several standard threading models, or patterns, for building concurrent applications, including thread pools, producer-consumer, bag-of-tasks, and work queues. We discuss these below.

22.1.1 Basic Server Model

Suppose we want to program a Web server; we could easily do so without using concurrency, as follows:

while(true) wait connection read from socket and parse url look up url contents in cache if (!in cache) fetch from disk put in cache send data to client

How do we make this code work for lots and lots of clients? Since there are a lot of slow operations being performed (e.g. connecting to the networking, accessing the disk, etc), processing only one client at a time clearly is not scalable. Our goal then is to build a concurrent application that both minimizes latency and maximizes parallelism.

22.1.2 New Thread for each Client

We could improve the model above using our previous knowledge of programming with threads, and just spawn a new thread for each new connection to the server. This would work, but with the drawback of requiring lots of thread creation operations, which can be relatively expensive.

22.1.3 Thread Pools

Another solution then would be to keep a pool of threads. Whenever a new task arrives, the system would simply get a thread from pool and set that thread to work on the given task. Whenever the pool is empty, the system blocks until another thread completes its task.

22-2 Lecture 22: April 30

One natural question now is: how many threads do we “pre-create” and add to the pool? Ideally, we would want to create an infinite (or large enough) amount of threads; however, this is clearly not possible, since each thread has a descriptor, a stack, etc, but our main memory is finite. Also, beyond a certain point, creating more threads does not help anymore, and the server starts to slow down again due to things like paging or lock contention. For example, if we only have 4 CPUs, it doesn’t help to try to run a billion things at the same time. In practice, the fine-tuning of how many thread to add to the pool for any given application is set empirically, or perhaps adaptively.

22.1.4 Producer-consumer

The general idea of a producer-consumer architecture is related to building a pipeline of threads, similar to the web spider from Project 2. Each step of the processing will now be done by a specialized threads; whenever one thread is done with its part of the processing, it forwards the data to the next stage of the pipeline, like a factory assembly line. In a producer-consumer system, we guarantee that each stage of the pipeline signals the next stage of the pipeline when it completes its task, and blocks when there’s nothing else for it to do, etc.

If we were to implement our web server in a producer-consumer fashion, we could simply create a pair of threads: one for reading the URL requested by the client, and another one for writing the answer back to the network. We could also create yet another intermediate thread, which would first look for the URL in a cache. This intermediate thread might help the overall average response time of the system, if we can implement the shortest time to completion first policy, i.e. finish the easy things first, which leads to the lowest average response time.

As with the thread pool approach, it is not always clear how many threads we need for each of stage of the pipeline; the exact amount depends on the application and is usually fine-tuned manually. Since each thread has a specialized task, if the number of threads for each task is not well-tuned, then many threads might be idle, wasting resources.

In general, the producer-consumer approach works well if the producer and the consumer are symmetric, i.e. if they proceed roughly at same rate. On the other hand, if the order of processing doesn’t matter, it doesn’t make sense to use a producer-consumer architecture, with its ordered pipeline of processing.

22.1.5 Bag of tasks

The producer-consumer model above has a standard human analogy of an assembly line. Humans have specialized skills, however, whereas threads running the same program do not need to be specialized: every thread has access to the same code and data. So any thread could do the work of any other thread.

In the bag of tasks approach, we have a collection of mostly independent “worker threads”; we no longer need to have different types of threads (one for the parser, one for the workers, etc). A bag of tasks system is very simple: every time a worker doesn’t have anything to do, it goes to the bag of tasks and gets something to work on; in our web server example, this bag of tasks could contain new connections to be processed.

The advantage of the bag of tasks approach is that we can imagine all workers as being homogeneously implemented, each one running a specific piece of code depending on the type of the task it has to process at that time (imagine each worker as being implemented with a big switch statement).

Let us now present a way to convert the pseudo-code for the web server, given in the first section, to a Bag of Tasks style. Assume we have an addWork thread and a bunch of worker threads.