4. Experiment
Aim: Write a program to implement Round Robin CPU scheduling algorithm.
Theory:
Round Robin (RR) is a preemptive CPU scheduling algorithm designed for time-sharing systems.
Each process is given a small time quantum or time slice.
The CPU executes a process for this fixed amount of time.
After the time quantum expires, the process is preempted and put back at the rear of the ready queue, allowing the next process to execute.
The CPU cycles through the ready queue in a circular fashion.
Advantages:
Fair and simple: Each process gets an equal share of CPU.
Preemptive: Allows responsive multi-user, interactive systems.
Precludes starvation: Every process eventually gets CPU time.
Suitable for time-sharing: Better for interactive computing.
Disadvantages:
Higher context switching: Small time quanta result in frequent switches.
Lower CPU utilization: Large context switches can affect CPU performance.
Average waiting and turnaround may be higher than other algorithms.
Determining the optimum time quantum is challenging: Too small or large can affect performance.
Flowchart
Algorithm
Step 1: Start
Step 2: Input number of processes (n) and time quantum.
Step 3: Loop from 1 to n:
Input Arrival Time and Burst Time for each process.
Step 4: Push all processes into a queue.
Step 5: current_time = 0
Step 6: Loop until all processes are finished:
- Dequeue first process.
- If remaining burst <= time quantum:
execute it fully, update waiting, turnaround.
- Else:
execute it for time quantum, reduce its remaining,
and put it back into the queue.
Step 7: Display all process details.
Step 8: End
Source Code :
// C++ CODE Round Robin CPU Scheduling with number of processes given by the user.
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int id, at, bt, rt, ct, tat, wt;
};
int main()
{
int n, quantum;
cout << "Enter number of processes: "; cin >> n;
cout << "Enter time quantum: "; cin >> quantum;
Process p[n];
for(int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Enter Arrival Time for P" << i + 1 << ": "; cin >> p[i].at;
cout << "Enter Burst Time for P" << i + 1 << ": "; cin >> p[i].bt;
p[i].rt = p[i].bt;
}
int completed = 0, current = 0;
queue<int>q;
int i = 0;
q.push(0);
i = 1;
while (completed < n) {
int idx = q.front();
q.pop();
if (p[idx].rt <= quantum) {
current = max(current, p[idx].at);
current += p[idx].rt;
p[idx].rt = 0;
p[idx].ct = current;
p[idx].tat = p[idx].ct - p[idx].at;
p[idx].wt = p[idx].tat - p[idx].bt;
completed++;
}
else {
current = max(current, p[idx].at);
current += quantum;
p[idx].rt -= quantum;
}
for (; i < n && p[i].at <= current; i++)
q.push(i);
if (p[idx].rt > 0)
q.push(idx);
else if (q.empty() && i < n)
q.push(i++);
}
cout << "PID\tAT\tBT\tWT\tTAT\n";
for(int i = 0; i < n; i++) {
cout << p[i].id << '\t' << p[i].at << '\t'<< p[i].bt << '\t'<< p[i].wt<< '\t'<< p[i].tat << '\n';
}
}
MCQ on Priority CPU Scheduling:
1. Round Robin is a:
a) Non-preemptive algorithm
b) Preemptive algorithm
c) Priority algorithm
d) Shortest first algorithm
➥ Answer: b) Preemptive algorithm
2. The main data structure used in RR is:
a) Stack
b) Priority queue
c) FIFO queue
d) Tree
➥ Answer: c) FIFO queue
3. Time allotted to each process in RR is called:
a) Arrival time
b) Burst time
c) Time quantum
d) Waiting time
➥ Answer: c) Time quantum
4. If the time quantum is large, RR performs like:
a) Priority
b) Shortest job first
c) First come first served
d) Multilevel queue
➥ Answer: c) First come first served
5. If the time quantum is small:
a) CPU utilization increases
b) Context switching increases
c) Throughput drops
d) Waiting drops
➥ Answer: b) Context switching increases
6. The main objective of Round Robin is:
a) To minimize waiting time
b) To maximize CPU utilization
c) To implement fairness in CPU allocation
d) To avoid context switches
➥ Answer: c) To implement fairness in CPU allocation
7. If a process finishes before its time quantum expires, then:
a) CPU immediately switches to next process
b) It waits for the quantum to expire
c) It is preempted forcibly
d) It restarts from the beginning
➥ Answer: a) CPU immediately switches to next process
8. Small time quanta lead to:
a) Higher throughput
b) Lower waiting time
c) Higher context switching
d) Lower CPU utilization
➥ Answer: c) Higher context switching
9. Large time quanta cause:
a) Better fairness
b) Lower context switches
c) Higher waiting time
d) Less CPU utilization
➥ Answer: b) Lower context switches
10. What kind of systems typically use round robin?
a) Batch processing
b) Real-time control
c) Time-sharing
d) File storage
➥ Answer: c) Time-sharing
11. If we ignore context switching, average waiting under RR:
a) Is minimum
b) Is medium
c) May be large
d) Equals to FIFO
➥ Answer: c) May be large
12. The main consideration in choosing the time quantum is:
a) Number of processors
b) CPU clock speed
c) Average process length
d) Number of I/O requests
➥ Answer: c) Average process length
13. If the quantum is tiny:
a) Few context switches
b) Large context switches
c) Higher throughput
d) Better CPU utilization
➥ Answer: b) Large context switches
14. If the quantum is huge:
a) It degenerates into FIFO
b) It reduces context switching
c) It guarantees fairness
d) It drops CPU utilization
➥ Answer: a) It degenerates into FIFO
15. The main advantage of RR is:
a) Low waiting time for all
b) Fair CPU allocation
c) Higher throughput
d) Few context switches
➥ Answer: b) Fair CPU allocation