Leaky bucket

PHOTO EMBED

Fri Nov 22 2024 04:09:36 GMT+0000 (Coordinated Universal Time)

Saved by @sem

#include <stdio.h>

int main() {
    int incoming, outgoing, bucket_size, num_inputs;
    int store = 0;  // Tracks the current amount of data in the bucket

    // Take input for bucket size, outgoing rate, and number of inputs
    printf("Enter bucket size, outgoing rate and number of inputs: ");
    scanf("%d %d %d", &bucket_size, &outgoing, &num_inputs);

    // Loop through all incoming packets
    for (int i = 0; i < num_inputs; i++) {
        printf("Enter incoming packet size: ");
        scanf("%d", &incoming);

        // Display incoming packet size
        printf("Incoming packet size: %d\n", incoming);

        // Check if the packet can be stored in the bucket
        if (incoming <= (bucket_size - store)) {
            store += incoming;
            printf("Bucket buffer size: %d out of %d\n", store, bucket_size);
        } else {
            // If the bucket is full, drop excess packets
            printf("Dropped %d packets\n", incoming - (bucket_size - store));
            store = bucket_size;
            printf("Bucket buffer size: %d out of %d\n", store, bucket_size);
        }

        // Simulate the outgoing rate
        store -= outgoing;
        if (store < 0) store = 0;  // Ensure the store doesn't go negative
        printf("After outgoing, %d packets left in the bucket out of %d\n", store, bucket_size);
    }

    return 0;
}
content_copyCOPY