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

Show file tree
Hide file tree
Changes from 2 commits
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;
}
66 changes: 66 additions & 0 deletions Preemptive Scheduling Algorithm/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Ripple Effect Scheduling (RES) Algorithm in C

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.

## Table of Contents

1. [Overview](#overview)
2. [Algorithm Details](#algorithm-details)
3. [Code Structure](#code-structure)
4. [How to Use](#how-to-use)
5. [Sample Input/Output](#sample-inputoutput)

## Overview

The Ripple Effect Scheduling (RES) algorithm is a **priority-based CPU scheduling algorithm** that aims to:
- Prioritize tasks based on their priority level (where a lower priority number indicates higher priority).
- 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.

This approach ensures that high-priority tasks are executed first but avoids causing excessive delays for lower-priority tasks.

## Algorithm Details

The algorithm involves the following steps:

1. **Sorting by Priority**: Processes are sorted by priority (ascending order), where a lower priority number indicates a higher priority.
2. **Waiting Time Calculation**:
- For each process, calculate the waiting time as the cumulative burst time of previous processes.
- 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.
3. **Turnaround Time Calculation**:
- For each process, calculate the turnaround time as the sum of waiting time and burst time.
4. **Average Waiting and Turnaround Times**: Display the average waiting and turnaround times for all processes.

## Code Structure

- **`Process` struct**: Stores the details for each process, including `id`, `priority`, `burstTime`, `waitingTime`, and `turnaroundTime`.
- **`rippleEffectScheduling()`**: Sorts processes by priority and calculates waiting and turnaround times, applying the ripple effect.
- **`printProcesses()`**: Displays the process details in a tabular format and calculates average waiting and turnaround times.
- **`main()`**: Collects user input for each process and invokes the scheduling functions.

## How to Use

1. Clone or download this repository to your local machine.
2. Compile the code using a C compiler (instructions below).
3. Run the executable and input the number of processes.
4. For each process, enter the priority and burst time when prompted.

The output will display the waiting time and turnaround time for each process, along with the average waiting and turnaround times.

## Sample Input/Output

### Sample Input

Enter number of processes: 3
Enter priority and burst time for process 1: 1 5
Enter priority and burst time for process 2: 2 8
Enter priority and burst time for process 3: 1 2

### Sample Output

ID Priority Burst Time Waiting Time Turnaround Time
1 1 5 0 5
3 1 2 5 7
2 2 8 4 12

Average Waiting Time: 3.00
Average Turnaround Time: 8.00
Loading