You are on page 1of 10

Aditya kathpalia

A2345916002
B.tech. CSE-EVE, semester-6
Data Communication and Computer Networks
Assignment 6

Write a C Program to implement pure Aloha and slotted Aloha.


Explain the above two Aloha techniques with an example.

1. Algorithms
1.1 Pure ALOHA protocol:

● In pure ALOHA, the stations transmit frames whenever they have data to send.
● When two or more stations transmit simultaneously, there is collision and the frames are
destroyed.
● In pure ALOHA, whenever any station transmits a frame, it expects the acknowledgement
from the receiver.
● If acknowledgement is not received within specified time, the station assumes that the frame
(or acknowledgement) has been destroyed.
● If the frame is destroyed because of collision the station waits for a random amount of time
and sends it again. This waiting time must be random otherwise same frames will collide
again and again.

1.2 Slotted ALOHA protocol:

● Slotted ALOHA was invented to improve the efficiency of pure ALOHA as chances of
collision in pure ALOHA are very high.
● In slotted ALOHA, the time of the shared channel is divided into discrete intervals called
slots.
● The stations can send a frame only at the beginning of the slot and only one frame is sent in
each slot.

● In slotted ALOHA, if any station is not able to place the frame onto the channel at the
beginning of the slot i.e. it misses the time slot then the station has to wait until the
beginning of the next time slot.
● In slotted ALOHA, there is still a possibility of collision if two stations try to send at the
beginning of the same time slot
2. Examples:

2.1lotted ALOHA protocol:

3. C implementation

3.1 Pure ALOHA Protocol


Program:

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <dos.h>

#define FRAME_TIME 250

main()
{
float S1, S2, G, J, val[100];
int I, n, K, delay;
void wait();
clrscr();
printf("Please Give the Total Load : ");
scanf("%d", &n);
printf("Please Enter the value of load \n");
for (I=0; I<n; I++)
{
scanf("%f", &val[I]);
}
clrscr();
printf("\nOUTPUT 1: (THROUGHPUT Vs LOADCURVE)\n\n");
printf("s=g*exp(-G) FOR SLOTTED ALOHA * \n");
printf("s=g*exp(-2G) FOR PURE ALPHA #\n");
printf("\n------ (THROUGHPUT PER FRAME TIME)----\n");
for(K=0; K<n; K++)
{
G=val[K];
S1 = G * exp (-G);
S2 = G * exp(-2 * G);
printf("%1.3f", G );
for (I=0; I <=S1*20; I++)
{
printf(" ");
}
printf("*");
for(I=S2*20; I<=S2*75; I++ )
{
printf(" ");
}
printf("#\n");
}
printf("G (ATTEMPTS PER PACKET TIME) \n\n");
wait();
getch();
clrscr();
printf("\nOUTPUT 2 (DELAY Vs THROUGHPUT) \n\n");
printf("\n-----(THOUGHPUT PER FRAME TIME)----\n");
for(K=0; K<n; K++)
{
G=val[K];
S1 = G * exp (-G);
printf("3");
for (I=0; I <=S1*2.7; I++)
{
printf(" ");
}
printf("*\n");
}
printf("\n");
printf("---- DELAY -----");
wait();
getch();
clrscr();
}

void wait()
{
sound(440);
delay(300);
nosound();
}

Output:
3.2 Slotted ALOHA Protocol:
Program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define ROW 25
#define COL 70
#define N 3

void igrid(char [][COL]);


void printgrid(char[][COL]);
void move (char [][COL], int, int*, int*, int*, float);

