Two Level

• Interactive systems commonly employ two-level scheduling Interactive Scheduling – CPU scheduler and Memory Scheduler • Memory scheduler was covered in VM – We will focus on CPU scheduling

1 2

Round Robin Scheduling Our Earlier Example

• Each is given a timeslice to run in • 5 Process J1 • When the timeslice expires, the next – Process 1 arrives slightly before process J2 process preempts the current process, 2, etc… and runs for its timeslice, and so on J3 – All are immediately • Implemented with runnable J4 – Execution times – A ready queue indicated by scale on – A regular timer interrupt J5 x-axis

0246 8 10 12 1416 18 20

3 4

Round Robin Schedule Round Robin Schedule

J1 J1

Timeslice = 3 units J2 Timeslice = 1 unit J2 J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

5 6

1 Round Robin Priorities •Pros – Fair, easy to implement • Each Process (or thread) is associated with a •Con priority – Assumes everybody is equal • Issue: What should the timeslice be? • Provides basic mechanism to influence a – Too short scheduler decision: • Waste a lot of time switching between processes – Scheduler will always chooses a thread of higher • Example: timeslice of 4ms with 1 ms context switch = 20% round robin overhead priority over lower priority – Too long • Priorities can be defined internally or externally • System is not responsive • Example: timeslice of 100ms – Internal: e.g. I/O bound or CPU bound – If 10 people hit “enter” key simultaneously, the last guy to run will only see progress after 1 second. – External: e.g. based on importance to the user • Degenerates into FCFS if timeslice longer than burst length

7 8

Example Example

• 5 Jobs J1 J1 – Job number equals priority J2 J2 – Priority 1 > priority 5 J3 – Release and execution J3 times as shown J4 • Priority-driven J4 preemptively J5 scheduled J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

9 10

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

11 12

2 Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

13 14

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

15 16

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

17 18

3 Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

19 20

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

21 22

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

23 24

4 Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

25 26

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

27 28

Example Example

J1 J1

J2 J2

J3 J3

J4 J4

J5 J5

0246 8 10 12 1416 18 20 0246 8 10 12 1416 18 20

29 30

5 Priorities Traditional UNIX Scheduler

• Two-level scheduler – High-level scheduler schedules processes between memory and disk – Low-level scheduler is CPU scheduler • Based on a multi- level queue structure • Usually implemented by multiple priority queues, with with round robin at round robin on each queue each level •Con – Low priorities can starve • Need to adapt priorities periodically – Based on ageing or execution history

31 32

Traditional UNIX Scheduler Traditional UNIX Scheduler

• The highest priority (lower • Priority = CPU_usage +nice +base number) is scheduled – CPU_usage = number of clock ticks • Decays over time to avoid • Priorities are re-calculated once permanently penalising the process per second, and re-inserted in – Nice is a value given to the process appropriate queue by a user to permanently boost or – Avoid starvation of low priority reduce its priority threads • Reduce priority of background jobs – Base is a set of hardwired, negative – Penalise CPU-bound threads values used to boost priority of I/O bound system activities • Swapper, disk I/O, Character I/O

33 34

Some Issues with Priorities Lottery Scheduling

• Require adaption over time to avoid starvation • Each process is issued with “lottery (not considering hard real-time which relies on tickets” which represent the right to strict priorities). • Adaption is: use/consume a resource – usually ad-hoc, – Example: CPU time • hence behaviour not thoroughly understood, and unpredictable • Access to a resource is via “drawing” a – Gradual, hence unresponsive lottery winner. • Difficult to guarantee a desired share of the CPU – The more tickets a process possesses, the • No way for applications to trade CPU time higher chance the process has of winning.

35 36

6 Lottery Scheduling Example Lottery Scheduling

• Advantages • Four process running concurrently – Simple to implement – Process A: 15% CPU – Highly responsive – Process B: 25% CPU • can reallocate tickets held for immediate effect – Process C: 5% CPU – Tickets can be traded to implement individual scheduling policy between co-operating – Process D: 55% CPU threads – Starvation free • How many tickets should be issued to • A process holding a ticket will eventually be scheduled. each?

37 38

Lottery Scheduling Performance

Observed performance of two processes with varying ratios of tickets

39 40

Fair-Share Scheduling

• So far we have treated processes as individuals • Assume two users – One user has 1 process – Second user has 9 processes • The second user gets 90% of the CPU • Some schedulers consider the owner of the process in determining which process to schedule – E.g., for the above example we could schedule the first user’s process 9 times more often than the second user’s processes • Many possibilities exist to determine a fair schedule – E.g. Appropriate allocation of tickets in lottery scheduler

41

7