0% found this document useful (0 votes)
8 views1 page

Untitled 1

Uploaded by

cloudlocker17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Untitled 1

Uploaded by

cloudlocker17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <stdlib.h>
#include <unistd.h>

#define BUCKET_SIZE 10
#define OUTPUT_RATE 1

void leaky_bucket(int packets[], int n) {


int bucket = 0;
for (int i = 0; i < n; i++) {
printf("Packet %d: %d units\n", i + 1, packets[i]);
if (packets[i] + bucket > BUCKET_SIZE) {
printf("Bucket overflow! Dropping packet %d\n", i + 1);
} else {
bucket += packets[i];
printf("Bucket size after adding packet %d: %d units\n", i + 1,
bucket);
}
while (bucket > 0) {
sleep(1);
bucket -= OUTPUT_RATE;
if (bucket < 0) bucket = 0;
printf("Bucket size after output: %d units\n", bucket);
}
}
}

int main() {
int packets[] = {5, 3, 8, 2, 7};
int n = sizeof(packets) / sizeof(packets[0]);
leaky_bucket(packets, n);
return 0;
}

You might also like