Program8.
Write a program for congestion control using the leaky bucket algorithm
#include<stdio.h>
int min(int x,int y)
{
if(x<y)
{
return x;
}
else
return y;
}
int main()
{
int drop=0,mini,nsec,cap,count=0;
int I,inp[25],process;
printf(“Enter the Bucket Size:\n”);
scanf(“%d”,&cap);
printf(“Enter the Processing Rate:\n”);
scanf(“%d”,&process);
printf(“Enter The No. of Seconds You Want To Stimulate:\n”);
scanf(“%d”,&nsec);
for(i=0;i<nsec;i++)
{
printf(“Enter the Size of the Packet Entering at %d sec:\n”,i+1);
scanf(“%d”,&inp[i]);
}
printf(“\nSecond|Packet Received|Packet Sent|Packet Left|Packet Dropped|\n”);
printf(“----------------------------------------------------------------------------\n”);
for(i=0;i<nsec;i++)
{
Count+=inp[i];
if(count>cap)
{
drop=count-cap;
count=cap;
}
printf(“%d”,i+1);
printf(“\t%d”,inp[i]);
mini=min(count,process);
printf(“\t\t\t\t%d”,mini);
printf(“\t\t\t\t%d\n”,drop);
drop=0;
}
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
printf(“%d”,i+1);
printf(“\t0”);
mini=min(count,process);
printf(“\t\t%d”,mini);
count=count-mini;
printf(“\t\t%d”,count);
printf(“\t\t%d\n”,drop);
}
}
Execution and Output:
$ cc 8.c
$ ./[Link]
Enter the Bucket Size: 5
Enter the Processing Rate: 2
Enter The No. Of Seconds You Want To Stimulate: 3
Enter the Size of the Packet Entering at 1 sec: 5
Enter the Size of the Packet Entering at 2 sec: 4
Enter the Size of the Packet Entering at 3 sec: 3
Second Packet Packet Sent Packet Left Packet
Received Dropped
1 5 2 3 0
2 4 2 3 2
3 3 2 3 1
4 0 2 1 0