-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaky_bucket.c
More file actions
34 lines (28 loc) · 808 Bytes
/
leaky_bucket.c
File metadata and controls
34 lines (28 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#define NUM_PACKETS 10
#define BUCKET_CAPACITY 20
#define LEAK_RATE 2
int main(){
int packet, bucket = 0;
for(int i=0; i<NUM_PACKETS; i++){
packet = rand() % 6;
sleep(1);
if((packet + bucket) <= BUCKET_CAPACITY){
printf("Adding %d packet to bucket\n", packet);
bucket += packet;
}
else{
printf("Overflow!!! Dropping %d packets.\n", (bucket + packet) - BUCKET_CAPACITY);
}
//leak
if(bucket >= LEAK_RATE){
printf("Leaking %d packets\n", LEAK_RATE);
bucket -= LEAK_RATE;
} else{
printf("Leaking %d packets\n", bucket);
bucket = 0;
}
printf("Current bucket size: %d\n", bucket);
}
}