diff --git a/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c b/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c new file mode 100644 index 00000000..94a042a2 --- /dev/null +++ b/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c @@ -0,0 +1,84 @@ +#include +#include + +#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; +} diff --git a/Miscellaneous Algorithms/multilevel_queue_scheduling/readme.md b/Miscellaneous Algorithms/multilevel_queue_scheduling/readme.md new file mode 100644 index 00000000..65bb8165 --- /dev/null +++ b/Miscellaneous Algorithms/multilevel_queue_scheduling/readme.md @@ -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.