0% found this document useful (0 votes)
25 views

Ds Unit 2

A thread is a lightweight process that shares resources with other threads. It can run on a single processor using time-division multiplexing or simultaneously on multiple processors. Distributed systems use threads to improve performance by handling requests concurrently and hiding network latency. There are three types of threads: user-level, kernel-level, and hybrid threads. Clock synchronization is important for distributed systems to have a consistent notion of time across processes.

Uploaded by

ravijat53080
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Ds Unit 2

A thread is a lightweight process that shares resources with other threads. It can run on a single processor using time-division multiplexing or simultaneously on multiple processors. Distributed systems use threads to improve performance by handling requests concurrently and hiding network latency. There are three types of threads: user-level, kernel-level, and hybrid threads. Clock synchronization is important for distributed systems to have a consistent notion of time across processes.

Uploaded by

ravijat53080
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT -2

Introduction of threads
 A thread is a light weight process

 A thread is part of the process ( a part of the program in execution )

 A thread could use with single processor or with multi-processor architecture.

 In single CPU multi-threading generally occurs with time division multiplexing

 In multi-processor every processor executing a separate thread simultaneously.

 A memory maintain thread table for execution.

Prof. Sanjay Sharma, IPS Academy #1


9893176328, 9977265269
Threads in distributed systems – clients
 Support web browsing :
o Web browser scans an incoming HTML page, and finds that more files need to
be fetched
o Each file is fetched by a separate thread, each doing a (blocking) HTTP request
o As files come in, the browser displays them
 Multiple request-response calls to other machines:
o A client does several RPC calls at the same time, each one by a different thread
o It then waits until all results have been returned
o Note: if calls are to different servers, we may have a linear speed-up
compared to doing calls one after the other

Threads in distributed systems – servers


 In servers, the main issue is improved performance and better structure
 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
– As with clients: hide network latency by reacting to next request while
previous one is being replied
 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

Types of Thread
1. User Level Thread

2. Kernel Level Thread


3. Hybrid Level Thread

Prof. Sanjay Sharma, IPS Academy #2


9893176328, 9977265269
User Level Thread Kernel Level Thread

Process vs. Thread

User Level vs. Kernel Level Threads


Prof. Sanjay Sharma, IPS Academy #3


9893176328, 9977265269
Thread library in user space In Kernel space

Thread management done by “user level By Kernel


thread” library

The Kernel not aware of the existence of The Kernel maintain the process and thread
Thread

Thread switching not require Kernel mode Switching between threads requires the Kernel
privileges privileges

User-level, Kernel-level and Combined

Combined Threads

Prof. Sanjay Sharma, IPS Academy #4


9893176328, 9977265269
Code/Process Migration
• Code/process migration in which an entire process was moving from one machine to
another.

• Change of location and execution of a process from current processor to the


destination processor for better utilization of resources and balancing load in
distributed system.

• Overall system performance can be improved if processes are moved from heavily
loaded to lightly loaded machines.

• Moving program or code must follow distributed system environment.

Code migration dynamically configuring a client to communicate to a server. The client first
fetches the necessary software then invokes the server. A process have three segments.

1. Code segment- set of instruction


2. External segment – input/output required by the process

3. Execution segment- used to store the current execution state of a process

Prof. Sanjay Sharma, IPS Academy #5


9893176328, 9977265269
Different Models/Modes of Migration

 Client to Server Migration


 Sever to Client Migration
 Weak mobility and strong mobility migration
 Sender / receiver initiated migration
Advantages of Migration
1. Better CPU utilization

2. Load distribution
3. Save valuable server time

4. Improve performance of

a. Network
b. Speed

c. Channels
d. Traffic

5. Improve client side performance

6. Partition the application into different parts


Disadvantages of Migration

1. Provide special security


2. Design special application and protocols for process migration.

Application

 Access server database

 Before submission, processing the form at client side

 Searching information on the web

 Remote access of file system

Prof. Sanjay Sharma, IPS Academy #6


9893176328, 9977265269
Clock synchronization

Clock synchronization is a problem from computer science and engineering which deals with
the idea that internal clocks of several computers may differ. Even when initially set accurately,
real clocks will differ after some amount of time due to clock drift, caused by clocks counting
time at slightly different rates. There are several problems that occur as a repercussion of clock
rate differences and several solutions, some being more appropriate than others in certain
contexts.

