Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.0 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int process_id;
    int arrival_time;
    int service_time;
} Process;

void roundRobin(Process processes[], int n);
void shortestProcessNext(Process processes[], int n);

int main() {
    int n = 0;
    Process processes[50];

    while (1) {
        printf("Input P (%d) (arrival time, service time): ", n);
        scanf("%d", &processes[n].arrival_time);

        if (processes[n].arrival_time == -1) {
            break;
        }

        scanf("%d", &processes[n].service_time);
        processes[n].process_id = n;
        n++;
    }

    roundRobin(processes, n);
    shortestProcessNext(processes, n);

    return 0;
}

void roundRobin(Process processes[], int n) {
    int wait_time = 0;
    int turnaround_time = 0;
    int remaining_time[n];

    for (int i = 0; i < n; i++) {
        remaining_time[i] = processes[i].service_time;
    }

    int time = 0;
    int completed = 0;
    while (completed < n) {
        for (int i = 0; i < n; i++) {
            if (remaining_time[i] > 0) {
                if (remaining_time[i] > 1) {
                    time += 1;
                    remaining_time[i] -= 1;
                } else {
                    time += remaining_time[i];
                    wait_time += time - processes[i].arrival_time - processes[i].service_time;
                    turnaround_time += time - processes[i].arrival_time;

                    remaining_time[i] = 0;
                    completed++;
                }
            }
        }
    }

    double avg_wait_time = (double)wait_time / n;
    printf("CPU Scheduling using (RR) – Average waiting time: %.2f\n", avg_wait_time);
}

void shortestProcessNext(Process processes[], int n) {
    int wait_time = 0;
    int turnaround_time = 0;
    int remaining_time[n];

    for (int i = 0; i < n; i++) {
        remaining_time[i] = processes[i].service_time;
    }

    int time = 0;
    int completed = 0;
    while (completed < n) {
        int shortest_index = -1;
        int shortest_time = INT_MAX;

        for (int i = 0; i < n; i++) {
            if (remaining_time[i] > 0 && processes[i].arrival_time <= time) {
                if (remaining_time[i] < shortest_time) {
                    shortest_time = remaining_time[i];
                    shortest_index = i;
                }
            }
        }

        if (shortest_index == -1) {
            time++;
        } else {
            time += remaining_time[shortest_index];
            wait_time += time - processes[shortest_index].arrival_time - processes[shortest_index].service_time;
            turnaround_time += time - processes[shortest_index].arrival_time;

            remaining_time[shortest_index] = 0;
            completed++;
        }
    }

    double avg_wait_time = (double)wait_time / n;
    printf("CPU Scheduling using (SPN) – Average waiting time: %.2f\n", avg_wait_time);
}
Leave a Comment