21CS52 Program 3

3. Write a program for congestion control using leaky bucket algorithm.

import java.util.Scanner;
import java.lang.*;
public class LBucket {
 public static void main(String[] args) {
 int i;
 int a[] = new int[20];
 int buck_rem = 0, buck_cap = 4, rate = 3, sent, recive;
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the size of packets:");
 int n = sc.nextInt();
 System.out.println("Enter the number:");
 for (i = 1; i <= n; i++)
 a[i] = sc.nextInt();
 System.out.println("Clock \t Packet size \t accept \t sent \tremaining");
 for (i = 1; i <= n; i++) {
 if (a[i] != 0) {
 if (buck_rem + a[i] > buck_cap)
 recive = -1;
 else {
 recive = a[i];
buck_rem += a[i];
 }
 } else
 recive = 0;
 if (buck_rem != 0) {
 if (buck_rem < rate) {
 sent = buck_rem;
buck_rem = 0;
 } else {
 sent = rate;
buck_rem = buck_rem - rate;
 }
 } else
 sent = 0;
 if (recive == -1)
	 System.out.println(+i + " \t\t " + a[i] + " \t\t dropped \t " + sent + " \t\t " + buck_rem);
	 else
	 System.out.println(+i + "\t\t" + a[i] + "\t\t" +recive + " \t\t " + sent + " \t\t " + buck_rem);
	 }
	 }
	}

OUTPUT:

**********************OUTPUT 1*********************************


Enter the size of packets:
5
Enter the number:
2
4
1
5
3
Clock 	 Packet size 	 accept 	 sent 	remaining
1		        2		         2 		    2 		    0
2		        4		         4 		    3 		    1
3		        1		         1 		    2 		    0
4 		      5 		      dropped 	0 		    0
5		        3		         3 		    3 		    0



**********************OUTPUT 2*********************************


Enter the size of packets:
5
Enter the number:
1
0
1
2
0
Clock 	 Packet size 	 accept 	 sent 	remaining
1		         1		       1 		    1 		   0
2		         0		       0 		    0 		   0
3	           1		       1 		    1 		   0
4		         2		       2 		    2 		   0
5		         0		       0 		    0 		   0

Leave a Reply

Your email address will not be published. Required fields are marked *