Chapter Three
Chapter Three
Chapter 3
Introduction
Name in a distributed system is a string of
bits or characters that is used to refer to an
entity.
To operate on an entity, it is necessary to
access it, for which we need an access point.
An access point is special kind of entity in a
distributed system.
An entity can offer more than one access
point. (Phone owning).
An entity may change its access points in
the course of time. (moving to another
country)
Cont..
An address is a special kind of name: it
refers to an access point of an entity.
There are other types of names that are
used to uniquely identify an entity
referred to us Identifiers.
A true identifier is a name that has the
following properties
– 1. An identifier refers to at most one entity.
– 2. Each entity is referred to by at most one
identifier.
– 3. An identifier always refers to the same
entity.
Name Spaces
User-space solution
have nothing to do with the kernel, so all
operations can be completely handled
within a single process
so implementations can be extremely
efficient.
All services provided by the kernel are
done on behalf of the process in which a
thread resides
In practice we want to use threads when
there are lots of external events: threads
block on a per-event basis if the kernel
can’t distinguish threads.
Kernel solution
The whole idea is to have the kernel contain
the implementation of a thread package.
Operations that block a thread are no
longer a problem: the kernel schedules
another available thread within the same
process.
Handling external events is simple: the
kernel (which catches all events) schedules
the thread associated with the event.
The big problem is the loss of efficiency due
to the fact that each thread operation
requires a trap to the kernel.
Conclusion: mix user-level and kernel-level
Cont..
Lightweight processes (LWP) that can execute
user-level threads.
An LWP runs in the context of a single (heavy-weight)
process and a user level thread package (create,
destroy and synchronization thread )
Assign a thread to an LWP (hidden from the
programmer)
When an LWP is created (through a system call) gets
its own stack and execute a scheduling routine (that
searches for a thread to execute)
If many LWPs, each executes the scheduler, they share
a thread table (with the current set of threads),
synchronization among them in user space.
Cont.. (LWP)
When a thread calls a blocking user-level operation
(e.g., blocks on a mutex or a condition variable), it calls
the scheduling routine, when another runnable thread
is found, a context switch is made to that thread which
is then bound to the same LWP (the LWP that is
executing the thread need not be informed)
When a user-level thread does a blocking system call,
the LWP that is executing that thread blocks. The
thread remains bound to the LWP.
The kernel can simply schedule another LWP having a
runnable thread bound to it.
Note that this thread can switch to any other runnable
thread currently in user space.
When there are no threads to schedule, an LWP may
remain idle, and may even be removed (destroyed) by
the kernel.
Cont..
Multithreaded Servers
Why threads?
In a single-threaded system process
whenever a blocking system call is
executed, the process as a whole is
blocked
executing a program on a multiprocessor
system (assign each thread to a different
CPU)
Example
Each request takes on average 2msecs of
processing and 8 msecs of I/O delay (no
caching)
Maximum server throughput (measured as
client requests handled per sec)
Single thread
– Turnaround time for each request: 2 + 8 = 10
msecs
– Throughput: 100 req/sec
Two threads (if disk requests are serialized)
– Turnaround time for each request: 8 msecs
– Throughput: 125 req/sec
Cont..
A pool of “worker” threads to process the
requests.
One I/O (dispatcher) thread: receives
requests from a collections of ports and
places them on a shared request queue
for retrieval by the workers.
Cont..
Improve Performance
Starting a thread to handle an incoming
request is much cheaper than starting a
new process
Having a single-threaded server prohibits
simply scaling the server to a
multiprocessor system
Hide network latency by reacting to next
request while previous one is being
replied
Cont..
Better Structure
Most servers have high I/O demands.
Using simple, well-understood blocking
calls simplifies the overall structure.
Multithreaded programs tend to be
smaller and easier to understand due to
simplified flow of control.
Multithreaded Clients
Multithreaded Web client
Web browser scans an incoming HTML page,
and finds that more files need to be fetched
Each file is fetched by a separate thread, each
doing a (blocking) HTTP request
As files come in, the browser displays them
Multiple RPCs
A client does several RPCs at the same time,
each one by a different thread
It then waits until all results have been returned.
Note: if RPCs are to different servers, we may
have a linear speed-up compared to doing RPCs
one after the other
Thread Functions
Thread(ThreadGroup group, Runnable target,
String name)
• Creates a new thread in the SUSPENDED
setPriority(int newPriority), getPriority()
• Set and return the thread’s priority.
run(): A thread executes the run() method of its
target object
start(): Change the state of the thread from
SUSPENDED to RUNNABLE.
sleep(int millisecs): Cause the thread to enter
the SUSPENDED state for the specified time.
yield(): Enter the READY state and invoke the
scheduler.
destroy(): Destroy the thread.
Java thread lifetimes
New thread created in the same JVM as
its creator in the SUSPENDED state
start() makes it runnable
• It executes the run() method of an object
designated in its constructor
A thread ends its life when it returns
from run() or when its destroy() method
is called
Execute on top of the OS
Servers
Basic model: A server is a process that
waits for incoming service requests at a
specific transport address.
Iterative server: the server itself
handles the request.
Concurrent server: does not handle
the request itself, but passes it to a
separate thread or another process, and
then immediately waits for the next
request.
Endpoints
Clients sends requests to an endpoint (port)
at the machine where the server is running.
Thanks