You are on page 1of 14

Subject Name:- DISTRIBUTED SYSTEM LAB

Subject Code:- RCS 751


Practical No.:- 3
Exercise No.:- 3

Ms. Anubha Dhaka


Department of CS

RCS 751/ P3 1
PRACTICAL-3
Objective: Simulation of Distributed mutual exclusion
Mutual exclusion:
1. Concurrent access of processes to a shared resource or data is executed in
mutually exclusive manner.
2. Only one process is allowed to execute the critical section (CS) at any given time.
3. In a distributed system, shared variables (semaphores) or a local kernel cannot be
used to implement mutual exclusion.
4. Message passing is the sole means for implementing distributed mutual exclusion.
5. Distributed mutual exclusion algorithms must deal with unpredictable message
delays and incomplete knowledge of the system state.
6. Three basic approaches for distributed mutual exclusion:
• Token based approach
• Non-token based approach
• Quorum based approach

RCS 751/ P3 2
Token-based approach:
7. A unique token is shared among the sites.
8. A site is allowed to enter its CS if it possesses the token.
9. Mutual exclusion is ensured because the token is unique.
Non-token based approach:
10. Two or more successive rounds of messages are exchanged among the
sites to determine which site will enter the CS next.
Quorum based approach:
11. Each site requests permission to execute the CS from a subset of sites
(called a quorum).
12. Any two quorums contain a common site.
13. This common site is responsible to make sure that only one request
executes the CS at any time.

RCS 751/ P3 3
Preliminaries-
System Model
14. The system consists of N sites, S1, S2, ..., SN.
15. We assume that a single process is running on each site. The process at
site Si is denoted by pi .
16. A site can be in one of the following three states: requesting the CS,
executing the CS, or neither requesting nor executing the CS (i.e., idle).
17. In the ‘requesting the CS’ state, the site is blocked and cannot make
further requests for the CS. In the ‘idle’ state, the site is executing outside
the CS.
18. In token-based algorithms, a site can also be in a state where a site
holding the token is executing outside the CS (called the idle token state).
19. At any instant, a site may have several pending requests for CS. A site
queues up these requests and serves them one at a time.

RCS 751/ P3 4
The Algorithm-Requesting the critical section:
• When a site Si wants to enter the CS, it broadcasts a REQUEST(tsi, i) message to all
other sites and places the request on request queuei. ((tsi, i) denotes the timestamp
of the request.)
• When a site Sj receives the REQUEST(tsi, i) message from site Si, places site Si’s
request on request queuej and it returns a time-stamped REPLY message to Si .
Executing the critical section:
• Site Si enters the CS when the following two conditions hold:
L1: Si has received a message with timestamp larger than (tsi, i) from all other sites.
L2: Si’s request is at the top of request queuei.
Releasing the critical section:
• Site Si , upon exiting the CS, removes its request from the top of its request queue
and broadcasts a time-stamped RELEASE message to all other sites.
• When a site Sj receives a RELEASE message from site Si , it removes Si’s request
from its request queue.
• When a site removes a request from its request queue, its own request may come at
the top of the queue, enabling it to enter the CS.

RCS 751/ P3 5
EXERCISE 1
Objective: Implement the Suzuki-Kasami algorithm in c
language.
Theory:
The Suzuki-Kasami algorithm is a token-based algorithm for
achieving mutual exclusion in distributed systems.
• Some Points about the Suzuki Kasami algorithm are:
‒ Only the site currently holding the token can access the CS
‒ All processes involved in the assignment of the CS
‒ Request messages sent to all nodes
‒ Not based on Lamport’s logical clock
‒ The algorithm uses sequence numbers instead
‒ Used to keep track of outdated requests
‒ They advance independently on each site

RCS 751/ P3 6
ALGORITHM
n Requesting the critical section (CS):
• When a process i wants to enter the CS, if it does not have the
token, it:
• Increments its sequence number RNi [i]
• Sends a request message containing new sequence number to
all processes in the system
When a process k receives the request(i,sn) message, it:
• Sets RNk [i] to MAX(RNk [i], sn)
• If sn < RNk [i], the message is outdated
• If process k has the token and is not in CS (i.e., is not using
token), and if RNk [i] == LN[i]+1 (indicating an outstanding
request) it sends the token to process i.
RCS 751/ P3 7
Executing the CS:
• A process enters the CS when it has acquired the token
Releasing the CS:
• When a process i leaves the CS, it:
‒ Sets LN[i] of the token equal to RNi [i]
‒ Indicates that its request RNi [i] has been executed
‒ For every process k whose ID is not in the token queue Q, it appends
its ID to Q if RNi [k] == LN[k]+1
‒ Indicates that process k has an outstanding request
‒ If the token queue Q is nonempty after this update, it deletes the
process ID at the head of Q and sends the token to that process
‒ Gives priority to others’ requests
‒ Otherwise, it keeps the token

RCS 751/ P3 8
IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
int max(int a ,int b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
int rn[5][5]={0};
int queue[10]={0};
int ln[5]={0};
int reqsite=0;
int i,j;
int sn=rn[0][0]+1;
clrscr();
RCS 751/ P3 9
rn[0][0]=rn[0][0]+1;
printf("the total no of process is 5");
printf("\nthe starting site is 0");
printf("\nsite 0 is requesting the token");
printf("\nsite 0 is broadcasting the message to all other sites");
for(i=0;i<5;i++)
{
rn[i][reqsite]=max(rn[i][reqsite],sn);
for(j=0;j<5;j++)
printf("\t%d",rn[i][j]);
printf("\n");
}
printf("\nas no site was having the token site 0 has acquired the token");
printf("\nsite 0 is executing the critical section");
delay(500);
printf("\nenter the site to enter the critical section");
scanf("%d",&reqsite);
sn=rn[reqsite][reqsite]+1;

RCS 751/ P3 10
for(i=0;i<5;i++)
{
rn[i][reqsite]=max(rn[i][reqsite],sn);
for(j=0;j<5;j++)
printf("\t%d",rn[i][j]);
printf("\n");
}
printf("\nthe token queue becomes");
queue[0]=reqsite;
for(i=0;i<10;i++)
{
printf("%d\t",queue[i]);
}
printf("\nreceiving the request message by site 0");
printf("\nsite 0 exiting the critical section");
ln[0]=rn[0][0];
printf("\nthe token is passed to site %d",reqsite);
}
RCS 751/ P3 11
VIVA QUESTIONS
1. What is Throughput, Turnaround time, waiting time and
Response time?
2. What are the sub-components of I/O manager in Windows
NT?

RCS 751/ P3 12
REFERENCES
• Book 1: Singhal & Shivaratri, "Advanced Concept in Operating Systems",
McGraw Hill
• Book 2: Ramakrishna,Gehrke,” Database Management Systems”, McGraw
Hill
• Book 3: Vijay K.Garg Elements of Distributed Compuitng , Wiley
• Book 4: Coulouris, Dollimore, Kindberg, "Distributed System: Concepts
and Design”, Pearson Education
• Book 5: Tenanuanbaum, Steen,” Distributed Systems”, PHI

RCS 751/ P3 13
Thank You

RCS 751/ P3 14

You might also like