Write a program for congestion control using Leaky bucket algorithm.
Congestion control using Leaky Bucket algorithm.
Source Code:
File Name: leakybucket.c
#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_PACKETS 10
// Custom random function to ensure non-zero result
int get_random(int max_value) {
int result = (random() % 10) % max_value;
return result == 0 ? 1 : result;
}
int main() {
int packet_size[NUM_PACKETS];
int i, clock_tick, bucket_size, output_rate;
int remaining_data = 0, data_to_send, transmission_time, transmitted;
// Generate random packet sizes
for (i = 0; i < NUM_PACKETS; ++i)
packet_size[i] = get_random(6) * 10;
// Display the generated packet sizes
for (i = 0; i < NUM_PACKETS; ++i)
printf("\nPacket[%d]: %d bytes", i + 1, packet_size[i]);
// Get output rate and bucket size from the user
printf("\n\nEnter the Output Rate (bytes/unit time): ");
scanf("%d", &output_rate);
printf("Enter the Bucket Size (in bytes): ");
scanf("%d", &bucket_size);
// Process each incoming packet
for (i = 0; i < NUM_PACKETS; ++i) {
// Check if incoming packet + remaining data exceeds bucket size
if ((packet_size[i] + remaining_data) > bucket_size) {
if (packet_size[i] > bucket_size) {
printf("\n\nIncoming Packet[%d] size (%d bytes) exceeds bucket capacity (%d bytes) - PACKET REJECTED!", i + 1, packet_size[i], bucket_size);
} else {
printf("\n\nBucket capacity exceeded for Packet[%d] - PACKET REJECTED!", i + 1);
}
} else {
// Accept and add packet to bucket
remaining_data += packet_size[i];
printf("\n\nIncoming Packet[%d] size: %d bytes", i + 1, packet_size[i]);
printf("\nTotal bytes to transmit (in bucket): %d", remaining_data);
// Simulated transmission time (random)
transmission_time = get_random(4) * 10;
printf("\nEstimated transmission time: %d units", transmission_time);
// Transmit data in units of time
for (clock_tick = 10; clock_tick <= transmission_time; clock_tick += 10) {
sleep(1); // Simulate delay (1 second per 10 units)
if (remaining_data > 0) {
if (remaining_data <= output_rate) {
transmitted = remaining_data;
remaining_data = 0;
} else {
transmitted = output_rate;
remaining_data -= output_rate;
}
printf("\nTransmitted %d bytes ---- Remaining in bucket: %d bytes", transmitted, remaining_data);
} else {
printf("\nNo data to transmit at time unit %d!", clock_tick);
}
}
}
}
return 0;
}
Output:
$ gcc leakybucket.c
$ ./a.out
Packet[1]: 30 bytes
Packet[2]: 10 bytes
Packet[3]: 10 bytes
Packet[4]: 50 bytes
Packet[5]: 30 bytes
Packet[6]: 50 bytes
Packet[7]: 10 bytes
Packet[8]: 20 bytes
Packet[9]: 30 bytes
Packet[10]: 10 bytes
Enter the Output Rate (bytes/unit time): 10
Enter the Bucket Size (in bytes): 15
Incoming Packet[1] size (30 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!
Incoming Packet[2] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 20 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes
No data to transmit at time unit 20!
Incoming Packet[3] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 30 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes
No data to transmit at time unit 20!
No data to transmit at time unit 30!
Incoming Packet[4] size (50 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!
Incoming Packet[5] size (30 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!
Incoming Packet[6] size (50 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!
Incoming Packet[7] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 10 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes
Incoming Packet[8] size (20 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!
Incoming Packet[9] size (30 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!
Incoming Packet[10] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 10 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes
๐ป Run This C Program
Write a program for congestion control using Leaky bucket algorithm.
The selected C program is loaded
automatically into the editor.
Edit the program, provide input through
STDIN if required
and click Run.
The compiler is embedded using OneCompiler.
Your C program runs inside the embedded
C environment; Study Glance does not execute
C code directly on your web server.
Enter the input values of the program in the
STDIN box before clicking Run.