Preview:
FCFS:
#include<stdio.h>

// Process structure
struct process {
    int pid;      // Process ID
    int burst;    // Burst time
    int waiting;  // Waiting time
    int turnaround;// Turnaround time
};

// Function to calculate waiting and turnaround times for each process
void calculateTimes(struct process proc[], int n) {
    int total_waiting = 0, total_turnaround = 0;
   
    // Waiting time for first process is 0
    proc[0].waiting = 0;
    // Turnaround time for first process is its burst time
    proc[0].turnaround = proc[0].burst;

    // Calculate waiting and turnaround times for each process
    for (int i = 1; i < n; i++) {
        // Waiting time of current process = Turnaround time of previous process
        proc[i].waiting = proc[i - 1].waiting + proc[i - 1].burst;
        // Turnaround time of current process = Waiting time of current process + Burst time of current process
        proc[i].turnaround = proc[i].waiting + proc[i].burst;
    }
}

// Function to display the process details
void displayProcesses(struct process proc[], int n) {
    printf("Process ID\tBurst Time\tWaiting Time\tTurnaround Time\n");

    for (int i = 0; i < n; i++) {
        printf("%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].burst, proc[i].waiting, proc[i].turnaround);
    }
}

// Function to calculate average waiting and turnaround times
void calculateAverages(struct process proc[], int n, float *avg_waiting, float *avg_turnaround) {
    int total_waiting = 0, total_turnaround = 0;

    for (int i = 0; i < n; i++) {
        total_waiting += proc[i].waiting;
        total_turnaround += proc[i].turnaround;
    }

    *avg_waiting = (float)total_waiting / n;
    *avg_turnaround = (float)total_turnaround / n;
}

int main() {
    int n; // Number of processes
    printf("Enter the number of processes: ");
    scanf("%d", &n);

    struct process proc[n]; // Array of processes

    // Input process details
    printf("Enter the burst time for each process:\n");
    for (int i = 0; i < n; i++) {
        proc[i].pid = i + 1;
        printf("Process %d: ", i + 1);
        scanf("%d", &proc[i].burst);
    }

    // Calculate waiting and turnaround times
    calculateTimes(proc, n);

    // Display the process details
    displayProcesses(proc, n);

    // Calculate and display average waiting and turnaround times
    float avg_waiting, avg_turnaround;
    calculateAverages(proc, n, &avg_waiting, &avg_turnaround);
    printf("\nAverage Waiting Time: %.2f\n", avg_waiting);
    printf("Average Turnaround Time: %.2f\n", avg_turnaround);

    return 0;
}


SJF:

#include <stdio.h>

struct Process {
    int process_id;
    int arrival_time;
    int burst_time;
};

void sjf_scheduling(struct Process processes[], int n) {
    int completion_time[n];
    int waiting_time[n];
    int turnaround_time[n];

    // Sort processes based on arrival time
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (processes[i].arrival_time > processes[j].arrival_time) {
                // Swap processes
                struct Process temp = processes[i];
                processes[i] = processes[j];
                processes[j] = temp;
            }
        }
    }

    int current_time = 0;

    for (int i = 0; i < n; i++) {
        // Read process details from the keyboard
        printf("Enter details for process %d:\n", i + 1);
        printf("Process ID: ");
        scanf("%d", &processes[i].process_id);
        printf("Arrival Time: ");
        scanf("%d", &processes[i].arrival_time);
        printf("Burst Time: ");
        scanf("%d", &processes[i].burst_time);

        // If the process hasn't arrived yet, wait
        if (current_time < processes[i].arrival_time) {
            current_time = processes[i].arrival_time;
        }

        // Update completion time
        completion_time[i] = current_time + processes[i].burst_time;

        // Update waiting time
        waiting_time[i] = current_time - processes[i].arrival_time;

        // Update turnaround time
        turnaround_time[i] = waiting_time[i] + processes[i].burst_time;

        // Move to the next process
        current_time += processes[i].burst_time;
    }

    // Display results
    printf("\nProcess\tCompletion Time\tWaiting Time\tTurnaround Time\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t\t%d\t\t%d\t\t%d\n", processes[i].process_id, completion_time[i], waiting_time[i], turnaround_time[i]);
    }

    // Calculate and display averages
    int total_waiting_time = 0;
    int total_turnaround_time = 0;
    for (int i = 0; i < n; i++) {
        total_waiting_time += waiting_time[i];
        total_turnaround_time += turnaround_time[i];
    }

    float avg_waiting_time = (float)total_waiting_time / n;
    float avg_turnaround_time = (float)total_turnaround_time / n;

    printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time);
    printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
}

int main() {
    int n;

    printf("Enter the number of processes: ");
    scanf("%d", &n);

    struct Process processes[n];

    sjf_scheduling(processes, n);

    return 0;
}


Round Robin:
#include<stdio.h>  
    #include<stdlib.h>  
     
    void main()  
    {  
        // initlialize the variable name  
        int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];  
        float avg_wt, avg_tat;  
        printf(" Total number of process in the system: ");  
        scanf("%d", &NOP);  
        y = NOP; // Assign the number of process to variable y  
     
    // Use for loop to enter the details of the process like Arrival time and the Burst Time  
    for(i=0; i<NOP; i++)  
    {  
    printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);  
    printf(" Arrival time is: \t");  // Accept arrival time  
    scanf("%d", &at[i]);  
    printf(" \nBurst time is: \t"); // Accept the Burst time  
    scanf("%d", &bt[i]);  
    temp[i] = bt[i]; // store the burst time in temp array  
    }  
    // Accept the Time qunat  
    printf("Enter the Time Quantum for the process: \t");  
    scanf("%d", &quant);  
    // Display the process No, burst time, Turn Around Time and the waiting time  
    printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");  
    for(sum=0, i = 0; y!=0; )  
    {  
    if(temp[i] <= quant && temp[i] > 0) // define the conditions  
    {  
        sum = sum + temp[i];  
        temp[i] = 0;  
        count=1;  
        }    
        else if(temp[i] > 0)  
        {  
            temp[i] = temp[i] - quant;  
            sum = sum + quant;    
        }  
        if(temp[i]==0 && count==1)  
        {  
            y--; //decrement the process no.  
            printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);  
            wt = wt+sum-at[i]-bt[i];  
            tat = tat+sum-at[i];  
            count =0;    
        }  
        if(i==NOP-1)  
        {  
            i=0;  
        }  
        else if(at[i+1]<=sum)  
        {  
            i++;  
        }  
        else  
        {  
            i=0;  
        }  
    }  
    // represents the average waiting time and Turn Around time  
    avg_wt = wt * 1.0/NOP;  
    avg_tat = tat * 1.0/NOP;  
    printf("\n Average Turn Around Time: \t%f", avg_wt);  
    printf("\n Average Waiting Time: \t%f"
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter