-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaky_bucket.c
36 lines (35 loc) · 952 Bytes
/
leaky_bucket.c
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
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define bucket_size 100
int main()
{
int stored = 0; // how much input is stored currently
int output, input, number_of_inputs;
printf("Enter the output rate and number of inputs: ");
scanf("%d %d", &output, &number_of_inputs);
while (number_of_inputs>=0)
{
input = (rand() % (20-1)) + 1 ;// random number between 1 and 20
int remaining_space = bucket_size - stored;
if(input < (remaining_space))
{
stored += input;
printf("The bucket has %d packets out of %d\n", stored, bucket_size);
sleep(1);
}
else
{
int overflowed = input - (remaining_space);
stored = bucket_size;
printf("The bucket has %d packets out of %d\n", stored, bucket_size);
}
stored -= output;
if (stored<0)
{
stored = 0;
}
number_of_inputs--;
}
return 0;
}