Skip to content

Commit

Permalink
Merge pull request #1708 from khwaishchawla/main
Browse files Browse the repository at this point in the history
multilevel queue cpu scheduling
  • Loading branch information
pankaj-bind authored Nov 6, 2024
2 parents 6496586 + 933d813 commit ce7bb15
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Miscellaneous Algorithms/multilevel_queue_scheduling/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>

#define MAX 10

typedef struct {
int pid; // Process ID
int burstTime; // Burst Time
int queue; // Queue Type: 1=System, 2=Interactive, 3=Batch
} Process;

// Function to sort processes based on queue priority
void sortProcessesByQueue(Process processes[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].queue > processes[j + 1].queue) {
Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
}

// Function to calculate waiting time and turn around time
void calculateTimes(Process processes[], int n) {
int waitingTime[MAX], turnAroundTime[MAX];
int totalWaitingTime = 0, totalTurnAroundTime = 0;
int completionTime = 0;

printf("\nProcess Execution Order:\n");

for (int i = 0; i < n; i++) {
waitingTime[i] = completionTime;
completionTime += processes[i].burstTime;
turnAroundTime[i] = waitingTime[i] + processes[i].burstTime;

totalWaitingTime += waitingTime[i];
totalTurnAroundTime += turnAroundTime[i];

printf("Process %d (Queue %d): Waiting Time = %d, Turnaround Time = %d\n",
processes[i].pid, processes[i].queue, waitingTime[i], turnAroundTime[i]);
}

printf("\nAverage Waiting Time = %.2f", (float)totalWaitingTime / n);
printf("\nAverage Turnaround Time = %.2f\n", (float)totalTurnAroundTime / n);
}

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

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

if (n > MAX) {
printf("Number of processes should be less than or equal to %d.\n", MAX);
return 1;
}

// Input process details
for (int i = 0; i < n; i++) {
printf("\nEnter details for Process %d:\n", i + 1);
printf("Process ID: ");
scanf("%d", &processes[i].pid);
printf("Burst Time: ");
scanf("%d", &processes[i].burstTime);
printf("Queue (1=System, 2=Interactive, 3=Batch): ");
scanf("%d", &processes[i].queue);

if (processes[i].queue < 1 || processes[i].queue > 3) {
printf("Invalid queue type. Please enter 1, 2, or 3.\n");
i--; // Retry current process input
}
}

// Sort processes by queue priority
sortProcessesByQueue(processes, n);

// Calculate and display times
calculateTimes(processes, n);

return 0;
}
41 changes: 41 additions & 0 deletions Miscellaneous Algorithms/multilevel_queue_scheduling/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Multilevel Queue Scheduling
Multilevel Queue Scheduling is a CPU scheduling approach in which processes are grouped into multiple queues based on certain characteristics, such as priority level, process type, or memory requirements. Each queue has its own scheduling algorithm, making this approach suitable for systems with processes that have distinct needs.

For example, in a system with interactive (foreground) and batch (background) processes, a Multilevel Queue Scheduler can prioritize interactive processes by placing them in a high-priority queue using Round Robin, while batch processes go to a lower-priority queue using First-Come, First-Served (FCFS). This ensures that different types of processes receive the appropriate scheduling treatment based on their queue.

How Multilevel Queue Scheduling Works
Queue Assignment: Processes are assigned to a specific queue based on predefined attributes such as priority or process type.
Independent Scheduling: Each queue has its own scheduling algorithm, chosen to suit the needs of processes in that queue.
Priority between Queues: Queues are ordered by priority, and the CPU scheduler serves higher-priority queues before lower-priority ones. For instance, a high-priority queue for interactive processes might run before a low-priority queue for batch jobs.
Advantages of Multilevel Queue Scheduling
Efficiency in Handling Different Process Types:

By assigning processes to queues based on characteristics, the scheduler can handle real-time, interactive, and batch processes more effectively. This results in optimized scheduling based on the process type.
Prioritization:

Critical or high-priority tasks can be given precedence over less critical tasks, leading to better performance for interactive or real-time applications.
Customizable Scheduling Algorithms:

Each queue can have its own scheduling policy (e.g., FCFS, Round Robin), which allows flexibility to meet the specific needs of different groups of processes.
Reduced Complexity within Queues:

Each queue handles a more uniform type of process, which simplifies scheduling decisions within individual queues.
Disadvantages of Multilevel Queue Scheduling
Rigid Queue Assignment:

Once a process is assigned to a specific queue, it cannot move to a different one, which can lead to inefficiencies if the process’s needs change over time.
Starvation:

Lower-priority queues can experience starvation if higher-priority queues consistently have processes waiting, leading to delayed execution of lower-priority tasks.
Limited Flexibility:

The strict separation of queues and the inability of processes to change queues make the approach less adaptable to dynamic workloads.
Inefficient CPU Utilization:

If a high-priority queue is empty, the CPU may remain idle instead of servicing processes in a lower-priority queue, which could impact overall CPU utilization.
Example Use Cases
Multilevel Queue Scheduling is often used in systems that require a clear distinction between interactive and batch processing, such as:

Operating Systems: Real-time, interactive, and background processes can each be scheduled based on their specific needs.
Embedded Systems: Some embedded systems categorize tasks based on response requirements, allowing real-time tasks to be processed with minimal delay.
Cloud Environments: Multi-tiered applications where user requests are processed quickly, and background jobs are scheduled with lower priority.

0 comments on commit ce7bb15

Please sign in to comment.