Professional Documents
Culture Documents
Algorithm for DS
Physical Clocks
I) calculated based on position of earth
The interval between two consecutive transits of the sun is called the solar day.
Not so accurate as earth slows down due to tidal friction and atmospheric drag.
Physical Clocks
II) atomic clock - by counting transitions of the cesium 133 atom
International Atomic Time(TAI) is just the mean number of ticks of the cesium 133 clocks since midnight on Jan. 1, 1958 (the beginning
of time) divided by 9,192,631,770.
TAI seconds is now about 3 msec less than a mean solar day and would mean that over the course of the years, noon would get earlier
and earlier.
Physical Clocks
III) Universal Coordinated Time,(UTC)
introduced leap seconds whenever the discrepancy between TAI and solar time grows to 800 msec.
system based on constant TAI seconds but which stays in phase with the apparent motion of the sun.
Clock Synchronization Algorithms
Cristian's Algorithm
Cristian's Algorithm
2 problems
I) time must never run backward. If the sender's clock is fast, CUTC will be smaller than the sender's current value of C. Just taking over
CUTC could cause serious problems.
Such a change must be introduced gradually .
Cristian's Algorithm
II) it takes a nonzero amount of time for the time server's reply to get back to the sender.
attempt to measure it.
(T1–T0)/2. starting time-T0, the ending time-T1, are measured using the same clock,
can be improved if we know interrupt handling time I As T1-T0-I,
To improve the accuracy, Cristian suggested to make a series of measurements.
Any measurements in which T1–T0 exceeds some threshold value are discarded
Berkeleys Algorithm
Berkeleys Algorithm
time server (actually, a time daemon) is active, polling every machine periodically to ask what time it is there.
Based on the answers, it computes an average time and tells all the other machines to advance their clocks to the new time or slow their clocks down until some specified reduction has been achieved.
Averaging Algorithms (decentralized algo)
divides time into fixed-length resynchronization intervals.
The ith interval starts at T0+iR and runs until T0+(i+1)R, where T0 is an agreed upon moment in the past, and R is a system parameter.
At the beginning of each interval, every machine broadcasts the current time according to its clock.
Averaging Algorithms
it starts a local timer to collect all other broadcasts that arrive during some interval 5.
When all the broadcasts arrive, an algorithm is run to compute a new time from them by
i)averaging the values.
ii)discard the m highest and m lowest values,
and average the rest.
iii)try to correct each message by adding to it an
estimate of the propagation time from the
source.
Use of Synchronized Clocks
i) At-Most-Once Message Delivery
every message carries a connection identifier (chosen by the sender) and a timestamp.
For each connection, the server records in a table the most recent timestamp it has seen.
If any incoming message for a connection is lower than the timestamp stored for that connection, the message is rejected as a duplicate.
Use of Synchronized Clocks
i) At-Most-Once Message Delivery
Any timestamp older than G can safely be removed from the table.
Use of Synchronized Clocks
ii) Clock-Based Cache Consistency
caching introduces potential inconsistency if two
clients modify the same file at the same time.
If one or more of them has crashed, the server can just wait until the dead server's lease times out.
When a process has to read or update certain shared data structures,
ensure that no other process will use the shared data structures at the same time.
A Centralized Algorithm
One process is elected as the coordinator.
the coordinator only lets one process at a time into each critical region.
(a) Two processes want to enter the same critical region at the
same moment.
(b) Process 0 has the lowest timestamp, so it wins.
(c) When process 0 is done, it sends an OK also, so 2 can now
enter the critical region.
A Distributed Algorithm
Disadvantages
probability of one of the n processes failing is n times as large as a single coordinator failing.
Solution: When a request comes in, the receiver always sends a reply, either granting or denying permission.
all processes are involved in all decisions concerning entry into critical regions. If one process is unable to handle the load, it is unlikely that forcing everyone to do exactly the same
thing in parallel is going to help much.
algorithm is slower, more complicated, more expensive, and less robust that the original centralized one.
A Token Ring Algorithm.
a logical ring is constructed in which each process is assigned a position
in the ring.
A Token Ring Algorithm.
Advantages
Only one process has the token at any instant, so only one process can be in a critical region.
Disadvantages
a dead process will be detected when its neighbor tries to give it the token and fails which can be removed from the group, and the token holder can throw the token over the head of the dead process to the
next member down the line
A Comparison of the Three Algorithms
election algorithms attempt to locate the process with the highest process number and designate it as coordinator.
assumed that every process knows the process number of every other process.
The Bully Algorithm.
devised by Garcia-Molina.
When a process notices that the coordinator is no longer responding to requests, it initiates an election.
1. P sends an ELECTION message to all processes
with higher numbers.
2. If no one responds, P wins the election and
becomes coordinator.
3. If one of the higher-ups answers, it takes over.
P's job is done.
The Bully Algorithm.
A Ring Algorithm.
We assume that the processes are physically or logically ordered.
When any process notices that the coordinator is not functioning, it builds an ELECTION message containing its own process number and sends the message to its successor.
If the successor is down, the sender skips over the successor and goes to the next member along the ring, or the one after that, until a running process is located.
A Ring Algorithm.
At each step, the sender adds its own process number to the list in the message.
Eventually, the message gets back to the process that started it all and recognizes this event with its own process number.
COORDINATOR message is circulated to inform everyone else who the coordinator is (the list member with the highest number) and who the members of the new ring are.
A Ring Algorithm.
All or nothing policy
3) Stable storage
Each block on drive 2 is an exact copy of the
corresponding block on drive 1.
Stable Storage.
Problem
cost of copying everything to a private
workspace is prohibitive.
Private Workspace
optimizations
when a process reads a file but does not modify
it, there is no need for a private copy. It creates
a private workspace for it that is empty except
for a pointer back to its parent's workspace.
shadow blocks.
II) Writeahead Log (intentions list)
files are actually modified in place,
before any block is changed, a record is
written to the writeahead log on stable
storage telling which transaction is making
the change, which file and block is being
changed, and what the old and new values
are.
II) Writeahead Log (intentions list)
can be done using a single centralized lock manager, or with a local lock manager on each
machine for managing local files.
If the process refrains from updating any files until it reaches the shrinking
phase, failure to acquire some lock can be dealt with simply by releasing all
locks, waiting a little while, and starting all over.
keeps track of which files have been read and written. At the point of committing,
it checks all other transactions to see if any of its files have been changed since
the transaction started. If so, the transaction is aborted. If not, it is committed.
II) Optimistic Concurrency Control
Advantages
it is deadlock free and allows maximum parallelism because no process ever has to wait for a lock.
Disadvantages
If fails, the transaction has to be run all over again.
III) Timestamps
when a process tries to access a file, the file's read and write timestamps will be
lower (older) than the current transaction's timestamp.
II) Timestamps
If transactions are short and widely spaced in time, it will normally occur that when a process tries to
access a file, the file's read and write timestamps will be lower (older) than the current transaction's
timestamp.
This ordering means that the transactions are being processed in the proper order, so everything is all
right.
each machine maintains the resource graph for its own processes and resources,
a central coordinator maintains the resource graph for the entire system (the union of all the
individual graphs).
When the coordinator detects a cycle, it kills off one process to break the deadlock.
Centralized Deadlock Detection
Each machine maintains the graph for its own processes and resources.
1)whenever an arc is added or deleted from the resource graph, a message can be sent to the
coordinator.
2)periodically, every process can send a list of arcs added or deleted since the previous update.
3) the coordinator can ask for information when it needs it.
Centralized Deadlock Detection
Some of the processes may wait for local resources(1) and some may wait for resources on a different machine. (2)
Distributed Deadlock Detection
Chandy-Misra-Haas algorithm deadlock
Updated
message
(0, 0, 1).
the recipient checks to see if it itself is waiting for any processes. If so, the message is updated, keeping the
first field but replacing the second field by its own process number and the third one by the number of the
process it is waiting for.
Distributed Deadlock Detection
The message is then sent to the process on which it is blocked. If it is blocked on multiple processes,
all of them are sent (different) messages.
If a message goes all the way around and comes back to the original sender, that is, the process
listed in the first field, a cycle exists and the system is deadlocked.
Distributed Deadlock Detection
Deadlock Removal
I) process that initiated the probe should commit
suicide.
Deadlock Removal
II) have each process add its identity to the end of the probe message
so that when it returned to the initial sender, the complete cycle
would be listed. The sender can then see which process has the
highest number, and kill that one or send it a message asking it to kill
itself.
a young process that is killed off will eventually age until it is the oldest
one in the system, so this choice eliminates starvation.
Distributed Deadlock Prevention
Points of discussion
situations in which it is desirable to have multiple
threads of control sharing one address space but running
in parallel, as though they were separate processes.
Introduction to Threads
The scheduler will now be invoked and another thread will be started,
possibly the dispatcher, in order to acquire more work, or possibly another
worker that is now ready to run.
Thread Usage(single threaded file server)
The main loop of the file server gets a request, examines it, and carries
it out to completion before getting the next one.
While waiting for the disk, the server is idle and does not process any
other requests.
The main loop of the file server gets a request, examines it, and carries it out to
completion before getting the next one.
While waiting for the disk, the server is idle and does not process any other requests.
If the file server is running on a dedicated machine, as is commonly the case, the
CPU is simply idle while the file server is waiting for the disk.
the first thread generates some data and passes them on to the next thread for
processing. The data continue from thread to thread, with processing going on at
each step hence called pipeline.
Issues
1)Thread management.
static threads - choice of how many threads there will be is made when
the program is written or when it is compiled. Each thread is allocated a
fixed stack. This approach is simple, but inflexible.
2) Thread synchronization
i) Mutex.
2 states - unlocked or locked.
LOCK,- attempts to lock the mutex.
If the mutex is unlocked, the LOCK succeeds
If two threads try to lock the same mutex at exactly the same instant,, one
of them wins and the other loses.
A thread that attempts to lock a mutex that is already locked is blocked.
2) Thread synchronization
i) Condition Variable.
Each condition variable is normally associated with a mutex at the time it is
created.
The difference between mutexes and condition variables is that mutexes
are used for short-term locking, mostly for guarding the entry to critical
regions. Condition variables are used for long-term waiting until a resource
becomes available.
Design Issues for Threads Packages.
Issues
3) Conflicts between threads over the use of a global variable.
thread 1 executes the system call ACCESS to find out if it has permission
to access a certain file.
The operating system returns the answer in the global variable errno.
After control has returned to thread 1, but before it has a chance to read
errno, the scheduler decides that thread 1 has had enough CPU time for the
moment and decides to switch to thread 2.
Thread 2 executes an open call that fails, which causes errno to be
overwritten and thread 1's access code to be lost forever. When thread 1
starts up later, it will read the wrong value and behave incorrectly.
Design Issues for Threads Packages.
Solution
prohibit global variables altogether.
assign each thread its own private global variables, as shown in Fig. In
this way, each thread has its own private copy of errno and other global
variables, so conflicts are avoided.
Design Issues for Threads Packages.
Solution
new library procedures can be introduced to create, set, and read these
thread-wide global variables.
Threads packages often provide calls to give the user the ability to specify
the scheduling algorithm and set the priorities, if any.
Implementing a Threads Package.
If so, it stores the thread's registers (i.e., its own) in a table, looks for an
unblocked thread to run, and reloads the machine registers with the
new thread's saved values.
As soon as the stack pointer and program counter have been switched,
the new thread comes to life again automatically.
Implementing a Threads Package.
I) Implementing Threads in User Space.
Problems
One of the main goals of having threads in the first place
was to allow each one to use blocking calls, but to prevent
one blocked thread from affecting the others. With
blocking system calls, this goal cannot be achieved.
To manage all the threads, the kernel has one table per process with
one entry per thread. Each entry holds the thread's registers, state,
priority, and other information.
All calls that might block a thread, such as interthread synchronization
using semaphores, are implemented as system calls, at considerably
greater cost than a call to a runtime system procedure.
Kernel threads do not require any new, nonblocking system calls, nor
do they lead to deadlocks when spin locks are used.
Disadvantage
cost of a system call is substantial, so if thread operations (creation,
deletion, synchronization, etc.) are common, much more overhead will
be incurred.
Implementing a Threads Package.
Disadvantages of Implementing Threads in user space and Kernel.
The basic idea is when the kernel knows that a thread has blocked,
the kernel notifies the process' runtime system,
The runtime system, at its own discretion, can either restart the blocked
thread immediately, or put it on the ready list to be run later.
Solution
The kernel sees this and knows that the call is local. (If it had
been remote, the kernel would have treated the call in the
normal manner for remote calls.)
It then changes the client's memory map to put the client in the
server's address space and starts the client thread executing
the server's procedure.
The call is made in such a way that the arguments are already
in place, so no copying or marshaling is needed. RPC is
implemented.
Threads and RPC
Implicit receive- technique to speed up RPC
When a new message comes in to the server's machine, the kernel creates a
new thread on-the-fly to service the request.
it maps the message into the server's address space, and sets up the new
thread's stack to access the message.
Threads do not have to block waiting for new work. Thus no context has to
be saved.
hybrid model
The Workstation Model
1.Paging and temporary files. - disks are used only for paging and files
that are temporary, unshared, and can be discarded at the end of the login
session.
use them as explicit caches (in addition to using them for paging,
temporaries, and binaries).
In this mode of operation, users can download files from the file servers
to their own disks, read and write them locally, and then upload the
modified ones at the end of the login session.
each machine can have its own self-contained file system, with the
possibility of mounting or otherwise accessing other machines' file
systems.
The idea here is that each machine is basically self-contained and that
contact with the outside world is limited.
The Workstation Model
The Workstation Model
Advantages of Model
easy to understand.
Each user has a large degree of autonomy and can allocate his
workstation's resources as he sees fit.
Much of the time users are not using their workstations, which are idle,
while other users may need extra computing capacity and cannot get it.
From a system-wide perspective, allocating resources in such a way that
some users have resources they do not need while other users need
these resources badly is inefficient.
Using Idle Workstations
Using “rsh machine” command
rsh runs the specified command on the specified machine.
FLAWS.
the user must tell which machine to use, putting the full burden of
keeping track of idle machines on the user.
When the replies come back, remote picks one and sets it up.
the reply from the least heavily loaded machine will come back first
and be selected.
Using Idle Workstations
PROBLEMS
2. How can a remote process be run transparently?
it needs the same view of the file system, the same working directory, and
the same environment variables (shell variables),
when the first system call, say a READ, is executed. The kernel decision
depends very much on the system architecture. If the system is diskless,
with all the files located on file servers, the kernel can just send the
request to the appropriate file server, the same way the home machine
would have done had the process been running there. if the system has
local disks, each with a complete file system, the request has to be
forwarded back to the home machine for execution. Some system calls
must be forwarded back to the home machine no matter what, even if all
the machines are diskless.
In addition, all system calls that query the state of the machine have to be
done on the machine on which the process is actually running. These
include asking for the machine's name and network address, asking how
much free memory it has, and so on. All in all, making programs run on
remote machines as though they were running on their home machines is
possible, but it is a complex and tricky business.
Using Idle Workstations
PROBLEMS
3. What happens if the machine's owner comes back?
to kill off the intruding process. The simplest way is to do this abruptly
and without warning, but all work will be lost and the file system may be
left in a chaotic state. A better way is to give the process fair warning, by
sending it a signal to allow it to detect impending doom, and shut down
gracefully (write edit buffers to the disk, close files, and so on). If it has
not exited within a few seconds, it is then terminated.
Now consider what happens if we scoop up all the CPUs and place them in
a single processor pool. Instead of having n small queueing systems
running in parallel, we now have one large one, with an input rate n and a
service rate n. Let us call the mean response time of this combined system
T1. From the formula above we find
The Processor Pool Model
Advantages
There is also no danger of the owner coming back, because there are no
owners