8. Write a C program for congestion control using Leaky bucket algorithm.
Ans:
The Leaky Bucket algorithm is a simple method for controlling congestion in a
network by regulating the rate at which data packets are sent. It ensures that
the data flow is constant and prevents bursty traffic, thereby avoiding
congestion.
Leaky bucket algorithm
Suppose we have a bucket in which we are pouring water at random points in
time but we have to get water at a fixed rate to achieve this we will make a
hole at the bottom of the bucket. This will ensure that the water coming out is
at some fixed rate. If the bucket gets full, then we will stop pouring water into
it.
The input rate can vary but the output rate remains constant. Similarly, in
networking, a technique called leaky bucket can smooth out bursty traffic.
Bursty chunks are stored in the bucket and sent out at an average rate.
In the above figure, we assume that the network has committed a bandwidth
of 3 Mbps for a host. The use of the leaky bucket shapes the input traffic to
make it conform to this commitment. In the above figure, the host sends a
burst of data at a rate of 12 Mbps for 2s, for a total of 24 Mbits of data. The
host is silent for 5 s and then sends data at a rate of 2 Mbps for 3 s, for a total
of 6 Mbits of data. In all, the host has sent 30 Mbits of data in 10 s. The leaky
bucket smooths out the traffic by sending out data at a rate of 3 Mbps during
the same 10 s.
Without the leaky bucket, the beginning burst may have hurt the network by
consuming more bandwidth than is set aside for this host. We can also see that
the leaky bucket may prevent congestion.
How Does the Leaky Bucket Algorithm Work?
A simple leaky bucket algorithm can be implemented using FIFO queue. A FIFO
queue holds the packets. If the traffic consists of fixed-size packets (e.g., cells in
ATM networks), the process removes a fixed number of packets from the
queue at each tick of the clock. If the traffic consists of variable-length packets,
the fixed output rate must be based on the number of bytes or bits.
The following is an algorithm for variable-length packets:
1. Initialize a counter to n at the tick of the clock.
2. Repeat until n is smaller than the packet size of the packet at the head of
the queue.
1. Pop a packet out of the head of the queue, say P.
2. Send the packet P, into the network
3. Decrement the counter by the size of packet P.
3. Reset the counter and go to step 1.
Note: In the below examples, the head of the queue is the rightmost position
and the tail of the queue is the leftmost position.
C Program for Leaky Bucket Algorithm
Here's a C program that simulates the Leaky Bucket algorithm:
c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For sleep function
void leakyBucket(int bucket_size, int output_rate, int *packets, int
num_packets) {
int bucket_content = 0;
for (int i = 0; i < num_packets; i++) {
printf("Packet %d: %d bytes arrived\n", i + 1, packets[i]);
if (packets[i] <= (bucket_size - bucket_content)) {
bucket_content += packets[i];
printf("Bucket content: %d bytes\n", bucket_content);
} else {
printf("Packet %d: Overflow. Packet discarded.\n", i + 1);
}
while (bucket_content > 0) {
sleep(1);
if (bucket_content < output_rate) {
printf("Sent %d bytes\n", bucket_content);
bucket_content = 0;
} else {
printf("Sent %d bytes\n", output_rate);
bucket_content -= output_rate;
}
printf("Bucket content after output: %d bytes\n", bucket_content);
}
printf("\n");
}
}
int main() {
int bucket_size, output_rate, num_packets;
printf("Enter the bucket size in bytes: ");
scanf("%d", &bucket_size);
printf("Enter the output rate in bytes/sec: ");
scanf("%d", &output_rate);
printf("Enter the number of packets: ");
scanf("%d", &num_packets);
int *packets = (int *)malloc(num_packets * sizeof(int));
printf("Enter the packet sizes in bytes:\n");
for (int i = 0; i < num_packets; i++) {
scanf("%d", &packets[i]);
}
leakyBucket(bucket_size, output_rate, packets, num_packets);
free(packets);
return 0;
}
Explanation
1. Input:
o The program prompts the user to enter the bucket size, the output
rate, and the number of packets.
o The user is then asked to enter the sizes of each packet.
2. Leaky Bucket Function:
o The leakyBucket function simulates the Leaky Bucket algorithm.
o It processes each packet and checks if it can be added to the
bucket without causing overflow.
o If the packet can be added, the bucket content is updated;
otherwise, the packet is discarded.
o The function then simulates the output of packets from the bucket
at a constant rate.
3. Main Function:
o The main function collects the user inputs and calls the
leakyBucket function.
o After processing, it frees the allocated memory for packet sizes.
Example
If you run the program and provide the following inputs:
Enter the bucket size in bytes: 10
Enter the output rate in bytes/sec: 3
Enter the number of packets: 4
Enter the packet sizes in bytes:
5
3
7
2
The output will simulate the process of handling each packet, checking for
overflow, and sending data at the specified rate, providing a clear illustration of
the Leaky Bucket algorithm in action.