From 256515db486e8891f91ca54d02078cfa9fc9007a Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Sun, 10 Nov 2024 21:37:55 +0530 Subject: [PATCH 1/3] Create program.c --- .../two level scheduling/program.cc | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Miscellaneous Algorithms/two level scheduling/program.cc diff --git a/Miscellaneous Algorithms/two level scheduling/program.cc b/Miscellaneous Algorithms/two level scheduling/program.cc new file mode 100644 index 00000000..ea294710 --- /dev/null +++ b/Miscellaneous Algorithms/two level scheduling/program.cc @@ -0,0 +1,64 @@ +#include +#include + +struct Process { + int pid; + int arrival_time; + int burst_time; + int waiting_time; + int turnaround_time; +}; + +void calculateTimes(struct Process *processes, int n) { + int total_waiting = 0, total_turnaround = 0; + processes[0].waiting_time = 0; + processes[0].turnaround_time = processes[0].burst_time; + + for (int i = 1; i < n; i++) { + processes[i].waiting_time = processes[i - 1].waiting_time + processes[i - 1].burst_time; + processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time; + + total_waiting += processes[i].waiting_time; + total_turnaround += processes[i].turnaround_time; + } + + printf("\nProcess ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n"); + for (int i = 0; i < n; i++) { + printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival_time, + processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time); + } + printf("\nAverage Waiting Time: %.2f", (float)total_waiting / n); + printf("\nAverage Turnaround Time: %.2f\n", (float)total_turnaround / n); +} + +void twoLevelScheduling(struct Process *processes, int n) { + // Step 1: Sort processes by arrival time (Long-term scheduling) + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + if (processes[i].arrival_time > processes[j].arrival_time) { + struct Process temp = processes[i]; + processes[i] = processes[j]; + processes[j] = temp; + } + } + } + + // Step 2: FCFS on sorted list (Short-term scheduling) + calculateTimes(processes, n); +} + +int main() { + int n; + printf("Enter the number of processes: "); + scanf("%d", &n); + + struct Process processes[n]; + for (int i = 0; i < n; i++) { + printf("Enter arrival time and burst time for process %d: ", i + 1); + processes[i].pid = i + 1; + scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time); + } + + twoLevelScheduling(processes, n); + return 0; +} From c7d1ad55cf266adb7d9932b41183555c14a3b11b Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Sun, 10 Nov 2024 21:39:48 +0530 Subject: [PATCH 2/3] Create readme.md --- .../two level scheduling/readme.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Miscellaneous Algorithms/two level scheduling/readme.md diff --git a/Miscellaneous Algorithms/two level scheduling/readme.md b/Miscellaneous Algorithms/two level scheduling/readme.md new file mode 100644 index 00000000..e4870a7a --- /dev/null +++ b/Miscellaneous Algorithms/two level scheduling/readme.md @@ -0,0 +1,43 @@ +Two-level scheduling is a strategy used in operating systems to manage processes efficiently. It divides the scheduling process into two distinct phases to balance resource usage and system responsiveness, particularly in systems with a mix of interactive and batch processes. + +### Description of Two-Level Scheduling + +1. **First Level (Long-Term Scheduling):** + - The long-term scheduler, also called the admission scheduler, determines which processes are admitted to the system for execution. + - It decides the overall mix of active processes in the system by selecting a subset from a larger pool of tasks. Only a portion of processes are allowed into memory based on system resource availability. + - Long-term scheduling helps control the degree of multiprogramming (i.e., the number of concurrent processes in memory). + +2. **Second Level (Short-Term Scheduling):** + - The short-term scheduler, or CPU scheduler, operates on the processes that are already in memory. + - It frequently selects one of these processes to execute on the CPU, switching between processes as needed to optimize CPU utilization and system responsiveness. + - Short-term scheduling uses various algorithms (e.g., round-robin, priority scheduling) to decide which process gets CPU time next. + +### Pros of Two-Level Scheduling + +1. **Improved Resource Utilization:** + - Long-term scheduling controls memory usage by limiting the number of concurrent processes, helping to reduce memory thrashing (frequent page swaps). + +2. **Enhanced System Performance:** + - By focusing on short-term scheduling among active processes, it achieves higher CPU utilization and responsiveness, especially beneficial for interactive applications. + +3. **Reduced Overhead in Process Management:** + - By admitting only a manageable number of processes to memory, the system reduces overhead in handling context switching and managing process queues. + +4. **Flexibility with Different Process Types:** + - Works well in systems with mixed workloads by allowing batch jobs to run efficiently in the background and giving priority to interactive tasks. + +### Cons of Two-Level Scheduling + +1. **Increased Complexity:** + - Requires careful design and tuning to manage the two levels of scheduling effectively, adding to the complexity of the operating system. + +2. **Potential Latency for Batch Processes:** + - Long-term scheduling may delay certain processes (especially lower-priority or batch jobs), affecting the turnaround time for such tasks. + +3. **Memory Management Challenges:** + - Determining the right mix of processes admitted to memory requires sophisticated memory management, and poor decisions may lead to inefficient memory usage. + +4. **Higher Initial Overhead:** + - The process of admitting and scheduling processes at two levels can introduce some initial overhead, potentially slowing down system responsiveness under heavy load conditions. + +Two-level scheduling is particularly beneficial for multi-user systems and real-time applications where maintaining responsiveness for interactive users is critical. However, the additional complexity requires careful management and may not be suitable for simpler or single-user systems. From f265b757fe7a8fb17bc3514329d5c2c4c8d9fec5 Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Sun, 10 Nov 2024 21:40:12 +0530 Subject: [PATCH 3/3] Rename program.cc to program.c --- .../two level scheduling/{program.cc => program.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Miscellaneous Algorithms/two level scheduling/{program.cc => program.c} (100%) diff --git a/Miscellaneous Algorithms/two level scheduling/program.cc b/Miscellaneous Algorithms/two level scheduling/program.c similarity index 100% rename from Miscellaneous Algorithms/two level scheduling/program.cc rename to Miscellaneous Algorithms/two level scheduling/program.c