Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RES.c #1618

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

RES.c #1618

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Preemptive Scheduling Algorithm/RES.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>

#define MAX_PROCESSES 10

// Define a structure for processes
typedef struct {
int id;
int priority; // Lower number means higher priority
int burstTime;
int waitingTime;
int turnaroundTime;
} Process;

// Function to perform Ripple Effect Scheduling (RES)
void rippleEffectScheduling(Process processes[], int n) {
// Sort processes by priority (ascending order for higher priority first)
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].priority > processes[j].priority) {
Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}

// Initialize waiting time for the first process
processes[0].waitingTime = 0;

// Calculate waiting time for each process
for (int i = 1; i < n; i++) {
processes[i].waitingTime = processes[i - 1].waitingTime + processes[i - 1].burstTime;

// Ripple effect: give small wait reduction to next lower-priority process
if (i < n - 1 && processes[i].priority < processes[i + 1].priority) {
processes[i + 1].waitingTime -= processes[i + 1].priority - processes[i].priority;
if (processes[i + 1].waitingTime < 0) {
processes[i + 1].waitingTime = 0; // Avoid negative waiting time
}
}
}

// Calculate turnaround time for each process
for (int i = 0; i < n; i++) {
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
}
}

// Function to print process details
void printProcesses(Process processes[], int n) {
float totalWaitingTime = 0, totalTurnaroundTime = 0;

printf("ID\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id, processes[i].priority,
processes[i].burstTime, processes[i].waitingTime,
processes[i].turnaroundTime);
}

printf("\nAverage Waiting Time: %.2f\n", totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / n);
}

int main() {
Process processes[MAX_PROCESSES];
int n;

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

// Input details for each process
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter priority and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].priority, &processes[i].burstTime);
}

// Perform Ripple Effect Scheduling
rippleEffectScheduling(processes, n);

// Print process details and average times
printProcesses(processes, n);

return 0;
}
Loading