DOS Uni3
DOS Uni3
Synchronization and
Processes
Dr. Swetaleena Sahoo
Assistant Professor
School of Electronics Engineering
KIIT Deemed to be University
Bhubaneswar
UNIT-3: Synchronization and Processes
▪ Clock Synchronization
▪ Mutual Exclusion
▪ Election Algorithms
▪ Atomic Transactions
▪ Deadlock in Distributed Systems
▪ Processes and Threads
▪ System Models
▪ Processor Allocation
▪ Process Scheduling
2
UNIT-3: Synchronization and Processes
▪ Clock Synchronization
▪ Mutual Exclusion
▪ Election Algorithms
▪ Atomic Transactions
▪ Deadlock in Distributed Systems
▪ Processes and Threads
▪ System Models
▪ Processor Allocation
▪ Process Scheduling
3
Some critical challenges and principles
of synchronization in distributed systems
1.Information Scattered Across Multiple Machines:
▪ Decentralization: In distributed systems, data and control are distributed across many machines.
Algorithms must be designed to work without gathering all the data at a single point, avoiding
bottlenecks and single points of failure.
▪ Local Decision Making: Processes rely on information that is available locally and communicate with
other processes to coordinate. This can involve passing messages or synchronizing state without central
oversight.
2.Decisions Based on Local Information:
▪ Autonomy: Each process or node operates based on its local view of the system. This local information
may include the state of the process itself and recent communications with other processes.
▪ Consistency Models: Distributed systems often use consistency models (like eventual consistency) to
ensure that, despite local decision-making, the system as a whole remains consistent over time.
4
3.Avoiding Single Points of Failure:
▪ Fault Tolerance: Distributed systems are designed to tolerate failures by ensuring that the failure
of one machine does not bring down the entire system. This is achieved through redundancy,
replication, and robust failure recovery mechanisms.
▪ Failover Mechanisms: Techniques such as leader election and backup nodes ensure that if one
node fails, another can take over its responsibilities.
4.Lack of Global Clock or Precise Time Source:
▪ Logical Clocks: Since global clocks are impractical, distributed systems use logical clocks (like
Lamport timestamps) to order events and ensure that the system can track causality and
consistency without synchronized time.
▪ Vector Clocks: For a finer granularity of event ordering and to track causality more precisely,
vector clocks can be used. They provide a way to capture the partial ordering of events in a
distributed system.
5
Centralized Resource Allocation: Problems
▪ Scenario: In a centralized approach, all resource requests (e.g., for I/O devices) are sent to a
single manager process. This manager is responsible for examining these requests, making
decisions, and allocating resources.
▪ Problem: In a large distributed system, this manager would be overwhelmed by the sheer volume
of requests and information it needs to handle. This centralized process becomes a bottleneck
because it must manage all resource requests from potentially thousands of other processes. As
the system scales up, the load on this single process becomes impractical and inefficient.
6
2.Single Point of Failure:
▪ Scenario: If the centralized manager process fails, it can result in significant disruptions. All
resource allocation decisions depend on this one manager, so its failure can bring the whole
system to a halt. For example, if the resource allocator crashes, none of the processes can get the
resources they need, causing a cascade of failures or blocking.
▪ Problem: This makes the system unreliable because the failure of one component (the manager)
affects the entire distributed system.
▪ In true sense, a distributed system should continue to function even if some of its components
fail. If a single point of failure can cause widespread disruption, it contradicts the goal of high
reliability and fault tolerance.
7
Challenges in a Distributed System:
8
Logical Clocks
9
Initialization and Updating of the Software Clock
▪ Upon system boot, the operator typically enters the current date and time, which is then
converted to the number of ticks since a known starting date and stored in memory. The system
clock is updated at every clock tick by incrementing this stored time.
10
Logical Clocks vs. Physical Clocks
▪ For many purposes, internal consistency of clocks suffices, even if the time does not match
real-world time exactly. Clocks in this context are called logical clocks.
▪ When clocks must align closely with real-world time, they are referred to as physical clocks.
11
Lamport's Happens-Before Relation
▪ If events a and b occur in the same process and a happens before b, then a → b.
▪ If event a is the sending of a message by one process and event b is its receipt by
another process, then a → b since a message cannot be received before it is sent.
12
Lamport solution
13
Way to assign times to events in a
distributed system
▪ If a happens before b in the same process, then C(a) < C(b).
▪ If a and b are the sending and receiving of a message, then C(a) < C(b).
▪ All events a and b must have unique times, C(a) ≠ C(b).
14
Physical Clocks
In systems where real-time accuracy is critical, such as in real-time systems, external physical clocks
are essential.
Having multiple physical clocks enhances efficiency and redundancy, but it introduces two
significant challenges:
15
Measurement of Time
Accurate time measurement is complex. Historically, time measurement was astronomical, based
on the solar day—the interval between two consecutive transits of the sun, which reaches its
highest point in the sky at noon each day. A solar second was defined as exactly 1/86400th of a
solar day.
Variations in Earth's Rotation
In the 1940s, it was discovered that the Earth's rotation period is not constant due to tidal friction
and atmospheric drag.
Geological studies suggest that 300 million years ago, there were about 400 days in a year,
indicating that the Earth's rotation has slowed down. Additionally, short-term variations in day
length occur due to turbulence in the Earth's core.
Mean Solar Second and Atomic Clocks
Astronomers calculated the mean solar second by averaging the length of many days. With the
invention of the atomic clock in 1948, timekeeping became more precise. The atomic second was
defined based on the cesium 133 atom's transitions, making the atomic second equal to the mean
solar second at the time of its introduction.
16
Computation of the mean solar day
17
International Atomic Time (TAI)
▪ Approximately 50 laboratories worldwide use cesium 133 clocks to measure time.
These laboratories report their clock ticks to the Bureau International de l'Heure (BIH) in
Paris, which averages them to produce International Atomic Time (TAI). TAI is the mean
number of cesium 133 clock ticks since January 1, 1958, divided by 9,192,631,770.
18
Distribution and Accuracy of UTC
▪ UTC is disseminated through various means, including shortwave radio stations like WWV in Fort
Collins, Colorado, and MSF in Rugby, Warwickshire. These broadcasts offer UTC with an accuracy
of about ±1 millisecond, though atmospheric conditions can reduce practical accuracy to ±10
milliseconds. Satellites also provide UTC services with high accuracy, requiring precise knowledge
19
Clock Synchronization Algorithms
▪ Clock Synchronization
▪ Clock Synchronization Algorithms
▪ Mutual Exclusion Algorithms
▪ Election Algorithms
▪ Atomic Transactions & Modeling
▪ Atomic Transaction Implementation
▪ Concurrency Control Algorithms in Atomic Transaction
20
Basic Concepts:
Goal:
1.If one machine has a WWV(UTC provider used by National Institute
of Standard Time(NIST) receiver ,then to keep all other machines
synchronized .
2.If no machine have WWV receiver, then each machine keeps track of
its own time as well as keep all other machines together w.r.t the
average time stamp.
21
Basic Concepts cont..
Assumtions:
▪ Each machine hava a timer and its interrupt is H times a second.
▪ When (the timer goes off),the interrupt handler adds 1 to a s/w
clock(value #C),which keeps track of the number of interrupts.
▪ When UTC(Universal Coordinated Time) time is t, the value of the
clock on machine p is Cp(t).
▪ In Perfect world
Cp(t)=t for all p and t
OR
dC/dt=1
UNIT-3 22
Basic Concepts cont..
23
Basic Concepts cont..
24
Cristian’s Algorithm
25
Cristian’s Algorithm cont..
27
Cristian’s Algorithm cont..
PROBLEMS OF Cristian’s Algorithm:
1.(Major problem)
Time must never run backward.
If the sender’s clock is fast, Cutc will be smaller than the sender’s current value of
C which is serious problem.
SOLUTION:
Such change must be introduced gradually,instead of jumping it forward all at
once.
EXAMPLE:Suppose the timer is set to generate 100 interrupts per second ,each
interrupt would add 10msec to the time and when slowing down,the interrupt
routine adds 9msec each time,until the correction has been made.
28
Cristian’s Algorithm cont..
2. (Minor problem)
It takes a nonzero amount of time for the time server’s reply to get
back to the sender.
SOLUTION:
Sender must record accurately the T0 and T1 which are measured
using the same clock, so the interval will be relatively accurate.
29
Berkeley Algorithm
31
Averaging Algorithms:
▪ This algorithm is known as decentralised algorithm.
▪ Divide the time into fixed length resynchronization interval
▪ The ith interval starts at T0 +iR and runs until T0+(i+1)R
▪ T0 is agreed upon moment on past and R is system parameter.
▪ At the beginning of each interval, every machine broadcasts the
current time according to its clock as different clock has different
speed.
▪ Then it starts a local timer to collect all other broadcasts during
some interval S.
▪ The algorithm runs to compute a new time from them by averaging
the values from other machines.
32
Averaging Algorithms cont...
Variations:
1.Discard the m highest and m lowest values and average the rest,as
the extreme values consider as faulty clocks.
2. Collect each message including its propagation time from the
sources.
33
Use of synchronized clocks
1. At most once message delivery:
▪ Traditional approch:
▪ Each messsage has a unique number and server stores all the numbers of
message and can detect new messages from retransmission.
▪ Problem:
▪ If (server crashes and reboots ) it loses its table of message numbers.
▪ Modified approach ( With time ):
▪ Each message carries a connection ID and timestamp.
▪ for each connection server records in table the most recent timestamp it has
seen,
• if (msg for a connection is lower than the timestamp stored for that connection) then it is
rejected.
34
Use of synchronized clocks cont..
2.Clock based cache consistency
▪ It is used in distributed file system.
▪ Traditional approch:
▪ Each client maintains local cache which is partitioned as read and
write file.
▪ Problem :
▪ If a client has a file cached for reading, before another client can get a
copy for writting,the the server has to first ask the reading client to
invalidate its copy.
▪ Solution for the extra overhead can be eliminated by synchronized
clock
35
Use of synchronized clocks cont..
36
UNIT-3:Synchronization in Distributed Systems
▪ Clock Synchronization
▪ Mutual Exclusion
▪ Election Algorithms
▪ Atomic Transactions
37
What is Mutual Exclusion ?
▪ When a process is accessing a shared resource, the process is said to
be in a critical section (CS).
▪ It ensures that no two process can be in the same CS at the same
time.
▪ In uniprocessor systems, CS’s are protected using semaphores,
monitors, and some similar constructs.
▪ Different algorithms based on message passing to implement mutual
exclusion in distributed systems are
#1. Centralized algorithms
#2. Distributed algorithms
#3. Token Ring algorithms
38
Centralized Algorithms: (1 Boss)
▪ One process is elected as the coordinator (highest network address).
▪ Whenever a process wants to access a shared resources, it sends
request to the coordinator to ask the permission.
▪ Queue is maintained to track the request.
39
Contd..
Case 1:
Process 1 asked permission to Process 3 to access shared resources (as
no other processes is currently in the CS) so, permission granted.
Case 2:
Process 2 asked permission to Process 3, to access some shared
resources, but it is already full, so no reply and Process 2 has been
pushed into the queue.
Case 3:
Process 1 released the CS, hence shared resources is free.
Dequeue has been done (popping of Process 2 ), and send OK for
accessing it.
40
Advantages
▪ Simple to use.
41
Disadvantages
▪ If the single system (co-ordinator) fails, the whole system will
collapse.
42
#2.Distributed Algorithm
43
Cont.
Case 3:
• If the receiver wants to enter the CS but not yet done.
• It compares the timestamp in the incoming message with the one
contained in the message that it has sent to everyone.
• Whichever has lowest (wins), and the winner will not say OK, rather
the looser will say OK, and the winner will continue in the CS.
• After exit from the CS, it sends OK message to all processes on its
queue and deletes them all from the queue.
44
Cont.
45
Cont.
a) Process 0 and Process 2 wants to enter the same CS at the same
time.
b) Process 0 has the lowest time stamp, so its win.
c) When Process 0 is done, it sends OK also, so Process 2 can now
enter CS.
Analysis
• In centralized algorithm, mutual exclusion is guaranteed without
deadlock and starvation.
• In distributed algorithm ,the number of messages required per entry
is now 2(n-1), where the total number of process in the system is ‘n’,
no single point failure exists.
• But unfortunately single point failure has been replaced by n points of
failure. If any process crashes, it fails to respond to requests.
46
Cont.
• Therefore, when a request comes in, the receiver should always
sends a reply, either granting or denying permission.
• Whenever either a request or a reply is lost, the sender times out and
keeps trying until either a reply comes back or the sender concludes
that the destination is dead.
47
#3. Token Ring Algorithm
▪ A logical ring is constructed in which each process is assigned a
position in the ring.
▪ It doesn’t matter what the ordering is, but each process knows who is
next in line after itself.
▪ When the ring is initialized, process 0 is given a token.
▪ The token circulates among the ring, as it passed from process K to
process K+1 (modulo the ring size) in point to point messages.
▪ When a process acquires the token,
• it checks whether(it needs to enter CS)
1. if yes, the process enters into CS, does all the work, and leaves the CS.
▪ After its exit, it passes the token along the ring.
48
#3. Token Ring Algorithm Cont.
▪ Not allowed to enter the second CS, with the same token.
▪ If (the process is handed the token by its neighbour but doesn’t need
it for CS)
• it just passes it along .
▪ When(no process wants to enter any CS)
• the token just circulates at a speed around the ring.
▪ Only one process can have the token at any instant and only one
process can be in a CS.
▪ No starvation.
▪ Once (a process decides it wants to enter CS, at worst)
• it will has to wait for every other process to enter and leave one CS.
49
Continue…
50
Disadvantages
▪ If (the token is ever lost)
• it must be regenerated.
▪ Detecting that, it is lost is difficult, since the amount of time between
successive appearance of token on the network is unbounded.
▪ It could not said, whether it is lost or it is still being used by some
other processes.
Analysis:
• If (the process crashes)
• it is little easier to recover as compared to other cases.
• If(A dead process will be detected by neighbour)
• Can be removed from the group,
• and the token can be thrown to the next process in the queue.
51
Comparison of the three Algorithms
52
Election Algorithms
▪ A Bully Algorithm
▪ A Ring Algorithm
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Atomic Transaction Implementation
▪ Private Workspace
▪ Writeahead Log
▪ Two-Phase Commit Protocol
72
Private Workspace.
When a process starts a transaction, it is given a private workspace containing all the files (and other
objects) to which it has access. Until the transaction either commits or aborts, all of its reads and writes go
to the private workspace, rather than the "real" one, by which we mean the normal file system.
Advantage of this method is actually giving a process a private workspace at the instant it begins a trans-
action.
Disadvantage is that the cost of copying everything to a private workspace is prohibitive, but various
optimizations make it feasible. The first optimization is based on the realization that when a process reads a
file but does not modify it, there is no need for a private copy. It can just use the real one (unless it has
been changed since the transaction started).
When a process starts a transaction, it is sufficient to create a private workspace for it that is empty except
for a pointer back to its parent's workspace.
When a file is opened for writing, it can be located in the same way as for reading, except that now it is first
copied to the private workspace but here also we can optimize even for write operation entire file need not
to be copied into private workspace,just copy entire index and modifieble block has both at original file and
private workspace. when transaction commit, changes made in private work space blocks will
effected/saved in original file of respected blocks.
73
In UNIX, the index is the i-node. Using the private index, the file
can be read in the usual way, since the disk addresses it contains
are for the original disk blocks. However, when a file block is first
modified, a copy of the block is made and the address of the
copy inserted into the index, as shown in Fig. 3-18. The block can
then be updated without affecting the original. Appended blocks
are handled this way too. The new blocks are sometimes called
shadow blocks.
In Fig. 3-18.
(a) indexes of file before modifying.
(b) Situation transacion has modified block 0 and new block (block 3)
appended to file. here observe private workspace indexes and extra
copis.
(c) after comminting transactions
If the transaction aborts, the private workspace is simply deleted and all the private blocks that it points to are
put back on the free list. If the transaction commits, the private indices are moved into the parent's workspace
atomically, as shown in Fig. 3-18(c). The blocks that are no longer reachable are put onto the free list.
74
Writeahead Log(intentions list)
With this method,
Files are actually modified in place, but 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.
Only after the log has been written successfully is the change made to the file.
75
Case 2: If the transaction aborts
If the transaction aborts, the log can be used to back up to the original state. Starting at the end and
going backward, each log record is read and the change described in it undone. This action is called a
rollback.
Case 3: The log can also be used for recovering from crashes.
Suppose that the process doing the transaction crashes just after having written the last log record, but before changing x.
After the failed machine is rebooted, the log is checked to see if any transactions were in progress at the time of the crash.
When the last record is read and the current value of x is seen to be 1, it is clear that the crash occurred before the update
was made, so x is set to 4.
If, on the other hand, x is 4 at the time of recovery, it is equally clear that the crash occurred after the update, so nothing
need be changed.
Using the log, it is possible to go forward(do the transaction) or go backward(undo the transaction)
76
Two-Phase commit Protocol(Atomic Commit)
As we have pointed out repeatedly, the action of committing a transaction must be done atomically, that is, instantaneously
and indivisibly.
In a distributed system, the commit may require the cooperation of multiple processes on different machines, each of
which holds some of the variables, files, and data bases, and other objects changed by the transaction.
Two-phase commit protocol one of widely used protocol for achieving atomic commit in a distributed system.
The basic idea is illustrated in Fig. 3-20. One of the processes involved functions as the coordinator. Usually, this is the one
executing the transaction.
The commit protocol begins when the coordinator writes a log entry saying that it is starting the commit protocol, followed
by sending each of the other processes involved (the subordinates) a message telling them to prepare to commit.
77
When( a subordinate gets the message) it checks to see if it
is ready to commit, makes a log entry, and sends back its
decision.
When the coordinator has received all the responses, it
knows whether to commit or abort. If all the processes are
prepared to commit, the transaction is committed.
Due to the use of the log on stable storage, this protocol is highly resilient in the face of (multiple)
crashes.
78
UNIT-3: Synchronization and Processes
▪ Clock Synchronization
▪ Clock Synchronization Algorithms
▪ Mutual Exclusion Algorithms
▪ Election Algorithms
▪ Atomic Transactions & Modeling
▪ Atomic Transaction Implementation
▪ Concurrency Control Algorithms in Atomic Transaction
79
Concurrency Control
▪ Concurrency control ensures the simultaneous execution of multiple transactions in different processes
without interfering with each other.
▪ The mechanism used for concurrency control is called the concurrency control algorithm.
▪ Some concurrency control algorithms are:
• #1. Locking
• #2. Optimistic Concurrency Control
• #3. Timestamps
80
Locking
▪ When a process needs to read or write a file it first locks the file.
▪ Locking can be done using
▪ A single centralized lock manager or
▪ Local lock manager on each machine.
▪ The lock manager maintains a list of locked files and rejects all attempts to lock files that are
already locked by other process.
▪
81
Two-phase Locking
▪ Growing phase:
▪ The process acquires all the locks it needs.
▪ Shrinking phase:
▪ The process releases the locks.
82
Example: Without Deadlock
Steps Transaction 1(T1) Transaction 2(T2) Consider the same database with three records A, B, and
C and two transactions T1 and T2.
1 S(A)
Transaction 1:
2 R(A)
Read record A
3 S(A) Write record C
4 R(A) Read record B
5 E(C) Transaction 2:
6 W(C) Read record A
7 S(B)
Read record B
Write record C
8 R(B)
Read Lock (Shared Lock): If a read lock is set on a file
9 S(B) other read locks are permitted but no write lock is
10 R(B) allowed.
11 U(A), U(C)
12 E(C) Write Lock(Exclusive Lock ): If a write lock is set on a file
13 W(C) no read lock or other write lock is permitted.
14 U(B)
For T1, steps 1-10 are the growing phase, and steps
15 U(A)
11-14 are the shrinking phase.
16 U(B), U(C)
For T2, steps 3-13 are the growing phase, and steps
15-16 are the shrinking phase.
83
Example: Deadlock
Time Transaction 1(T1) Transaction 2(T2)
Consider the same database with three records A, B,
and C and two transactions T1 and T2.
1 S(A)
Transaction 1:
2 R(A) Read record A
3 E(C) Write record B
4 W(C) Read record C
5 E(B) Transaction 2:
6 W(B)
Write record B
Write record C
7 E(B) wait until T1
release E(B)
8 E(C) wait until T2
Read Lock (Shared Lock): If a read lock is set on a file
release E(C) other read locks are permitted but no write lock is
allowed.
It leads to deadlock.
84
Strict two-phase Locking
▪ The shrinking phase does not take place until the transaction has finished running and has either
committed or aborted.
▪ Advantages
▪. A transaction reads a value written by a committed transaction
▪ All lock acquisitions and releases can handled by the system without the
▪ transaction being aware of it.
85
Disadvantages of Locking Protocols
▪ Deadlock:
▪ If two processes each try to acquire the same pair of locks but in the opposite direction may
results in deadlock.
▪ Solution:
▪ Acquiring all locks in some canonical order
▪ Maintaining an explicit graph of which process has which locks, and checking the
▪ graph for a cycle
▪ A timeout scheme can be used
86
Optimistic Concurrency Control
▪ Do whatever you want to without paying attention to what anybody else is doing
in practice.
▪ The conflicts are rare but not impossible.
▪ The optimistic concurrency control 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 was aborted. If not, it is committed.
87
Timestamps
Time Stamp Ordering algorithm uses timestamps to order transactions and resolve
conflicts.
Read Timestamp (TRD): For each data item, the TRD indicates the largest
timestamp of any transaction that has successfully read the item.
Write Timestamp (TWR): For each data item, the TWR indicates the largest
timestamp of any transaction that has successfully written to the item.
88
Read Operation
• then Ti is aborted and restarted with a new timestamp. This is because Ti is trying to read a version of X
that has already been overwritten by a newer transaction.
89
Write Operation
▪ When a transaction Ti with timestamp T(Ti) attempts to write to a data item X:
• then Ti is aborted and restarted. Ti is trying to overwrite a value that has already been read by a newer
transaction.
▪ If(T(Ti)< TWR(X))
90
Example
• There are three transaction alpha, beta and gamma.
Alpha ran a long time ago, and used every file needed by
beta and gamma.
• So, all files have read and write timestamps set to
alpha’s time stamps.
• Beta and gamma start concurrently, with beta having
a lower timestamp than gamma .
92
Conclusion
▪ Transaction has many advantages and thus are a promising technique for building reliable
distributed system.
▪ It has great implementation complexity which yield low performance.
93
Process and Threads
▪ Introduction
▪ Usage
94
Introduction
Processes
· Definition: A process is an instance of a program in execution. It is an active
entity, with a program counter specifying the next instruction to execute and a
set of associated resources (such as memory, files, and I/O devices).
· Characteristics:
o Has its own memory space.
o Can communicate with other processes via Inter-Process Communication
(IPC) mechanisms like pipes, shared memory, message queues, etc.
o More overhead due to context switching.
95
Threads
· Definition: A thread is the smallest unit of execution within a
process. Multiple threads can exist within the same process, sharing
the same memory space.
· Characteristics:
o Share the same memory space and resources of the process.
o More efficient than processes due to lower context-switching
overhead.
o Easier and more efficient for communication within the same
process.
96
Why Use Processes?
· Isolation: Processes are isolated from each other, providing better
fault tolerance and security.
· Resource Management: Suitable for tasks that require a dedicated
amount of resources.
97
Why Use Threads?
· Efficiency: Threads are lightweight compared to processes, allowing faster
creation, termination, and context switching.
· Shared Memory: Easier communication and data sharing since threads
within the same process share memory.
· Concurrency: Useful for performing multiple operations concurrently
within the same application.
98
Implementing Threads in User Space and Kernel Space
99
3.Thread Management:The library handles scheduling, context switching,
and synchronization between threads.
100
Code for User Space Threads:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
pthread_t thread1, thread2;
101
Code for User Space Threads (Cont’d...)
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
102
Kernel Space Threads
Kernel space threads are managed directly by the operating system kernel.
Below are steps to implement kernel space threads.
Steps to Implement Kernel Space Threads:-
Thread Creation:Use system calls or native threading APIs provided by
the operating system.
Thread Management:The kernel manages scheduling, context switching,
and synchronization between threads.
Synchronization:Use OS-provided synchronization primitives like
mutexes, semaphores, or condition variables.
103
Code for Kernel Space Threads:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pthread_t thread1, thread2;
104
Code for Kernel Space Threads (Cont’d...):
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
105
#1: Introduction to Threads and Remote
Procedure Calls (RPC)
▪ Threads: A thread is the smallest unit of a process that can be
scheduled and executed by the operating system.
▪ Threads within the same process share the same memory space but
have separate execution contexts, such as the program counter and
stack.
▪ Threads can be implemented at the user level or kernel level:
▪ User-Level Threads: Managed by a user-level library, invisible to the
kernel.
▪ Kernel-Level Threads: Managed directly by the operating system.
106
Remote Procedure Calls (RPC):
▪ RPC is a protocol that one program can use to request a service from
a program located on another computer in a network.
▪ Abstracts the procedure call mechanism across a network, making a
remote service invocation appear as a local procedure call.
• (RPC as LPC)
▪ Key components:
▪ Client: The entity requesting the service.
▪ Server: The entity providing the service.
▪ Stub: Generated code that acts as an intermediary, handling
communication between the client and server.
107
System Models
▪ Workstation Model
108
System Models: A. Workstation Model
▪ Each user has a dedicated workstation, and all the processes
initiated by that user are executed on their workstation.
▪ Advantages:
▪ Users have control over their work environment.
▪ Can handle moderate workloads effectively.
▪ Disadvantages:
▪ Underutilization of resources, if( the workstation is idle).
▪ Scalability issues, when (workload exceeds the capacity of a single workstation).
109
B.Using Idle Workstations
▪ Utilizes the idle processing power of workstations when they are not
in use by their primary user.
▪ Mechanism:
▪ Idle workstations are identified by the system.
▪ Distributed tasks (such as background processing) are offloaded to
these idle machines.
▪ Advantages:
▪ Better utilization of resources across the network.
▪ Reduces the need for additional hardware for background processing
tasks.
110
B.Using Idle Workstations (Cont.)
▪ Disadvantages:
▪ Complexity in managing and scheduling tasks across multiple
workstations.
▪ Possible disruption of the primary user's tasks if the workstation
becomes active.
111
C. The Processor Pool Model
▪ Disadvantages:
▪ Increased overhead in managing the processor pool.
▪ Potential for contention if multiple tasks require a large number of
processors simultaneously.
113
Comparison of System Models
Resource underutilization,
Workstation Model User control, moderate workload handling
scalability issues
Complex management, potential
Using Idle Workstations Resource optimization, reduced hardware costs
user disruption
Processor Pool Model High flexibility, efficient resource use Management overhead, possible
processor contention
114
Deadlock Detection and Prevention
Techniques in DOS
Deadlock
Definition: A set of processes is to be in a deadlock when every process in the set waits
for an event that can only be caused by another process in the set
R1 R2 Objective:
carefully)
1. Each machine maintains the resource graph for its own processes and
resources, and a central coordinator maintains the resource graph for
the entire system (the union of all the resource graphs)
2. When (the coordinator detects a cycle), it kills off one process to break
the deadlock.
119
Contd ..
Unlike the centralized system (single processor), in a distributing
environment, all the information has to be sent to the right place explicitly.
Here, Each machine maintains a graph for its own processes and resources.
Possible situations to get the informations to the right place:
1. Whenever, an arc is added or deleted from the resource graph, a
message can be sent to the coordinator providing the update.
2. Periodically, every message can send a list of arcs added or deleted since
the previou update.
3. The coordinator can ask for informations when it needs it.
Unfortunately, none of these methods work well. Why ? Explain in the next slides 120
Centralized Deadlock Detection Explanation
1. Process A and Process B are running in Machine 0, and Process C is running in Machine 1.
3. A holds S but wants R, which it cannot have because B is using it; C has T wants S.
121
Contd ..
Now C has both S & T, now C can execute its task 125
Contd..
Machine 1 sends a message to the coordinator announcing the fact that B is requesting its
resource, T
127
Contd..
Unfortunately, the message from Machine 1 arrives first, leading the coordinator to
construct the graph as shown above.
Coordinator incorrectly concludes that a deadlock exists and kills some process
Such a situation is called False deadlock 128
Contd ..
1. Many deadlock algorithms in distributed systems produce False deadlocks like
this due to incomplete or delayed information.
2. One possible way out might be to use Lamport’s algorithm to provide global
time.
i) Message from machine 1 to the coordinator is triggered by the request from
machine 0, so the message from machine 1 will have later timestamp than the
message from machine 0 to the coordinator.
ii) When the coordinator gets a message from machine 1 that leads it to
suspect deadlock, it could send a message to every machine to the system
saying that: “ I just received a message with timestamp T that leads to
deadlock. If anyone has a message for me with earlier timestamp, send me
immediately.”
iii) When every message will send positively or negatively, the coordinator will
see that arc from R to B is vanished. Hence, the system is still safe.
129
Distributed Deadlock Detection
1. When a deadlock is detected in a system based on atomic transactions,
it is resolved by aborting one or more transactions.
130
Distributed Deadlock Detection- Chandy-Misra-Haas
Algorithm
Idea of the algorithm
131
Distributed Deadlock Detection- Chandy-Misra-Haas
Algorithm (contd..)
132
Contd ..
133
Contd..
134
Contd ..
1. The Chandy-Misra-Haas Algorithm is invoked when a process has to wait
for some resources, process 0 blocking on process 1
2. At that point, a special probe message is generated and sent to the
process(es) holding the needed resources.
3. The probe message consists of three numbers:
a. The Process that just blocked
b. The process of sending the message
c. The Process to whom it is being sent.
The initial message from 0 to 1 contains the triple (0,0,1)
1. When the message arrives, the recipient checks to see if it itself is waiting
for any processes.
135
Contd ..
5. If so, the message is updated
i) Keeping the first field the same but
ii) replacing the second field by its own process number and
iii) The Third one by the number of process it is waiting for.
6. The message is sent to the process on which it is blocked. If it is blocked on multiple
processes, all of them are sent (different) messages.
7. If the 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 system is deadlocked.
136
Explanation with Example
137
Contd..
138
Contd ..
139
Cond..
140
Cond..
141
Contd ..
✔ There are various ways to in which the deadlock can be broken.
✔ One way is to have the process that initiated the probe commit suicide
✔ However, this method has problems if several processes invoke the
algorithm simultaneously.
✔ For example, imagine that both 0 and 6 block at the same moment, and
both initiated probes. Each would eventually discover the deadlock, and
each would kill itself. It is overkill. Getting rid of one of them is enough.
142
Deadlock Prevention Algorithm
Possible Solutions:
✔ To order the resources and require processes to acquire them in
strictly increasing order. This approach means that a process
can never hold high resources and ask for a low one, thus making
cycles impossible.
143
Deadlock Prevention Algorithm (contd..)
3. When one process is about to block waiting for a resource that another process is using, a
check is made to see which has a larger timestamp (i.e., is younger).
❖ We can then allow the wait only if the waiting process has a lower timestamp (i.e., is
older). Here, the timestamp is always increasing if we follow any chain of waiting
processes, so cycles are impossible.
❖ Alternatively, we can allow processes to wait only if the waiting process has a higher
timestamp than the process waited for; here, the timestamps decrease along the chain.
144
Contd..
Although both methods prevent deadlock, it is wiser to give priority to older processes
because
They have run longer, so the system has a larger investment in these processes.
They are likely to hold more resources
A young process that is killed off will eventually age until it is the oldest one in the
system, and that eliminates starvation.
Killing a transaction is relatively harmless since, by definition, it can be restarted
safely later.
To make this technique clear, two algorithms are discussed next.
Types of Algorithm
147
Contd ..
So the Young process will be killed (see the next slide) 148
Contd..
149
Contd ..
Observation:
✔ The young process, after being killed, will then start up again and be
killed again. This cycle may go on many times before the old one releases
the resource.
✔ Once we are assuming the existence of transactions, we can do
something that has previously been forbidden: take resources away
from running processes.
✔ When a conflict arises, instead of killing the process of making the
request, we can kill the resource owner. Without transactions, killing a
process might have severe consequences. With transactions, this effect
will vanish magically when the transaction dies.
150
Wound-Wait deadlock Prevention Algorithm
If an Old process wants a resource held by a Young process, the old one will preempt
the young process – wounded and killed, restarts and wait.
151
Contd..
If a Young process wants a resource held by an Old process, the Young one will wait
152
Questions
153
Homework Questions
155
Q &A
156
QUESTIONS:
157
Homework Questions
158
Processor Allocation
Algorithms
159
Processor allocation algorithms
❑ A distributed system consists of multiple processors & thus some
algorithm is needed to decide which process should run on which
machine.
❑ System is denoted as a weighted graph with each node being a process & each arc represents flow
of messages between 2 processes.
❑ The problem is reduced to finding a way to partition the graph into k disjoint sub-graphs (with
total CPU & memory needs below some limits in each sub-graph) so that network traffic is
reduced.
=============================================================================
• In (b), a different partitioning is followed which yields less
network traffic (28 units).
AB(3) + BE(2) + GH(4) + EH(4)
+ CI(5) + CD(3) + IF(5) + HI(2) = 28
❑ Graph based algorithm have limited application as they need complete information in advance.
❑ Centralized algorithm is also called up-down algorithm where the coordinator maintains a usage table
with one entry per user. (initially zero).
❑ When a process is to be created & the machine that created it decides that the process is to be run
elsewhere, it asks the usage table coordinator to allocate it a processor. If there is one available & no one
else wants it, the request is granted. If no processors are free, the request is denied.
❑ When a user is running processes on other user’s machines, it accumulates penalty points which are
added to its usage table entry. When it has unsatisfied requests pending, penalty points are subtracted
from its usage table entry.
❑ When no requests are pending & no processors are being used, the usage table entry is moved a certain
no of points closer to zero, until it reaches zero. This way, the score gets up & down and hence the name.
❑ Usage table entries can be positive, negative or zero. A +ve score means that it has surplus resources, -ve
score means that it needs resources while a zero score is neutral.
163
Heuristic used for processor allocation:
When a processor becomes free, the pending request whose owner has the least score
wins. Thus a user with no processor & who had a longer pending request will win
over someone who is using many processors. Thus, the resources are allocated fairly.
164
Hierarchical algorithm
❑ Centralized algorithms fail to scale well in large systems due to single point of
failure.
❑ For a group of ‘k’ workers, 1 manager keeps track of machines that are busy/idle.
For a large network, multiple managers or sub-managers are assigned as they work
in a hierarchy.
❖ If the manager receiving the request think it has very few processors available, it passes the request
upward in the tree to its boss. If the boss can’t handle it either, the request continues propagating
upward until it reaches a level that has enough available workers at its disposal.
❖ At that point, the manager splits the request into parts & sends them to the managers below it & the
process continues until the allocation hits bottom level. At the bottom level, the processors are marked
as “busy”.
166
Sender-Initiated algorithm
❑ When a process is created, the machine on which it originates
send probe messages to a randomly chosen machine asking if its
load is below certain threshold value.
If so, the process is sent there.
If not, another machine is chosen for probing.
167
Receiver-Initiated algorithm
❑ If no work is found with ‘N’ probes, the receiver temporarily stops asking,
completes any work it has queued up & trie sagain when the next process
finishes.
❑ If no work is available, the machine goes idle & after some interval it
starts probing again.
168
UNIT-3: Synchronization and Processes
# 1. Clock Synchronization
# 2. Mutual Exclusion
# 3. Election Algorithms
# 4. Atomic Transactions
# 5. Deadlock in Distributed Systems
# 6. Processes and Threads
# 7. System Models
# 8. Processor Allocation
#9. Process Scheduling
169
#9: Scheduling in Distributed System
▪ In DOS each processor does its own local scheduling (assuming that it has
multiple processes running on it), without regard to what other processors
are doing. Usually this approach works fine.
Time P0 P1 P2 P3 P4 P5 P6 P7
slot
0 × ×
1 × ×
2 × × ×
3 × ×
4 × × ×
5 × ×
Explanation: Column i consists of all processes that run on processor i.
Each processor use a “Round Robin Scheduling Algorithm”
A broadcast message could be used to tell each processor when to do process switching, to keep
the time slice synchronized.
173
Example: N-way Multiprogramming
Time slot P0 P1 P2 P3 P4 P5 P6 P7
0 × ×
1 × ×
2 × × ×
3 × ×
4 × × ×
5 × ×
Explanation: Performance improvement method: Break the matrix into rows and concatenates the rows to
form one long row.
With N processors, any N consecutive slots belong to different processors.
To allocate a new process group to slots, one lays a window N slots wide over the long row such
that the leftmost slot is empty but the slot just outside the left edge of the window is full.
If sufficient empty slots are present in the window, the processes are assigned to the empty slot;
otherwise, the window is slid to the right and the algorithm is repeated.
174
Thank You!
175