Computer Clock
 All computers have a circuit for keeping track of times.
 A computer time is a machined quartz crystal, usually a quartz crystal, oscillating at a
well-defined frequency. A timer is associated with two registers: a counter and a
holding register, and counter decreasing one at each oscillation.
 When the counter gets to zero, an interruption is generating and is count as one clock
tick.
 ROM BIOS and time information is managed by CMOS battery
 Crystals run at slightly different rates, the difference in time value known as a clock
skew.
 Clock skew causes time-related failures.

How Clocks Work in Computer

Prof. Sanjay Sharma, IPS Academy #7


9893176328, 9977265269
Why clocks need to be synchronised
 Many applications rely on correct and accurate timing
o Real time applications, e.g. calculation of interests,
o Version control and configuration management, e.g. the make command in
Unix
 Correct and accurate clocks can simplify communication and concurrent control
 In centralized systems, where processes can share a clock and memory,
implementation of synchronization primitives relies on shared memory and the times
that events happened.
 In distributed system, processes can run on different machines.
o No shared memory physically exists in a multi-computer system
o No global clock to judge which event happens first
o Logical simulation of shared memory is not a good solution
Clock Skew problem
When different computers have different clock value

Clock Drifting

The relation between clock time and UTC (Universal Coordinated Time) when clocks
tick at different rates

Prof. Sanjay Sharma, IPS Academy #8


9893176328, 9977265269
Synchronization with Physical Clocks
 How do we synchronize physical clocks with real-word clock?
o TAI (International Atomic Time): Cs133 atomic clock
o UTC (Universal Coordinated Time): modern civil time, can be received from
WWV (shortwave radio station), satellite, or network time server.
o ITS (Internet Time Service) NTS (Network Time Protocol)

 How do we synchronize clocks with each other?


o Centralized Algorithm: Cristian’s Algorithm- synchronize with a time server

o Distributed algorithm: Berkeley Algorithm – without time server

Cristian’s algorithm (1)


Assumptions: There is a machine with WWV receiver, which receives precise UTC (Universal
Coordinated Time). It is called the time server.

Algorithm:
1. A machine sends a request to the time server at least every d/2r seconds, where d is
the maximum difference allowed between a clock and the UTC;
2. The time server sends a reply message with the current UTC when receives the
request;
3. The machine measures the time delay between time server’s sending the message
and the machine’s receiving the message. Then, it uses the measure to adjust the
clock.

Cristian’s algorithm (2) - Getting the current time from a time server

Prof. Sanjay Sharma, IPS Academy #9


9893176328, 9977265269
The Berkeley Algorithm – synchronize clock without time server
a) The time server/daemon asks all the other machines for their clock values

b) The machines answer

c) Based on the answer it computes an average time and tells all the other machines to
change their clocks.

Prof. Sanjay Sharma, IPS Academy #10


9893176328, 9977265269
Distributed System and Mutual Exclusion

 A condition in which there is a set of processes and only one of them is able to access
a given resources or perform a given function at a time.
 Protects from others to interfere and waiting for to free
 The major differences comparing with the single processor ME problem are:
1. there is no shared memory and
2. there is no common physical clock
 Two kinds of algorithms:
1. Logical clock based
2. Token based.

Requirements of Distributed Mutual Exclusion

 Mutual Exclusion: guarantee that only one request access the Critical Region at a time.
 Freedom from deadlock: two or more sites (hosts) should not endlessly wait for msg’s
that will never arrive.
 Freedom from starvation: a site should not be forced to wait indefinitely to access CR.
 Fairness: requests must be executed in the order they are made. (fairness  freedom of
starvation, but not reverse)
 Fault tolerance: in the case of failure, the algorithm can reorganize itself so that it
continues to function without any disruption.

Distributed ME and Synchronization Scheme

1. Centralized Algorithm
2. Distributed Algorithm
3. Token Ring Algorithm

Centralized Algorithm

 Based on two operation [ request and grant ]


 One process elected as a coordinator
 When a process wants to enter a critical region it sends a request message to the
coordinator, asking for the permission.
 If resource is free or no other process in Critical Region, the coordinator sends back a
reply for granting permission.
 Requesting process now in Critical Region
 In a large system, a single coordinator can become a bottleneck performance

Prof. Sanjay Sharma, IPS Academy #11


