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

proportional scheduling #1822

Merged
merged 12 commits into from
Nov 10, 2024
66 changes: 66 additions & 0 deletions Miscellaneous Algorithms/proportional scheduling/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define NUM_PROCESSES 5

// Structure for process details
struct Process {
int pid; // Process ID
int burstTime; // Burst time of the process
int group; // Group ID (for fair share)
};

// Function to simulate fair share scheduling
void fairShareScheduling(struct Process processes[], int numProcesses) {
int timeQuantum = 2; // Time quantum for each round
int timeElapsed = 0;

// Keep track of remaining burst times
int remainingBurst[numProcesses];
for (int i = 0; i < numProcesses; i++) {
remainingBurst[i] = processes[i].burstTime;
}

printf("Starting Fair Share Scheduling...\n");

// Continue until all processes are done
while (1) {
int allDone = 1;

for (int i = 0; i < numProcesses; i++) {
if (remainingBurst[i] > 0) {
allDone = 0;

// Execute for time quantum or remaining burst time
int execTime = (remainingBurst[i] > timeQuantum) ? timeQuantum : remainingBurst[i];
remainingBurst[i] -= execTime;
timeElapsed += execTime;

printf("Process %d (Group %d) ran for %d units\n", processes[i].pid, processes[i].group, execTime);

// If process finished
if (remainingBurst[i] == 0) {
printf("Process %d completed at time %d\n", processes[i].pid, timeElapsed);
}
}
}

// Check if all processes are done
if (allDone) break;
}
}

int main() {
struct Process processes[NUM_PROCESSES] = {
{1, 8, 1}, // Process ID, Burst Time, Group
{2, 4, 2},
{3, 9, 1},
{4, 5, 2},
{5, 7, 1}
};

fairShareScheduling(processes, NUM_PROCESSES);

return 0;
}
38 changes: 38 additions & 0 deletions Miscellaneous Algorithms/proportional scheduling/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Fair Share Scheduling is a scheduling strategy that distributes CPU time fairly among users or groups of users. Instead of focusing solely on individual processes, fair share scheduling ensures that each user or group gets a specified proportion of CPU time, helping prevent any single user or group from monopolizing resources.

Description
In a fair share scheduling system, the CPU is allocated based on user-defined shares or groups. Each group is given an equal or specified share of CPU resources. For example, if two users each have processes that need CPU time, the scheduler will ensure both users receive a fair amount of CPU time, regardless of how many processes each user has running.

The scheduler operates in rounds (usually called time slices or time quanta) and allocates CPU time to processes within each user group. If a user has more processes than another user, the time is divided among that user's processes accordingly. This way, fair share scheduling attempts to prevent cases where one user's processes consume an excessive amount of CPU time, starving other users.

Advantages (Pros)
Fairness Across Users or Groups: Fair share scheduling ensures that each user or group receives an equitable share of CPU time, promoting fairness in resource allocation.

Prevents Starvation: By ensuring each group gets a proportion of CPU time, this method prevents any user or process from monopolizing the CPU, reducing the chance of starvation for low-priority users or processes.

Customizable Resource Distribution: It can be configured to assign specific shares based on group importance, enabling priority allocation to certain users or critical processes.

Enhanced Multitasking: By distributing CPU time fairly, it improves the responsiveness of the system across multiple users and processes, which is beneficial for environments with diverse workloads.

Disadvantages (Cons)
Increased Complexity: Fair share scheduling can be more complex to implement compared to simpler algorithms like round-robin or first-come-first-served, as it requires managing and tracking groups and their allocated shares.

Overhead in Resource Tracking: The system must monitor the CPU time used by each group, adding overhead to maintain this information, which can slightly reduce efficiency.

Not Optimized for Real-Time Tasks: Fair share scheduling does not prioritize time-sensitive tasks, potentially leading to delays for high-priority processes if they are part of a lower-priority group.

Suboptimal Performance for Single-User Systems: In environments with only one user or where most resources are used by a single user, fair share scheduling may be unnecessary and could add unnecessary complexity.

Use Cases
Fair share scheduling is ideal in multi-user or multi-group environments such as:

Academic or Research Institutions: Where multiple researchers or students share computational resources.
Enterprise Environments: Where resources need to be equitably divided among departments or teams.
Shared Server Systems: Cloud environments or shared servers where multiple users or clients access limited computational resources.
Overall, fair share scheduling balances CPU usage among users or groups, making it well-suited for multi-user systems, but it may add complexity and be less efficient in simpler or single-user systems.






Loading