int main()
{
float p;
char grid [ROW][COL];
int count, ff1 = 0, ff2 =0, ff3=0;

rand();
printf(" The Slotted ALOHA Simulation\n\n");
printf(" This program simluates one instance of slotted ALOHA.\n");
printf(" There are 3 nodes, each with one packet to transmit.\n");
printf(" The user imputs the probability of transmission\n");
printf(" which is the same for all three nodes.\n");
printf(" After a node has sucessfully transmitted its one packet\n");
printf(" it will stop transmission and wait for the other nodes\n");
printf(" to transmit its one packet\n\n");

printf("\n\nEnter the probability from 0 to 1 for the packet transmission: ");


scanf("%f",&p);

igrid(grid);
printgrid(grid);
for (count=1; count<10; count++)
move (grid, count, &ff1,&ff2, &ff3,p);

void igrid (char grid[][COL])


{
int i, j ;

for (i = 0; i <ROW; i++)


for (j= 0; j<COL;j++)
grid[i][j]= ' ';

for (i=3; i<ROW-4; i++)


grid[i][7] = '|';
for (j= 7; j<COL; j++)
grid[20][j] = '_';
for (j = 1; j<10; j ++)
grid [20][j*7] = '|';
for (i = 1; i <4; i++){
grid [i *5][1] = 'N';
grid [i *5][2] = 'o';
grid [i *5][3] = 'd';
grid [i *5][4] = 'e';
}
grid [5][6] ='1';
grid [10][6] ='2';
grid [15][6] ='3';

for (i = 0; i<3; i++)


for (j = 1; j<6; j++){
grid [(i*5)+6][j] = '*';
grid [(i*5)+7][j] = '*';
grid [(i*5)+8][j] = '*';
} }

void printgrid(char grid[][COL])


{
int i,j;
for(i=0; i<ROW; i++)
{ for( j=0; j<COL; j++)
printf("%c", grid[i][j]);
printf("\n");
}
}

void move (char grid[][COL], int count1, int *ff1,int *ff2, int*ff3, float p1)
{
float prob;

int j, f1 = 0, f2 = 0, f3 = 0,
n1 = 0, n2 =0 , n3= 0, newp=0;

prob = p1 * 100;
newp = ceil(prob);

n1 = rand();
n2 = rand();
n3 = rand();

if (n1 < newp && *ff1 == 0)


f1 = 1;
else
f1 = 0;

if (n2 < newp && *ff2 == 0)


f2 = 2;
else
f2 = 0;

if(n3 < newp && *ff3 == 0)


f3 = 3;
else
f3 = 0;

if (f1==1)
for (j = 1; j<6; j++){
grid [6][j+1+(7* count1)] = '*';
grid [7][j+1+(7* count1)] = '*';
grid [8][j+1+(7* count1)] = '*';
}

if (f2==2)
for (j = 1; j<6; j++){
grid [11][j+1+(7* count1)] = '*';
grid [12][j+1+(7* count1)] = '*';
grid [13][j+1+(7* count1)] = '*';
}
if (f3==3)
for (j = 1; j<6; j++){
grid [16][j+1+(7* count1)] = '*';
grid [17][j+1+(7* count1)] = '*';
grid [18][j+1+(7* count1)] = '*';
}

if (f1==1 && f2==0 && f3==0){


*ff1 = 1;
grid [22][3 + (7*count1)] = 'S';
}

if (f1==0 && f2==2 && f3==0){


*ff2 = 1;
grid [22][3 + (7*count1)] = 'S';
}

if (f1==0 &&
f2==0 && f3==3){
*ff3 = 1;
grid
[22][3 + (7*count1)] =
'S';
}

if (f1==0 &&
f2==0 && f3==0)
grid
[22][3 + (7*count1)] =
'E';
if (f1==0 &&
f2==2 && f3==3)
grid
[22][3 + (7*count1)] =
'C';
if (f1==1 &&
f2==0 && f3==3)
grid
[22][3 + (7*count1)] =
'C';
if (f1==1 &&
f2==2 && f3==0)
grid
[22][3 + (7*count1)] =
'C';
if (f1==1 &&
f2==2 && f3==3)
grid
[22][3 + (7*count1)] =
'C';
printgrid(grid);
}

Output:

You might also like