Sei sulla pagina 1di 12

Operating Systems

CS F372

CPU Scheduling

Biju K Raveendran
• Average sleep time is also used by the scheduler to
determine whether a given process should be
considered interactive or branch process
• Interactive process
– Dynamic priority <= 3 X static priority / 4 + 28
– Which is equivalent to Bonus – 5 >= static priority / 4 – 28
– static priority / 4 – 28 is called the interactive delta
• varies from -3 (highest priority) to +6 (lowest priority)
– It is far easier for a high priority process to become interactive
than low priority process
• Process with priority 100 is considered interactive if bonus >2
(when its average sleep time exceeds 200ms)
• Process with priority 139 is never considered interactive because
bonus value required is 11 which is not achievable

Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 2


• Conventional processes with higher static priorities get
larger slices of the CPU
– They should not completely lock out the processes
having lower static priority
• To avoid process starvation, when a process finishes its
time quantum, it can be replaced by a lower priority
process whose time quantum has not yet been
exhausted
• To achieve this, in Linux scheduler maintains 2 disjoint
set of runnable processes
– Active Processes
– Expired processes
Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 3
Reality
• Scheduler tries to boost the performance of interactive processes
– An active batch process finishes its time quantum always becomes
expired
– An active interactive process finishes its time quantum usually remains
active
• Scheduler refills its time quantum and leaves it in the set of active
processes
– However, scheduler moves an interactive process that finishes its time
quantum to expired process if
• The eldest expired process has already waited for long time
• An expired process has higher static priority (lower value) than the
interactive process
– The active processes will become empty & expired processes will get a
chance to run

Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 4


Scheduling Real-time Processes
• Every real time process is associated with a real time priority
ranging from 1 (highest) to 99 (lowest)
• Scheduler always favors a higher priority runnable process over a
lower priority one
• Real time processes are always considered active
• User can change the real-time priority of a process by
sched_setparam() and sched_setscheduler() system calls
• If several real time runnable processes have the same highest
priority, the scheduler chooses the process that occurs first in the
corresponding list of the local CPU’s run queue

Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 5


• A real time process is replaced by another only when
one of the following events occur
– Process is preempted by a higher real time priority
process
– Process performs blocking operation (TASK_
INTERRUPTABLE or TASK_UNINTERRUPTABLE)
– Process is stopped (TASK_STOPPED or TASK_TRACED)
– Process is killed (EXIT_ZOMBIE or EXIT_DEAD)
– Process voluntarily relinquishes the CPU by invoking
sched_yield() system call
– The process is Round Robin real time (SCHED_RR)
and it has exhausted its time quantum
Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 6
• The nice() and setpriority() system calls, when
applied to a RR real time process, do not change
the real time priority
– Changes the duration of the base time
quantum
• Duration of the base time quantum in RR does
not depend on real time priority
– Depends on the static priority of the process

Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 7


• The arrays field of the runqueue is an array consisting of
two prio_array_t structures
– Each data structure represents
• A set of runnable processes and includes 140 doubly linked
list heads (one list for each possible process priority),
• A priority bit map and
• A counter of the processes included in the set
– The active field of the runqueue structure points to
one of the two prio_array_t data structure in arrays
• Exchange of active and expired data structures is
achieved by exchanging the contents of the active and
expired fields of the runqueue
Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 8
List of Tasks Indexed According to Priorities

139 139
Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 9
• recalc_task_prio()
– Updates the dynamic priority of a process
– Updates the average sleep time of a process
• schedule()
– Selects a process from the runqueue list and assign CPU to it.
– Invoked directly or lazy (deferred) way
• Direct
– The current process is blocked right away because the resource it
needs is not available.
• Lazy invocation
– By setting the TIF_NEED_RESCHED flag of current process to 1
– Check on the value of this flag is made before resuming the
execution of a User Mode Process

Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 10


• Examples of lazy invocation
– When current process has used up its quantum time
(done by scheduler_tick() function)
– When a process is woken up and its priority is higher
than that of the current process (done by
try_to_wake_up() function)
– When a sched_setscheduler() system call is issued
• load_balance()
– Keeps the runqueues of a multiprocessor system
balanced

Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 11


Exercise
Process Nice Execution Sleep Real Time Sched policy
Time(ms) time(ms) Priority
A 8 50 0 - SCHED_OTHER
B -15 800 0 - SCHED_OTHER
C -5 600 - 30 SCHED_RR
D 0 100 - 30 SCHED_FCFS
• A Linux OS supporting O(1) Scheduler has this snapshot at time t. Assume
that the order of arrival of processes to the system is Process A to Process
D. Process A and B are Conventional processes and Process C and D are
Real Time Processes.
• Find Static Priority for all the processes.
• Find Quantum time for all the processes.
• Find Dynamic priority for all Conventional processes.
• Find the resultant schedule and represent it as Gantt chart
Monday, October 21, 2019 Biju K Raveendran @ BITS Pilani Goa 12

Potrebbero piacerti anche