ggggggg

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define MAX_ATTEMPTS 5  // Maximum number of attempts to resend data on collision
// Function to simulate checking if the channel is idle
int isChannelIdle() {
    return rand() % 2;  // Randomly returns 1 (idle) or 0 (busy)
}
// Function to simulate collision detection
int detectCollision() {
    return rand() % 2;  // Randomly returns 1 (collision) or 0 (no collision)
}
// Function to simulate exponential backoff after collision
void backOff(int attempt) {
    int waitTime = (1 << attempt) – 1;  // Calculate wait time (2^attempt – 1)
    int delay = rand() % (waitTime + 1);
    printf(“Backing off for %d seconds…\n”, delay);
    sleep(delay);  // Wait for random time within backoff range
}
void csma_cdProtocol() {
    int attempt = 0;
    while (attempt < MAX_ATTEMPTS) {
        printf(“Attempt %d to send data.\n”, attempt + 1);
        // Step 1: Sense the channel
        if (isChannelIdle()) {
            printf(“Channel is idle. Starting data transmission…\n”);
            // Step 2: Start transmission and check for collisions
            if (detectCollision()) {
                printf(“Collision detected! Aborting transmission.\n”);
                attempt++;
                backOff(attempt);  // Backoff and wait before retrying
            } else {
                printf(“Data transmitted successfully.\n”);
                break;  // Exit if transmission is successful
            }
        } else {
            printf(“Channel is busy. Retrying…\n”);
            sleep(1);  // Wait before sensing the channel again
        }
    }
    if (attempt == MAX_ATTEMPTS) {
        printf(“Maximum attempts reached. Transmission failed.\n”);
    }
}
int main() {
    srand(time(0));  // Seed the random number generator
    printf(“CSMA/CD Protocol Simulation\n”);
    csma_cdProtocol();
    return 0;
}
// Write a C program to implement CSA/CD Protocol
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define MAX_ATTEMPTS 5  // Maximum number of attempts to resend data on collision
// Function to simulate checking if the channel is idle
int isChannelIdle() {
    return rand() % 2;  // Randomly returns 1 (idle) or 0 (busy)
}
// Function to simulate collision detection
int detectCollision() {
    return rand() % 2;  // Randomly returns 1 (collision) or 0 (no collision)
}
// Function to simulate exponential backoff after collision
void backOff(int attempt) {
    int waitTime = (1 << attempt) – 1;  // Calculate wait time (2^attempt – 1)
    int delay = rand() % (waitTime + 1);
    printf(“Backing off for %d seconds…\n”, delay);
    sleep(delay);  // Wait for random time within backoff range
}
void csma_cdProtocol() {
    int attempt = 0;
    while (attempt < MAX_ATTEMPTS) {
        printf(“Attempt %d to send data.\n”, attempt + 1);
        // Step 1: Sense the channel
        if (isChannelIdle()) {
            printf(“Channel is idle. Starting data transmission…\n”);
            // Step 2: Start transmission and check for collisions
            if (detectCollision()) {
                printf(“Collision detected! Aborting transmission.\n”);
                attempt++;
                backOff(attempt);  // Backoff and wait before retrying
            } else {
                printf(“Data transmitted successfully.\n”);
                break;  // Exit if transmission is successful
            }
        } else {
            printf(“Channel is busy. Retrying…\n”);
            sleep(1);  // Wait before sensing the channel again
        }
    }
    if (attempt == MAX_ATTEMPTS) {
        printf(“Maximum attempts reached. Transmission failed.\n”);
    }
}
int main() {
    srand(time(0));  // Seed the random number generator
    printf(“CSMA/CD Protocol Simulation\n”);
    csma_cdProtocol();
    return 0;
}
Scroll to Top