CSC501 Operating SystemsPrinciples

Process Scheduling

1 Last Lecture

q Lifecycles q Context Switching

2 Process Scheduling q Key steps Q Examine processes eligible for execution Q Select one based on a certain scheduling policy Q Switch CPU to selected item q Two-level scheduling possible Q Select Process Q Select within Process

3 Scheduling Policy

q Fundamental OS policy q Determines when process selected for execution q May depend on Q Process priority Q Time process waits Q Process owner (user)

4 Process Scheduling Queues q Ready queue Q Set of processes residing in main memory, ready, and waiting to execute q Job queue Q Set of all processes in the system q Device queues Q Set of processes waiting for an I/O device q Process migration between the various queues

5 Ready Queue And Various I/O Device Queues Representation of Process Scheduling Schedulers q Long-term scheduler (or job scheduler) Q Which processes should be brought into the ready queue Q Invoked very infrequently (seconds, minutes) Þ (may be slow) Q Controls the degree of multiprogramming q Short-term scheduler (or CPU scheduler) Q Which process should execute next (allocates CPU) Q Invoked very frequently (milliseconds) Þ (must be fast)

8 Schedulers (Cont.) q Processes can be described as either: Q I/O-bound process v spends more time doing I/O than computations, v many short CPU bursts Q CPU-bound process v spends more time doing computations v few very long CPU bursts

9 Example Scheduling in XINU q Each process assigned a priority Q Non-negative integer value Q Initialized when process created Q Can be changed q Scheduler chooses process with highest priority Q Processes with the same priority are scheduled in a round-robin fashion q Policy enforced as a system-wide invariant

10 The XINU Scheduling Invariant

q At any time, the CPU must run the highest priority eligible process. Among processes with equal priority, scheduling is round robin.

q Invariant enforced during Q Q

11 Implementation of Scheduling q Process eligible if state is Q ready or current q To avoid searching process table Q Keep ready processes on linked list called ready list Q Order ready list by priority Q Selection in constant time

12 Example Scheduler Code

13 Puzzle #1

q Invariant says that at any time, one process must be executing q code moves from one process to another q Question: which process executes the context switch code?

14 Solution to Puzzle #1 q ‘‘Old’’ process Q Executes first half of context switch Q Is suspended q ‘‘New’’ process Q Continues executing where previously suspended Q Usually runs second half of context switch

15 Puzzle #2

q Invariant says that at any time, one process must be executing q All user processes may be idle (e.g., applications all wait for input) q Which process executes?

16 Solution to Puzzle #2 q OS needs an extra process Q Called NULL process Q Never terminates Q Cannot make a system call that takes it out of ready or current state Q Typically an infinite loop

17 Lab 1 –Process Scheduling q Part I Q The chprio() function contains a bug (sys/chprio.) q Part II Q The resched() function contains a limitation (sys/resched.c), i.e., a process with a lower priority could suffer from starvation

18 Lab 1 –Process Scheduling q Part I Q The chprio() function contains a bug (sys/chprio.c)

19 Lab 1 –Process Scheduling q Part II Q The process scheduling policy has a limitation, namely process starvation Q You are asked to implement two different policies v Random scheduler v Proportional sharing scheduler

20 Random Scheduler

q Total N processes Pi (i=0..N-1) in the ready queue q Each Pi Q Priority: PRIOi Q Probability: PRIOi/sum(PRIOj) (j=0..N-1)

21 Proportional Sharing Scheduler q Two factors: Q The priority and execution time q Timer interrupt handler Q Related files: sys/clkint.Ssys/clkinit.c Q Interrupt rate –based on clock timer v ctr1000: 1ms Q Scheduling rate: v Interrupt rate * QUANTUM Q Others v preempt: counter

22 Lab 1 –Process Scheduling q Read relevant source code in Xinu Q Process queue management v h/q.hsys/queue.csys/insert.c, … Q Proc. creation/suspension/resumption/termination: v sys/create.c, sys/suspend.csys/resume.c, sys/kill.c Q Priority change v sys/chprio.c Q Process scheduling v sys/resched.c Q Other initialization code v sys/initialize.c

23 Process Control Block (proc.h)

24 Next Lecture

q Process Synchronization

25