9893176328, 9977265269
Distributed Algorithm (Ricart-Agrawala Algorithm )

 When a process wants to enter a critical region it builds a message and sends the
message to all other processes.
 When a process receives a request message from another process, three situation
may be possible:
1. If the receiver not in the critical region and does not want to enter it, it sends
back an “OK” message to the sender.
2. If the receiver already in critical region, it does not reply, instead, it already in
the queues for requesting the process.
3. If the receiver also in critical region then receiver time-stamp is compare with
sender as well as all the process under critical region, the lowest time-stamp
allow permission for mutual exclusion.

Prof. Sanjay Sharma, IPS Academy #12


9893176328, 9977265269
Token Ring Algorithm

 All processes are arranged in a logical ring by special order number


 A Token is circulate around the ring in a single direction
 If the next process want to access Critical Region then process granted permission for
using the required resource and Token now move until he finished the job, after
completion now Token moved for next process.
 If any process not want Critical Region then just pass to the next
 The Token continuously circulates in a single direction in the logical ring.

Example:

 Suppose a group of processes on a network


 P1, P2, P3, P4, P5, P6, P7
 All connected in a logical ring
 A single resource R1 is only available for all the processes

CASE 1-

In Distributed System, suppose P2 and P5 is in Critical Region. Token are started from P1
using clock-wise direction when he reached at process P2, P2 already demanded for
resource R1 so now P2 is in Critical Region and using R1. After completion P2 free to R1 and
now “Token” again move to next process P3.

Prof. Sanjay Sharma, IPS Academy #13


9893176328, 9977265269
CASE 2

After processing of P2, Token passed to P3 but P3 not in Critical Region then P3 by pass to P4
again P4 not in Critical Region, Token passed to P5.

CASE 3

Now P5 already in Critical Region so Token assigned to process P5 for executing its job using
resources R1

Prof. Sanjay Sharma, IPS Academy #14


9893176328, 9977265269
Process Synchronization using Bully and Ring Algo – Election Algorithm
Election Algorithms:
 Distributed system is a collection of independent computers that do not share their
memory. Each processor has its own memory and they communicate via
communication networks.

 Communication in networks is implemented in a process on one machine


communicating with a process on other machine. Many algorithms used in
distributed system require a coordinator that performs functions needed by other
processes in the system.

 Election algorithms are designed to choose a coordinator. Election algorithms choose


a process from group of processors to act as a coordinator. If the coordinator process
crashes due to some reasons, then a new coordinator is elected on other processor.

 Election algorithm basically determines where a new copy of coordinator should be


restarted. Election algorithm assumes that every active process in the system has a
unique priority number. The process with highest priority will be chosen as a new
coordinator. Hence, when a coordinator fails, this algorithm elects that active process
which has highest priority number. Then this number is send to every active process
in the distributed system.

 An election algorithm determines which process will play the role of coordinator or
server. All processes need to agree on the selected process. Any process can start an
election,
 We have two election algorithms for two different configurations of distributed
system.
1. The Bully Algorithm

2. The Ring Algorithm

Bully Algorithm
Bully Algorithm starts its process of selecting of coordinator when a working process
notices that the coordinator is not responding to it and then initiates an election. The
selection of a coordinator sends an election message to all the other available processes. If
none of the process responds to its message, the process itself wins the election and
becomes the coordinator.
Meanwhile, if any process that has a higher ID than the initiating process answers, it takes
charge of initiating the process. In this algorithm, the bully guy that is the participating

Prof. Sanjay Sharma, IPS Academy #15


9893176328, 9977265269
process that has the highest ID becomes the coordinator. This approach is efficient in
terms of fault tolerance but complex to implement.

Ring Algorithm
 In distributed system where many processes are working, for to avoid deadlock
condition and proper process synchronization, algorithm elected a coordinator for
monitoring on other processes
 Ring Algorithm starts its process of selecting of coordinator, any process connected
in the ring notices that the coordinator is not functioning. The initiating process
then prepares an election message that contains its own process number and then
sends this message to its successor in the ring. If the successor is not functioning
then it skills that successor and sends it to the next one.
 Every next process then adds its own process number to the message and delivers
the message to the next process connected in the ring. It continues until the
message again reaches the initiating process. Now the message consists of the
process number of all the functioning processes. The process that has the highest ID
wins the election. The initiating process then sends another message in the ring
about the winning process.

Prof. Sanjay Sharma, IPS Academy #16


