CPU
Scheduling
Practice Exercises
5.1 A CPU-scheduling algorithm determines an order for the execution of its
scheduled processes. Given n processes to be scheduled on one proces-
sor, how many different schedules are possible? Give a formula in terms
of n.
Answer:
n! (n factorial = n × n – 1 × n – 2 × ... × 2 × 1).
5.2 Explain the difference between preemptive and nonpreemptive schedul-
ing.
Answer:
Preemptive scheduling allows a process to be interrupted in the midst of
its execution, taking the CPU away and allocating it to another process.
Nonpreemptive scheduling ensures that a process relinquishes control
of the CPU only when it finishes with its current CPU burst.
5.3 Suppose that the following processes arrive for execution at the times
indicated. Each process will run for the amount of time listed. In answer-
ing the questions, use nonpreemptive scheduling, and base all decisions
on the information you have at the time the decision must be made.
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
a. What is the average turnaround time for these processes with the
FCFS scheduling algorithm?
b. What is the average turnaround time for these processes with the
SJF scheduling algorithm?
c. The SJF algorithm is supposed to improve performance, but notice
that we chose to run process P1 at time 0 because we did not know
115
, that two shorter processes would arrive soon. Compute what the
average turnaround time will be if the CPU is left idle for the first
1 unit and then SJF scheduling is used. Remember that processes
P1 and P2 are waiting during this idle time, so their waiting time
may increase. This algorithm could be known as future-knowledge
scheduling.
Answer:
a. 10.53
b. 9.53
c. 6.86
Remember that turnaround time is finishing time minus arrival time, so
you have to subtract the arrival times to compute the turnaround times.
FCFS is 11 if you forget to subtract arrival time.
5.4 Consider the following set of processes, with the length of the CPU burst
time given in milliseconds:
Process Burst Time Priority
P1 2 2
P2 1 1
P3 8 4
P4 4 2
P5 5 3
The processes are assumed to have arrived in the order P1, P2, P3, P4, P5,
all at time 0.
a. Draw four Gantt charts that illustrate the execution of these pro-
cesses using the following scheduling algorithms: FCFS, SJF, non-
preemptive priority (a larger priority number implies a higher
priority), and RR (quantum = 2).
b. What is the turnaround time of each process for each of the
scheduling algorithms in part a?
c. What is the waiting time of each process for each of these schedul-
ing algorithms?
d. Which of the algorithms results in the minimum average waiting
time (over all processes)?
Answer:
a. The four Gantt charts:
P P P3 3
P 4 P 5
1 2 2 4 5
0 2 3 2 3 11 11 15 15 20
Scheduling
Practice Exercises
5.1 A CPU-scheduling algorithm determines an order for the execution of its
scheduled processes. Given n processes to be scheduled on one proces-
sor, how many different schedules are possible? Give a formula in terms
of n.
Answer:
n! (n factorial = n × n – 1 × n – 2 × ... × 2 × 1).
5.2 Explain the difference between preemptive and nonpreemptive schedul-
ing.
Answer:
Preemptive scheduling allows a process to be interrupted in the midst of
its execution, taking the CPU away and allocating it to another process.
Nonpreemptive scheduling ensures that a process relinquishes control
of the CPU only when it finishes with its current CPU burst.
5.3 Suppose that the following processes arrive for execution at the times
indicated. Each process will run for the amount of time listed. In answer-
ing the questions, use nonpreemptive scheduling, and base all decisions
on the information you have at the time the decision must be made.
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
a. What is the average turnaround time for these processes with the
FCFS scheduling algorithm?
b. What is the average turnaround time for these processes with the
SJF scheduling algorithm?
c. The SJF algorithm is supposed to improve performance, but notice
that we chose to run process P1 at time 0 because we did not know
115
, that two shorter processes would arrive soon. Compute what the
average turnaround time will be if the CPU is left idle for the first
1 unit and then SJF scheduling is used. Remember that processes
P1 and P2 are waiting during this idle time, so their waiting time
may increase. This algorithm could be known as future-knowledge
scheduling.
Answer:
a. 10.53
b. 9.53
c. 6.86
Remember that turnaround time is finishing time minus arrival time, so
you have to subtract the arrival times to compute the turnaround times.
FCFS is 11 if you forget to subtract arrival time.
5.4 Consider the following set of processes, with the length of the CPU burst
time given in milliseconds:
Process Burst Time Priority
P1 2 2
P2 1 1
P3 8 4
P4 4 2
P5 5 3
The processes are assumed to have arrived in the order P1, P2, P3, P4, P5,
all at time 0.
a. Draw four Gantt charts that illustrate the execution of these pro-
cesses using the following scheduling algorithms: FCFS, SJF, non-
preemptive priority (a larger priority number implies a higher
priority), and RR (quantum = 2).
b. What is the turnaround time of each process for each of the
scheduling algorithms in part a?
c. What is the waiting time of each process for each of these schedul-
ing algorithms?
d. Which of the algorithms results in the minimum average waiting
time (over all processes)?
Answer:
a. The four Gantt charts:
P P P3 3
P 4 P 5
1 2 2 4 5
0 2 3 2 3 11 11 15 15 20