Outline
Basic Ideas
Threads
Virtualization
Code Migration
Basic ideas(1)
To execute a program, an operating system creates a number of virtual processors, each one for running a different program.
To keep track of these virtual processors, the operating system has a process table, Containing entries to store CPU register Values, memory maps, open files, etc.
A process is often defined as a program in
execution, that is, a program that is currently
being executed on one of the operating system's
Basic ideas(2)
An important issue is that the operating system takes great care to ensure that independent processes cannot maliciously or inadvertently affect the correctness of each other's behavior .
In other words, the fact that multiple processes
may be concurrently sharing the same CPU and
other hardware resources is made transparent.
Basic ideas(3)
This concurrency transparency comes at a relatively high price.
Each time a process is created, the operating
system must create a complete independent
address space. Allocation can mean initializing
memory segments by, for example, zeroing a data
segment, copying the associated program into a
text segment, and setting up a stack for temporary
data.
Basic ideas(4)
switching the CPU between two processes may be relatively expensive as well.
Apart from saving the CPU context (which
consists of register values, program counter,
stack pointer, etc.), the operating system will also
have to modify registers of the memory
management unit (MMU) and invalidate address
translation caches such as in the translation
lookaside buffer (TLB).
Basic ideas(5)
if the operating system supports more processes than it can simultaneously hold in main memory, it may have to swap processes between main memory and disk before the actual switch can take place.
Like a process, a thread executes its own piece of code, independently from other threads.
However, in contrast to processes, no attempt is made to achieve a high degree of concurrency transparency if this would result in performance degradation.
Therefore, a thread system generally maintains only the
minimum information to allow a CPU to be shared by
several threads.
Basic ideas(6)
We build virtual processors in software, on top of physical processors:
Processor: Provides a set of instructions along with the capability of automatically executing a series of those instructions.
Thread: A minimal software processor in whose context a series of instructions can be executed. Saving a thread context implies stopping the current execution and saving all the data needed to continue the execution at a later stage.
Process: A software processor in whose context one or
more threads may be executed. Executing a thread, means
executing a series of instructions in the context of that thread.
Outline
Basic Ideas
Threads
Virtualization
Code Migration
• Observation 1: Threads share the same address space.
• Observation 2: Process switching is generally more expensive. OS is involved.
– e.g., trapping to the kernel.
• Observation 3: Creating and destroying threads is cheaper than doing it for a process.
Threads
Multithread Benefits(1/3)
The most important benefit comes from the fact that in a single-threaded process, whenever a blocking system call is executed, the process as a whole is blocked.
consider an application such as spreadsheet program, and assume that a user continuously and interactively wants to change values
Another advantage of multithreading is that it becomes possible to exploit parallelism when executing the program on a multiprocessor system
In that case, each thread is assigned to a different CPU while
shared data are stored in shared main memory.
Multithread Benefits(2/3)
Multithreading is also useful in the context of large
applications. Such applications are often developed as a collection of co-operating programs, each to be executed by a separate process.
Cooperation between programs is implemented by means of inter process communication (IPC)
mechanisms
The major drawback of all IPC mechanisms is that
communication often requires extensive context
switching, shown at three different points in next
slide Figure
Context Switching in Large Apps
A large app is commonly a set of cooperating processes,
communicating via IPC, which is expensive.
Multithread Benefits(3/3)
Different parts of an application can be implemented using isolated threads. These parts can communicate with each other via shared data space.
Context switch between thread can be done in user space
Low overhead in CS performance gain
A SW engineering reason: Many applications are easier to be designed and developed as a collection of cooperating threads
e.g., using separate threads for input validation, grammar check, paging, TOC generation and etc. in a word processing program.
Because threads are not automatically protected against each
other the way processes are, development of multithreaded
applications requires additional intellectual effort (i.e.,
additional care from the programmer).
Threads and OS (1/2)
Main Issue: Should an OS kernel provide threads or should they be implemented as part of a user-level package?
User-space solution:
Pros:
Low cost of thread creation and deletion (only needs to create and release a stack for thread)
Low cost of context switch between threads (only needs storing and retrieving CPU registers’ values, no need for TLB clearance, …)
Cons:
Everything done by a thread affects the whole process.
So what happens when a thread blocks on a syscall?
The whole process will be blocked!
Threads and OS (2/2)
Kernel solution: Kernel implements threads.
Everything is system call.
Pros:
Operations that block a thread are no longer a problem: kernel schedules another.
External events are simple: the kernel (which catches all events) schedules the thread associated with the event.
Cons:
Less efficient: All thread-related operations (e.g., creation, deletion, coordination, etc.) should be done by kernel Needs system calls CS between threads is as expensive as CS between processes!)
A solution: Try to mix user-level and kernel-level threads into a single concept:
Light Weight Processes (LWP)
Hybrid (Solaris)
Use two levels. Multiplex user threads on top of LWPs (kernel threads).
•Threads are created and managed by programmer
•Kernel is not aware of threads
•All thread-related operations (creation, deletion, coordination, Mutex, conditional variables, etc.) are performed in user-level
•Kernel dynamically assigns some LWPs to each process
•Programmer is not aware of LWPs and cannot manage them.
•Each thread is run in the context of a LWP
When a user-level thread does a blocking syscall:
Executions change from user-level to level (but in the context of the same LWP)
the LWP blocks (So, only the thread bounded to that LWP is blocked).
Kernel schedules another LWP.
Another thread is executed
This means a Context switch occurs (at user-level)
Hybrid (Solaris)
Hybrid (Solaris)
• Pros:
– Low cost of thread creation, deletion and coordination (all done in user-level)
– Blocking system calls do not block the whole process
– The programmer is not aware of the LWP
– LWPs can be run on simultaneously on different CPUs, transparently from the application.
• Cons:
– It needs to create and delete LWPs (kernel-level threads) which has high cost.
• But, LWP creating and deletion occoures rarely.
Threads in Distributed Systems
Server N threads
Input-output
Client
Thread 2 makes
T1 Thread 1
requests to server
generates results
Requests
Receipt &
queuing
Client and server with threads
Threads in Distributed Systems
Multithreaded clients(1/2)
• Main issue is hiding network latency.
• Multithreaded web client:
– Browser scans HTML, and finds more files that need to be fetched.
– Each file is fetched by a separate thread, each issuing an HTTP request.
– As files come in, the browser displays them.
• Multiple request-response calls to other machines (RPC):
– Client issues several calls, each one by a different thread.
– Waits till all return.
– If calls are to different servers, will have a linear speedup.
Fetching Images
Suppose there are ten images in a page. How should they be fetched?
Sequentially
fetch_sequential() {
for (int i = 0; i < 10; i++) { int sockfd = ...;
write(sockfd, "HTTP GET ...");
n = read_till_socket_closed(sockfd, jpeg[i], 100K);
} }
Concurrently
fetch_concurrent() { int thread_ids[10];
for (int i = 0; i < 10; i++) {
thread_ids[i] = start_read_thread(urls[i], jpeg[i]);
}
for (int i = 0; i < 10; i++) {
wait_for_thread(thread_ids[i]);
} }
Threads in Distributed Systems
Multithreaded clients(2/2)
Threads in Distributed Systems
Multithreaded Servers(1/2)
• Improve performance:
– Starting a thread to handle an incoming request is much cheaper than starting a process.
– Single-threaded server can’t take advantage of multiprocessor.
– Hide network latency. Other work can be done while a request is coming in.
• Better structure:
– Using simple blocking I/O calls is easier.
– Multithreaded programs tend to be simpler.
Threads in Distributed Systems
Multithreaded Servers(2/2)
A multithreaded server organized in a dispatcher/worker model.
• Single-Threaded Servers:
• The server can not respond to clients while is doing a blocking system call (e.g., waiting for the disk)
• A solution: Implementing the server as a Finite-State-Machine.
Threads in Distributed Systems
A Finite State Machine(1/2)
Suppose that threads are not available but the system designers find the performance loss due to single threading unacceptable.
A third possibility is to run the server as a big finite-state machine.
When a request comes in, the one and only thread examines it.
If it can be satisfied from the cache, fine, but if
not, a message must be sent to the disk.
However, instead of blocking, it records the state of the current request in a table and then goes and gets the next message.
The next message may either be a request for new work or a reply from the disk about a previous operation.
If it is new work, that work is started.
If it is a reply from the disk, the relevant information is fetched from the table and the reply processed and subsequently sent to the client.
In this scheme, the server will have to make use of non- blocking calls to send and receive.
Threads in Distributed Systems
A Finite State Machine(2/2)
Threads in Distributed Systems
• Three ways to construct a server:
Model Characteristics
Threads Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Finite-state machine Parallelism, nonblocking system calls
Outline
Basic Ideas
Threads
Virtualization
Code Migration
The Traditional Server Concept
Web Server Windows
IIS
App Server Linux Glassfish
DB Server Linux MySQL
Windows
Exchange
And if something goes wrong ...
Web Server Windows
IIS
App Server DOWN!
DB Server Linux MySQL
Windows
Exchange
The Traditional Server Concept
• System Administrators often talk about servers as a whole unit that includes the hardware, the OS, the storage, and the applications.
• Servers are often referred to by their function i.e. the Exchange server, the SQL server, the File server, etc.
• If the File server fills up, or the Exchange server
becomes overtaxed, then the System Administrators
must add in a new server.
The Traditional Server Concept
• Unless there are multiple servers, if a service experiences a hardware failure, then the service is down.
• System Admins can implement clusters of servers to make them more fault tolerant.
However, even clusters have limits on
their scalability, and not all applications
work in a clustered environment.
The Traditional Server Concept
• Pros
– Easy to conceptualize – Fairly easy to deploy – Easy to backup
– Virtually any
application/service can be run from this type of setup
• Cons
– Expensive to acquire and maintain hardware
– Not very scalable – Difficult to replicate
– Redundancy is difficult to implement
– Vulnerable to hardware outages
– In many cases, processor
is under-utilized
Virtual Machine Monitors (VMMs)
...
Virtual Machine Monitor (VMM) VMn VM0 VM1
Platform HW
I/O Devices Processor/CS
Memory
Appn App0
Guest OS0
App1
Guest OS1 Guest OSn
The Virtual Server Concept
• Virtual servers seek to encapsulate the server software away from the hardware
– This includes the OS, the applications, and the storage for that server.
• Servers end up as mere files stored on a physical box, or in enterprise storage.
• Virtual servers can still be referred to by
their function i.e. email server, database
server, etc.
The Virtual Server Concept
• Virtual servers can be scaled out easily.
– If the administrators find that the resources supporting a virtual server are being taxed too much, they can adjust the amount of resources allocated to that virtual server
• Server templates can be created in a virtual
environment to be used to create multiple, identical virtual servers
• Virtual servers themselves can be migrated from
host to host almost at will.
The Virtual Server Concept
• Pros
– Resource pooling – Highly redundant – Highly available
– Rapidly deploy new servers
– Easy to deploy
– Reconfigurable while services are running – Optimizes physical
resources by doing more with less
• Cons
– Slightly harder to conceptualize
– Slightly more costly (must
buy hardware, OS, Apps,
and now the abstraction
layer)
Virtualization
•Related Concepts:
•Isolation
•Consolidation
•Migration
Virtualization: Isolation
HW
App2 App1
OS
VMM HW
App1 App2
OS OS
Virtualization: Consolidation
HW1 HW2
App2 App1
OS1 OS2
VMM HW
App2 App1
OS1 OS2
Virtualization: Migration
VMM HW1
App
HW2 VMM
OS
VMM HW1
App
HW2 VMM
OS