Skip to content

Commit 36873e9

Browse files
authored
Merge pull request #1618 from AshmitaBarthwal/AshmitaBarthwal-patch-1
RES.c
2 parents 412418d + b999811 commit 36873e9

File tree

2 files changed

+159
-0
lines changed
  • Miscellaneous Algorithms/Ripple Effect Scheduling (RES) Algorithm

2 files changed

+159
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define MAX_PROCESSES 10
5+
6+
// Define a structure for processes
7+
typedef struct {
8+
int id;
9+
int priority; // Lower number means higher priority
10+
int burstTime;
11+
int waitingTime;
12+
int turnaroundTime;
13+
} Process;
14+
15+
// Function to perform Ripple Effect Scheduling (RES)
16+
void rippleEffectScheduling(Process processes[], int n) {
17+
// Sort processes by priority (ascending order for higher priority first)
18+
for (int i = 0; i < n - 1; i++) {
19+
for (int j = i + 1; j < n; j++) {
20+
if (processes[i].priority > processes[j].priority) {
21+
Process temp = processes[i];
22+
processes[i] = processes[j];
23+
processes[j] = temp;
24+
}
25+
}
26+
}
27+
28+
// Initialize waiting time for the first process
29+
processes[0].waitingTime = 0;
30+
31+
// Calculate waiting time for each process
32+
for (int i = 1; i < n; i++) {
33+
processes[i].waitingTime = processes[i - 1].waitingTime + processes[i - 1].burstTime;
34+
35+
// Ripple effect: give small wait reduction to next lower-priority process
36+
if (i < n - 1 && processes[i].priority < processes[i + 1].priority) {
37+
processes[i + 1].waitingTime -= processes[i + 1].priority - processes[i].priority;
38+
if (processes[i + 1].waitingTime < 0) {
39+
processes[i + 1].waitingTime = 0; // Avoid negative waiting time
40+
}
41+
}
42+
}
43+
44+
// Calculate turnaround time for each process
45+
for (int i = 0; i < n; i++) {
46+
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
47+
}
48+
}
49+
50+
// Function to print process details
51+
void printProcesses(Process processes[], int n) {
52+
float totalWaitingTime = 0, totalTurnaroundTime = 0;
53+
54+
printf("ID\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");
55+
for (int i = 0; i < n; i++) {
56+
totalWaitingTime += processes[i].waitingTime;
57+
totalTurnaroundTime += processes[i].turnaroundTime;
58+
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n",
59+
processes[i].id, processes[i].priority,
60+
processes[i].burstTime, processes[i].waitingTime,
61+
processes[i].turnaroundTime);
62+
}
63+
64+
printf("\nAverage Waiting Time: %.2f\n", totalWaitingTime / n);
65+
printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / n);
66+
}
67+
68+
int main() {
69+
Process processes[MAX_PROCESSES];
70+
int n;
71+
72+
printf("Enter number of processes: ");
73+
scanf("%d", &n);
74+
75+
// Input details for each process
76+
for (int i = 0; i < n; i++) {
77+
processes[i].id = i + 1;
78+
printf("Enter priority and burst time for process %d: ", i + 1);
79+
scanf("%d %d", &processes[i].priority, &processes[i].burstTime);
80+
}
81+
82+
// Perform Ripple Effect Scheduling
83+
rippleEffectScheduling(processes, n);
84+
85+
// Print process details and average times
86+
printProcesses(processes, n);
87+
88+
return 0;
89+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Ripple Effect Scheduling (RES) Algorithm in C
2+
3+
This project implements the **Ripple Effect Scheduling (RES)** algorithm in C, a priority-based scheduling algorithm that minimizes disruptions to lower-priority tasks. This algorithm sorts tasks based on their priority and calculates waiting and turnaround times for each process. Additionally, it applies a "ripple effect" adjustment to reduce the waiting time of lower-priority tasks.
4+
5+
## Table of Contents
6+
7+
1. [Overview](#overview)
8+
2. [Algorithm Details](#algorithm-details)
9+
3. [Code Structure](#code-structure)
10+
4. [How to Use](#how-to-use)
11+
5. [Sample Input/Output](#sample-inputoutput)
12+
6. [Compilation and Execution](#compilation-and-execution)
13+
7. [Contributing](#contributing)
14+
8. [License](#license)
15+
16+
## Overview
17+
18+
The Ripple Effect Scheduling (RES) algorithm is a **priority-based CPU scheduling algorithm** that aims to:
19+
- Prioritize tasks based on their priority level (where a lower priority number indicates higher priority).
20+
- Minimize disruptions to lower-priority tasks using a "ripple effect" adjustment, where each process slightly adjusts the waiting time of the next lower-priority process.
21+
22+
This approach ensures that high-priority tasks are executed first but avoids causing excessive delays for lower-priority tasks.
23+
24+
## Algorithm Details
25+
26+
The algorithm involves the following steps:
27+
28+
1. **Sorting by Priority**: Processes are sorted by priority (ascending order), where a lower priority number indicates a higher priority.
29+
2. **Waiting Time Calculation**:
30+
- For each process, calculate the waiting time as the cumulative burst time of previous processes.
31+
- Apply the "ripple effect" by reducing the waiting time of the next process slightly if it has a lower priority, ensuring it doesn't fall below zero.
32+
3. **Turnaround Time Calculation**:
33+
- For each process, calculate the turnaround time as the sum of waiting time and burst time.
34+
4. **Average Waiting and Turnaround Times**: Display the average waiting and turnaround times for all processes.
35+
36+
## Code Structure
37+
38+
- **`Process` struct**: Stores the details for each process, including `id`, `priority`, `burstTime`, `waitingTime`, and `turnaroundTime`.
39+
- **`rippleEffectScheduling()`**: Sorts processes by priority and calculates waiting and turnaround times, applying the ripple effect.
40+
- **`printProcesses()`**: Displays the process details in a tabular format and calculates average waiting and turnaround times.
41+
- **`main()`**: Collects user input for each process and invokes the scheduling functions.
42+
43+
## How to Use
44+
45+
1. Clone or download this repository to your local machine.
46+
2. Compile the code using a C compiler (instructions below).
47+
3. Run the executable and input the number of processes.
48+
4. For each process, enter the priority and burst time when prompted.
49+
50+
The output will display the waiting time and turnaround time for each process, along with the average waiting and turnaround times.
51+
52+
## Sample Input/Output
53+
54+
### Sample Input
55+
56+
Enter number of processes: 3
57+
Enter priority and burst time for process 1: 1 5
58+
Enter priority and burst time for process 2: 2 8
59+
Enter priority and burst time for process 3: 1 2
60+
61+
### Sample Output
62+
63+
ID Priority Burst Time Waiting Time Turnaround Time
64+
1 1 5 0 5
65+
3 1 2 5 7
66+
2 2 8 4 12
67+
68+
Average Waiting Time: 3.00
69+
Average Turnaround Time: 8.00
70+

0 commit comments

Comments
 (0)