Professional Documents
Culture Documents
Slides 03
Slides 03
Introduction to threads
Basic idea
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.
Introduction to threads
Processes Threads
Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of
instructions (e.g., stack pointer, addressing registers, program counter).
Introduction to threads
Processes Threads
Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of
instructions (e.g., stack pointer, addressing registers, program counter).
• Thread context: The minimal collection of values stored in registers and
memory, used for the execution of a series of instructions (i.e.,
processor context, state).
Introduction to threads
Processes Threads
Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of
instructions (e.g., stack pointer, addressing registers, program counter).
• Thread context: The minimal collection of values stored in registers and
memory, used for the execution of a series of instructions (i.e.,
processor context, state).
• Process context: The minimal collection of values stored in registers
and memory, used for the execution of a thread (i.e., thread context, but
now also at least MMU register values).
Introduction to threads
Processes Threads
Context switching
Observations
1. Threads share the same address space. Thread context switching can
be done entirely independent of the operating system.
2. Process switching is generally (somewhat) more expensive as it
involves getting the OS in the loop, i.e., trapping to the kernel.
3. Creating and destroying threads is much cheaper than doing so
for processes.
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Trade-offs
• Threads use the same address space: more prone to errors
• No support from OS/HW to protect threads using each other’s memory
• Thread context switching may be faster than process context switching
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
User-space solution
• All operations can be completely handled within a single process
⇒
implementations can be extremely efficient.
• All services provided by the kernel are done on behalf of the process
in which a thread resides ⇒ if the kernel decides to block a thread, the
entire process will be blocked.
• Threads are used when there are many external events: threads block
on a per-event basis ⇒ if the kernel can’t distinguish threads, how can
it support signaling events to them?
Introduction to threads
Processes Threads
Conclusion – but
Try to mix user-level and kernel-level threads into a single concept,
however, performance gain has not turned out to generally outweigh the
increased complexity.
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Practical measurements
A typical Web browser has a TLP value between 1.5 and 2.5 ⇒ threads
are primarily used for logically organizing browsers.
Better structure
• Most servers have high I/O demands. Using simple, well-
understood blocking calls simplifies the structure.
• Multithreaded programs tend to be smaller and easier to understand
due to simplified flow of control.
Overview
Model Characteristics
Multithreading Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Finite-state machine Parallelism, nonblocking system calls
Virtualization
Observation
Virtualization is important:
• Hardware changes faster than software
• Ease of portability and code migration
• Isolation of failing or attacked components
Principle of virtualization
Processes Virtualization
Mimicking interfaces
Four types of interfaces at three different levels
1. Instruction set architecture: the set of machine instructions, with
two subsets:
• Privileged instructions: allowed to be executed only by the
operating system.
• General instructions: can be executed by any program.
2. System calls as offered by an operating system.
3. Library calls, known as an application programming interface (API)
Principle of virtualization
Processes Virtualization
Ways of virtualization
Differences
(a) Separate set of instructions, an interpreter/emulator, running atop an
OS.
(b) Low-level instructions, along with bare-bones minimal operating system
(c) Low-level instructions, but delegating most work to a full-fledged OS.
Principle of virtualization
Processes Virtualization
Special instructions
• Control-sensitive instruction: may affect configuration of a machine (e.g.,
one affecting relocation register or interrupt table).
• Behavior-sensitive instruction: effect is partially determined by context
(e.g., POPF sets an interrupt-enabled flag, but only in system mode).
Principle of virtualization
Processes Virtualization
Solutions
• Emulate all instructions
• Wrap nonprivileged sensitive instructions to divert control to VMM
• Paravirtualization: modify guest OS, either by preventing
nonprivileged sensitive instructions, or making them nonsensitive (i.e.,
changing the context).
Principle of virtualization
Processes Virtualization
Containers
Example: PlanetLab
Essence
Different organizations contribute machines, which they subsequently share
for various experiments.
Problem
We need to ensure that different distributed applications do not get into
each other’s way ⇒ virtualization
Containers
Processes Virtualization
Vserver
Independent and protected environment with its own libraries, server
versions, and so on. Distributed applications are assigned a collection of
vservers distributed across multiple machines
Containers
Processes Virtualization
Containers
Processes Virtualization
IaaS
Instead of renting out a physical machine, a cloud provider will rent out a VM
(or VMM) that may be sharing a physical machine with other customers ⇒
almost complete isolation between customers (although performance isolation
may not be reached).
Client-server interaction
Distinguish application-level and middleware-level solutions
Improving X
Practical observations
• There is often no clear separation between application logic
and user-interface commands
• Applications tend to operate in a tightly synchronous manner with an
X kernel
Alternative approaches
• Let applications control the display completely, up to the pixel level
(e.g., VNC)
• Provide only a few high-level display operations (dependent on
local video drivers), allowing more efficient display operations.
Client-side software
Generally tailored for distribution transparency
• Access transparency: client-side stubs for RPCs
• Location/migration transparency: let client-side software keep track
of actual location
• Replication transparency: multiple invocations handled by client stub:
Observation
Concurrent servers are the norm: they can easily handle multiple
requests, notably in the presence of blocking operations (to disks or other
servers).
General design issues
Processes Servers
Contacting a server
Observation: most services are tied to a specific port
ftp-data 20 File Transfer [Default Data]
ftp 21 File Transfer [Control]
telnet 23 Telnet
smtp 25 Simple Mail Transfer
www 80 Web (HTTP)
Out-of-band communication
Issue
Is it possible to interrupt a server once it has accepted (or is in the process
of accepting) a service request?
Out-of-band communication
Issue
Is it possible to interrupt a server once it has accepted (or is in the process
of accepting) a service request?
Out-of-band communication
Issue
Is it possible to interrupt a server once it has accepted (or is in the process
of accepting) a service request?
Consequences
• Clients and servers are completely independent
• State inconsistencies due to client or server crashes are reduced
• Possible loss of performance because, e.g., a server cannot
anticipate client behavior (think of prefetching file blocks)
Consequences
• Clients and servers are completely independent
• State inconsistencies due to client or server crashes are reduced
• Possible loss of performance because, e.g., a server cannot
anticipate client behavior (think of prefetching file blocks)
Question
Does connection-oriented communication fit into a stateless design?
Observation
The performance of stateful servers can be extremely high, provided clients
are allowed to keep local copies. As it turns out, reliability is often not a
major problem.
Object servers
• Activation policy: which actions to take
when an invocation request comes in:
• Where are code and data of
the object?
• Which threading model to use?
• Keep modified state of object, if
any?
• Object adapter: implements a specific
activation policy
Object servers
Processes Servers
1 import s y s , Ic e
2 import Demo
3
4 c l a s s PrinterI(Demo.Printer):
5 def i n i t ( s e l f , t ) :
6 self.t = t
7
8 def p r i n t S t r i n g ( s e l f , s , current=None):
9 print(self.t, s)
10
11 communicator = Ic e . i n it i a l iz e (s y s . a rg v )
12
13 adapter = communicator.createObjectAdapterWithEndpoints("SimpleAdapter", " de fa u lt - p 11000")
14 object1 = PrinterI("Object1 s a ys :" )
15 object2 = PrinterI("Object2 s a ys :" )
16 adapter.add(object1, communicator.stringToIdentity("SimplePrinter1"))
17 adapter.add(object2, communicator.stringToIdentity("SimplePrinter2"))
18 adapter. activate()
19
20 communicator.waitForShutdown()
Object servers
Processes Servers
1 import s y s , Ic e
2 import Demo
3
4 communicator = Ic e . i n it i a l iz e (s y s . a rg v )
5
6 base1 = communicator.stringToProxy("SimplePrinter1:default - p 11000")
7 base2 = communicator.stringToProxy("SimplePrinter2:default - p 11000")
8 p r i n t e r 1 = Demo.PrinterPrx.checkedCast(base1)
9 p r i n t e r 2 = Demo.PrinterPrx.checkedCast(base2)
10 i f (not p r i n t e r 1 ) or (not p r i n t e r 2 ) :
11 r a i s e RuntimeError("Invalid proxy")
12
13 printe r1. printString( " He llo World from printe r1!" )
14 printe r2. printString( " He llo World from printe r2!" )
15
16 communicator.waitForShutdown()
Object servers
Processes Servers
Crucial element
The first tier is generally responsible for passing requests to an
appropriate server: request dispatching
Server clusters
Processes Servers
Request Handling
Observation
Having the first tier handle all communication from/to the cluster may lead to
a bottleneck.
Server clusters
Processes Servers
Client transparency
To keep client unaware of distribution, let DNS resolver act on behalf of
client. Problem is that the resolver may actually be far from local to the
actual client.
Server clusters
Processes Servers
Server clusters
Processes Servers
Important note
The cache is often sophisticated enough to hold more than just passive
data. Much of the application code of the origin server can be moved to the
cache as well.
Server clusters
Processes Code migration
Weak mobility: Move only code and data segment (and reboot
execution)
• Relatively simple, especially if code is portable
• Distinguish code shipping (push) from code fetching (pull)
Observation
As containers are directly dependent on the underlying operating system,
their migration in heterogeneous environments is far from trivial, to simply
impractical, just as process migration is.