9893176328, 9977265269
Working Process / steps
 As per the above diagram suppose total eight processes (zero to 7 ) are working in
distributed system and at primary stage no one elected as coordinator.

 To choose coordinator, process (4) sends an ELECTION message to its successor with
its ID.

 Successor (5) again attach its ID and forward to process no (6)

 Process (6) is active and after attaching its ID move to nest Process (7), but process (7)
is not active so forward for Process (0)

 Process (0) after attaching its ID forward for next process, so one by one all process
registered their identification.

 The final information is again move to starting process – (4) , now process (4) have all
the information of processes, so select higher ID process as coordinator and inform to
all the process for selection of coordinator. As per the above diagram process (6) is
selected for its higher ID’s ( each steps will convert into diagram)

Prof. Sanjay Sharma, IPS Academy #17


9893176328, 9977265269
Distributed Transaction

 Transaction may access data at several sites.


 Each site has a local “transaction manager” responsible for
o Maintaining a log for recovery purposes
o Participating a coordinating the concurrent execution of the transactions executing
at that site.
 Each site has a “transaction coordinator” which is responsible for
o Starting the execution of transaction that originate at the site
o Distributing sub-transactions at appropriate sites for execution
o Coordinating the termination of each transaction that originates at the site, which
may result in the transaction being committed at all sites or aborted at all sites
 A distributed transaction accesses resource managers distributed across a network
 A transaction is a collection of operation that represents a unit of consistency and recovery
 A transaction starts by initializing things, then reads and/or modify objects. At the end,
either
o Commit – Transaction successfully completed, Changes are saved, resources are
released; state is consistent
o Abort – terminate running process

Prof. Sanjay Sharma, IPS Academy #18


9893176328, 9977265269
Transaction Properties in Distributed System (ACID properties)

1. Atomicity- All or Nothing


2. Consistency- No violation of integrity constants, i.e., from one consistent state to
another consistent state
3. Isolation - Concurrent changes invisible, i.e. serializable
4. Durability - Committed updates persist (saved in permanent storage)

Transaction Processing Issues

 Transaction structure
o Flat vs Nested vs Distributed
 Internal database consistency
o Semantic data control and integrity enforcement
 Reliability protocols
o Atomicity and durability
o Local recovery protocols
o Global commit protocols
 Concurrency control algorithms
 Replica control protocols

Classification of Distributed Transaction

Distributed transactions are those that involve more than one server. A client transaction
becomes distributed if it invokes operations in several different servers. There are two
different ways of distributed transactions:

1. Flat Transaction
2. Nested Transaction

Flat Transactions

 In flat transaction, client makes requests to more than one server Means allowing
transactions to be composed of other transaction.
 A flat transaction completes each of its requests before going on to the next one.
 Each transaction accesses server’s objects sequentially.

Prof. Sanjay Sharma, IPS Academy #19


9893176328, 9977265269
Example
A flat transaction has a single initiating point (Begin) and a single end (Commit or abort).
They are usually very simple and generally used for short activities rather than larger ones.
A client makes requests to multiple servers in a flat transaction. Transaction T, for
example, is a flat transaction that performs operations on objects in servers X, Y, and Z.
Before moving on to the next request, a flat client transaction completes the previous one.
As a result, each transaction visits the server object in order.
A transaction can only wait for one object at a time when servers utilize locking.

Limitations of a flat Transaction :


 All work is lost in the event of a crash.
 Only one DBMS may be used at a time.
 No partial rollback is possible.

Nested Transactions

 In nested transaction, the top-level transaction can open sub-transactions and each sub-
transaction can open further sub-transactions.
 Sub-transactions at the same level can run concurrently

Prof. Sanjay Sharma, IPS Academy #20


9893176328, 9977265269
Example

As per the above diagram a transaction ‘T” open transactions T1 and T2 and both also have
sub-transactions (T11, T12 ) and (T21, T22)

Concurrency control Algorithms in parallel transactions

1. Pessimistic
a. Two-Phase locking (2PL)
i. Centralized 2PL
ii. Distributed 2PL
b. Timestamp Ordering (TO)
i. Basic TO
ii. Multi-version TO
iii. Conservative TO
c. Hybrid
2. Optimistic
a. Locking and TO based

Prof. Sanjay Sharma, IPS Academy #21


9893176328, 9977265269

online-pdf-no-copy.com

You might also like