Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment-10

119CS0159 | Roshan Kumar Sahu

Token Bucket
Code

#include<bits/stdc++.h>
using namespace std;

int main(){
int bucket_size;
cout<<"Enter Bucket Size:\t";
cin>>bucket_size;
int store=0;
int n;
cout<<"Enter no of Packets to be sent:\t";
cin>>n;
vector<int> packets(n);
vector<int> times(n);
for(int i=0;i<n;i++){
cout<<"Enter the packet size of "<<i+1<<"th packet:\t";
cin>>packets[i];
cout<<"Enter Time of Arrival:\t";
cin>>times[i];
}
int time=1;
int i=0;
while(i<n){
if(store!=bucket_size){
store++;
cout<<"Token Added. Bucket Size is:\t"<<store<<"\n";
}else{
cout<<"Bucket is Full. Cannot add Packets. Bucket Size is:\t"<<bucket_size<<"\n";
}
if(store>=packets[i] && time>=times[i]){
store-=packets[i];
cout<<packets[i]<<" is Sent. Bucket Size:\t"<<store<<'\n';
i++;
}
if(packets[i]>bucket_size){
cout<<packets[i]<<" Dropped. Since packet size is greater than bucket size.\n";
i++;
}
time++;
}
}

Output

Assignment-10 1
Assignment-10 2
Q2

#include<bits/stdc++.h>
using namespace std;

int main(){
int incoming, outgoing, buck_size, n, store = 0;
cout<<"Enter bucket size:\t";
cin>>buck_size;

cout<<"Enter outgoing rate:\t";


cin>>outgoing;
cout<<"Enter no of inputs:\t";
cin>>n;

while (n != 0) {
cout<<"Enter the incoming packet size : ";
cin>>incoming;
cout<<"Incoming packet size: "<<incoming<<"\n";
if (incoming <= (buck_size - store)){
store += incoming;
cout<<"Bucket buffer size "<<store<<" out of "<<buck_size<<"\n";
} else {
cout<<"Dropped "<<incoming - (buck_size - store)<<" no of packets\n";
cout<<"Bucket buffer size "<<store<<" out of "<<buck_size<<"\n";
store = buck_size;
}
store = store - outgoing;
cout<<"After outgoing "<<outgoing<<", "<< store <<" packets left out of "<<buck_size<<" in buffer\n";
n--;
}
}

Output

Assignment-10 3
Assignment-10 4

You might also like