From 77a906ddfe363bf6472f35ae46474d39a36adbc1 Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:46:54 +0530 Subject: [PATCH 1/3] Create program.c --- .../multilevel_queue_scheduling/program.c | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Miscellaneous Algorithms/multilevel_queue_scheduling/program.c diff --git a/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c b/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c new file mode 100644 index 00000000..312a5634 --- /dev/null +++ b/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c @@ -0,0 +1,89 @@ +#include +#include +#include +using namespace std; + +// Structure to represent a process +struct Process { + int pid; // Process ID + int burstTime; // Burst time + int priority; // Priority (1 = high, 2 = low) + int arrivalTime; // Arrival time for Round Robin +}; + +// Function for FCFS Scheduling for high-priority queue +void FCFS(queue &highPriorityQueue) { + cout << "\nExecuting High-Priority Queue (FCFS):\n"; + int time = 0; + while (!highPriorityQueue.empty()) { + Process p = highPriorityQueue.front(); + highPriorityQueue.pop(); + cout << "Process " << p.pid << " executed from time " << time << " to " << time + p.burstTime << endl; + time += p.burstTime; + } +} + +// Function for Round Robin Scheduling for low-priority queue +void RoundRobin(queue &lowPriorityQueue, int timeQuantum) { + cout << "\nExecuting Low-Priority Queue (Round Robin):\n"; + int time = 0; + queue readyQueue = lowPriorityQueue; + + while (!readyQueue.empty()) { + Process p = readyQueue.front(); + readyQueue.pop(); + + if (p.burstTime > timeQuantum) { + cout << "Process " << p.pid << " executed from time " << time << " to " << time + timeQuantum << endl; + time += timeQuantum; + p.burstTime -= timeQuantum; + readyQueue.push(p); // Put back into queue if not finished + } else { + cout << "Process " << p.pid << " executed from time " << time << " to " << time + p.burstTime << endl; + time += p.burstTime; + } + } +} + +int main() { + int n, timeQuantum; + cout << "Enter the number of processes: "; + cin >> n; + + vector processes(n); + + // Taking process information from the user + for (int i = 0; i < n; ++i) { + cout << "\nEnter details for Process " << i + 1 << ":\n"; + processes[i].pid = i + 1; // Automatically assigning process ID + cout << "Burst Time: "; + cin >> processes[i].burstTime; + cout << "Priority (1 = high, 2 = low): "; + cin >> processes[i].priority; + cout << "Arrival Time: "; + cin >> processes[i].arrivalTime; + } + + cout << "\nEnter the time quantum for Round Robin: "; + cin >> timeQuantum; + + queue highPriorityQueue; + queue lowPriorityQueue; + + // Sort processes into queues based on priority + for (auto &process : processes) { + if (process.priority == 1) { + highPriorityQueue.push(process); + } else { + lowPriorityQueue.push(process); + } + } + + // Execute high-priority queue with FCFS + FCFS(highPriorityQueue); + + // Execute low-priority queue with Round Robin + RoundRobin(lowPriorityQueue, timeQuantum); + + return 0; +} From 377af87e1836150a62d34f0fb11d0b6b9b93e95c Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:49:19 +0530 Subject: [PATCH 2/3] Create readme.md --- .../multilevel_queue_scheduling/readme.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Miscellaneous Algorithms/multilevel_queue_scheduling/readme.md 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. From 933d813ae7954a9cd5ca6fb27b794d73b69f1d88 Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:49:55 +0530 Subject: [PATCH 3/3] Update program.c --- .../multilevel_queue_scheduling/program.c | 139 +++++++++--------- 1 file changed, 67 insertions(+), 72 deletions(-) diff --git a/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c b/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c index 312a5634..94a042a2 100644 --- a/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c +++ b/Miscellaneous Algorithms/multilevel_queue_scheduling/program.c @@ -1,89 +1,84 @@ -#include -#include -#include -using namespace std; - -// Structure to represent a process -struct Process { - int pid; // Process ID - int burstTime; // Burst time - int priority; // Priority (1 = high, 2 = low) - int arrivalTime; // Arrival time for Round Robin -}; - -// Function for FCFS Scheduling for high-priority queue -void FCFS(queue &highPriorityQueue) { - cout << "\nExecuting High-Priority Queue (FCFS):\n"; - int time = 0; - while (!highPriorityQueue.empty()) { - Process p = highPriorityQueue.front(); - highPriorityQueue.pop(); - cout << "Process " << p.pid << " executed from time " << time << " to " << time + p.burstTime << endl; - time += p.burstTime; +#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 for Round Robin Scheduling for low-priority queue -void RoundRobin(queue &lowPriorityQueue, int timeQuantum) { - cout << "\nExecuting Low-Priority Queue (Round Robin):\n"; - int time = 0; - queue readyQueue = lowPriorityQueue; - - while (!readyQueue.empty()) { - Process p = readyQueue.front(); - readyQueue.pop(); - - if (p.burstTime > timeQuantum) { - cout << "Process " << p.pid << " executed from time " << time << " to " << time + timeQuantum << endl; - time += timeQuantum; - p.burstTime -= timeQuantum; - readyQueue.push(p); // Put back into queue if not finished - } else { - cout << "Process " << p.pid << " executed from time " << time << " to " << time + p.burstTime << endl; - time += p.burstTime; - } +// 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() { - int n, timeQuantum; - cout << "Enter the number of processes: "; - cin >> n; - - vector processes(n); - - // Taking process information from the user - for (int i = 0; i < n; ++i) { - cout << "\nEnter details for Process " << i + 1 << ":\n"; - processes[i].pid = i + 1; // Automatically assigning process ID - cout << "Burst Time: "; - cin >> processes[i].burstTime; - cout << "Priority (1 = high, 2 = low): "; - cin >> processes[i].priority; - cout << "Arrival Time: "; - cin >> processes[i].arrivalTime; - } + Process processes[MAX]; + int n; + + printf("Enter the number of processes (max %d): ", MAX); + scanf("%d", &n); - cout << "\nEnter the time quantum for Round Robin: "; - cin >> timeQuantum; + if (n > MAX) { + printf("Number of processes should be less than or equal to %d.\n", MAX); + return 1; + } - queue highPriorityQueue; - queue lowPriorityQueue; + // 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); - // Sort processes into queues based on priority - for (auto &process : processes) { - if (process.priority == 1) { - highPriorityQueue.push(process); - } else { - lowPriorityQueue.push(process); + 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 } } - // Execute high-priority queue with FCFS - FCFS(highPriorityQueue); + // Sort processes by queue priority + sortProcessesByQueue(processes, n); - // Execute low-priority queue with Round Robin - RoundRobin(lowPriorityQueue, timeQuantum); + // Calculate and display times + calculateTimes(processes, n); return 0; }