Skip to content

Commit

Permalink
Merge pull request #1866 from khwaishchawla/main
Browse files Browse the repository at this point in the history
two level scheduling
  • Loading branch information
pankaj-bind authored Nov 10, 2024
2 parents d3dc9a1 + f265b75 commit e19e888
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Miscellaneous Algorithms/two level scheduling/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
43 changes: 43 additions & 0 deletions Miscellaneous Algorithms/two level scheduling/readme.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit e19e888

Please sign in to comment.