Skip to content

Commit e19e888

Browse files
authored
Merge pull request #1866 from khwaishchawla/main
two level scheduling
2 parents d3dc9a1 + f265b75 commit e19e888

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Process {
5+
int pid;
6+
int arrival_time;
7+
int burst_time;
8+
int waiting_time;
9+
int turnaround_time;
10+
};
11+
12+
void calculateTimes(struct Process *processes, int n) {
13+
int total_waiting = 0, total_turnaround = 0;
14+
processes[0].waiting_time = 0;
15+
processes[0].turnaround_time = processes[0].burst_time;
16+
17+
for (int i = 1; i < n; i++) {
18+
processes[i].waiting_time = processes[i - 1].waiting_time + processes[i - 1].burst_time;
19+
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
20+
21+
total_waiting += processes[i].waiting_time;
22+
total_turnaround += processes[i].turnaround_time;
23+
}
24+
25+
printf("\nProcess ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
26+
for (int i = 0; i < n; i++) {
27+
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival_time,
28+
processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
29+
}
30+
printf("\nAverage Waiting Time: %.2f", (float)total_waiting / n);
31+
printf("\nAverage Turnaround Time: %.2f\n", (float)total_turnaround / n);
32+
}
33+
34+
void twoLevelScheduling(struct Process *processes, int n) {
35+
// Step 1: Sort processes by arrival time (Long-term scheduling)
36+
for (int i = 0; i < n - 1; i++) {
37+
for (int j = i + 1; j < n; j++) {
38+
if (processes[i].arrival_time > processes[j].arrival_time) {
39+
struct Process temp = processes[i];
40+
processes[i] = processes[j];
41+
processes[j] = temp;
42+
}
43+
}
44+
}
45+
46+
// Step 2: FCFS on sorted list (Short-term scheduling)
47+
calculateTimes(processes, n);
48+
}
49+
50+
int main() {
51+
int n;
52+
printf("Enter the number of processes: ");
53+
scanf("%d", &n);
54+
55+
struct Process processes[n];
56+
for (int i = 0; i < n; i++) {
57+
printf("Enter arrival time and burst time for process %d: ", i + 1);
58+
processes[i].pid = i + 1;
59+
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
60+
}
61+
62+
twoLevelScheduling(processes, n);
63+
return 0;
64+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
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.
2+
3+
### Description of Two-Level Scheduling
4+
5+
1. **First Level (Long-Term Scheduling):**
6+
- The long-term scheduler, also called the admission scheduler, determines which processes are admitted to the system for execution.
7+
- 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.
8+
- Long-term scheduling helps control the degree of multiprogramming (i.e., the number of concurrent processes in memory).
9+
10+
2. **Second Level (Short-Term Scheduling):**
11+
- The short-term scheduler, or CPU scheduler, operates on the processes that are already in memory.
12+
- It frequently selects one of these processes to execute on the CPU, switching between processes as needed to optimize CPU utilization and system responsiveness.
13+
- Short-term scheduling uses various algorithms (e.g., round-robin, priority scheduling) to decide which process gets CPU time next.
14+
15+
### Pros of Two-Level Scheduling
16+
17+
1. **Improved Resource Utilization:**
18+
- Long-term scheduling controls memory usage by limiting the number of concurrent processes, helping to reduce memory thrashing (frequent page swaps).
19+
20+
2. **Enhanced System Performance:**
21+
- By focusing on short-term scheduling among active processes, it achieves higher CPU utilization and responsiveness, especially beneficial for interactive applications.
22+
23+
3. **Reduced Overhead in Process Management:**
24+
- By admitting only a manageable number of processes to memory, the system reduces overhead in handling context switching and managing process queues.
25+
26+
4. **Flexibility with Different Process Types:**
27+
- Works well in systems with mixed workloads by allowing batch jobs to run efficiently in the background and giving priority to interactive tasks.
28+
29+
### Cons of Two-Level Scheduling
30+
31+
1. **Increased Complexity:**
32+
- Requires careful design and tuning to manage the two levels of scheduling effectively, adding to the complexity of the operating system.
33+
34+
2. **Potential Latency for Batch Processes:**
35+
- Long-term scheduling may delay certain processes (especially lower-priority or batch jobs), affecting the turnaround time for such tasks.
36+
37+
3. **Memory Management Challenges:**
38+
- Determining the right mix of processes admitted to memory requires sophisticated memory management, and poor decisions may lead to inefficient memory usage.
39+
40+
4. **Higher Initial Overhead:**
41+
- The process of admitting and scheduling processes at two levels can introduce some initial overhead, potentially slowing down system responsiveness under heavy load conditions.
42+
43+
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 commit comments

Comments
 (0)