Lecture 3 CPU Scheduling
Lecture 3 CPU Scheduling
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that
program.
When a process enters the ready queue, its PCB is linked onto the tail of
the queue. When the CPU is free, it is allocated to the process at the
head of the queue. The running process is then removed from the queue.
The code for FCFS scheduling is simple to write and understand.
0 24 27 30
P2 P3 P1
0 3 6 30
The real difficulty with the SJF algorithm is knowing the length of the next CPU
request. For long-term (job) scheduling in a batch system, we can use the
process time limit that a user specifies when he submits the job. SJF scheduling
is used frequently in long-term scheduling.
An SJF algorithm is simply a priority algorithm where the priority (p) is the
inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the
priority, and vice versa.
Note that we discuss scheduling in terms of high priority and low priority. Priorities
are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to
4,095. We assume that low numbers represent high priority.
A preemptive priority scheduling algorithm will preempt the CPU if the priority of
the newly arrived process is higher than the priority of the currently running
process.
A nonpreemptive priority scheduling algorithm will simply put the new process at
the head of the ready queue.
A small unit of time, called a time quantum or time slice, is defined. A time quantum
is generally from 10 to 100 milliseconds in length.
The CPU scheduler goes around the ready queue, allocating the CPU to each
process for a time interval of up to 1 time quantum.
The scheduler will then proceed to the next process in the ready queue. If the CPU
burst of the currently running process is longer than 1 time quantum, the timer
will go off and will cause an interrupt to the operating system.
A context switch will be executed, and the process will be put at the tail of the
ready queue. The CPU scheduler will then select the next process in the ready
queue.
Process P2 does not need 4 milliseconds, so it quits before its time quantum
expires. The CPU is then given to the next process, process P3. Once each
process has received 1 time quantum, the CPU is returned to process P1 for an
additional time quantum.
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Let’s calculate the average waiting time for this schedule. P1 waits for 6
milliseconds (10 - 4), P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds.
Thus, the average waiting time is 17/3 = 5.66 milliseconds.
The performance of the RR algorithm depends heavily on the size of the time
quantum. At one extreme, if the time quantum is extremely large, the RR policy is
the same as the FCFS policy. In contrast, if the time quantum is extremely small
(say, 1 millisecond), the RR approach can result in a large number of context
switches.
Thus, we want the time quantum to be large with respect to the context
switch time. In practice, most modern systems have time quanta ranging
from 10 to 100 milliseconds. The time required for a context switch is
typically less than 10 microseconds.