Scheduling

Dan Goldsmith

Scheduling

Introduction

  • Things are starting to get interesting now
    • We have been developing systems that do multiple things at once
    • Looked at strategies for dealing with Input driven tasks
  • Time for some Theory

Thread States

Thread States

Thread States

  • Running: Currently running thread
  • Ready: Threads that are ready to run
  • Waiting: Threads waiting for an event to occur
  • Inactive: Threads that are not created

So How do we switch between Threads

  • Only one task can one at a time
  • We need some mechanism to switch between tasks
  • This is the responsibility of the Scheduler

The Scheduler

The Scheduler

  • Part of the Kernel responsible for determining which task runs next
  • At each Tick the scheduler evaluates:
    • What tasks are Ready to be Run
    • Which task should run next

Scheduling Criteria

  • Utilisation (ie CPU / IO Usage)
  • Throughput (completing more processes in a given time)
  • Turnaround (Time taken to complete processes)
  • Response (Delay in user interaction)
  • Deadline (Hard or Soft Realtime)
  • Priority (Some Processes are more important than others)
  • Predictability (less variation in performance)

Scheduler Concepts

  • Wait Time: Time between a process being made ready to run and it starting
  • Run Time: Length of time a job runs for
  • Turnaround Time: Time between a process being make ready and it completing

Properties of the Task

  • We also have to deal with different types of task
    • IO Bound: Deal mainly with User Input
    • CPU Bound: Deal mainly with processing.
  • We may also have Tasks that depend on each other.

Non Preemptive Scheduling (Cooperative Multitasking)

  • Processes co-operate to make best use of the CPU
  • Each task will keep running till it decides to give up or blocks
    • New Higher Priority tasks will only get access when the CPU is free
    • Events are handled by interrupts

Non Preemptive Scheduling

  • Advantages:
    • Reduces Interrupt latency
    • Can make it easier to use shared resources
  • Disadvantages:
    • responsiveness is low
    • non deterministic

First Come First Served

  • First Come First Served (or FIFO)
    • Processes are added to the queue in order
    • When a process completes, the next one is executed.

FCFS Example

Process Start Time Job Length
1 0 5
2 1 10
3 2 1

FCFS Example:

FCFS Initial

FCFS Example: Task 1

FCFS Task 1

FCFS Example: Task 2

FCFS Task 2

FCFS Example: Task 3

FCFS Task 2

FCFS Summary

  • Average Waiting Time:

    • \[\frac{(0 + 4+ 13)}{3} = 5.66\]
  • Average Turnaround Time:

    • \[\frac{(5+14+14)}{3} = 11.0\]

First Come First Served:

  • Advantages:
    • Low scheduler overhead
    • Excellent when the program only consists of short, tasks
  • Disadvantages:
    • Low Throughput ?
    • How to deal with blocking tasks?

Shortest Job First

  • Aims to improve scheduling by giving priority to short lived tasks
    • Minimises average waiting time for completion.
    • How do we predict how long a job will take?
    • What happens if we have lots of short processes
      • Can starve longer processes of resources.

SJF

Process Start Time Job Length
1 0 5
2 1 10
3 2 1

SJF Example:

SJF Initial

SJF Example: Task 1

SJF Task 1

SJF Example: Task 3

SJF Task 2

SJF Example: Task 2

SJF Task 2

SJF Summary

  • Average Waiting Time:

    • \(\frac{(0 + 3+ 5)}{3} = 2.66\)
  • Average Turnaround Time:

    • \(\frac{(5+4+15)}{3} = 8.0\)

SJF Starving Longer Processes

  • A Problem with SJF is it can starve longer running processes
  • If many short processes come in, then they are given priority
  • This can be a problem with repeating tasks. (Especially if added to a queue)

Task

  • Using the folloiwng processes. Calculate Wait, and Tunraround time for both SJF and FCFS
Process Arrives At Execute Time
A 1 1
B 2 5
C 3 3

Preemptive Scheduling

  • RTOS use a Pre-emtive scheduling approach
  • Based on the Priory of Tasks
    • Task with the highest priority is the next to be executed
  • Schedule can also interrupt a tasks execution

Preemptive Scheduling

  • Task with the highest priority is run first
  • If there are tasks with equal priority the scheduler chooses which to run

Preemptive Scheduling

  • Advantages:
    • Tend to have increased responsiveness
    • Task response time is limited
  • Disadvantages:
    • Overhead in task switching
  • Much harder to deal with shared resources.

The Scheduling Quantum

  • The Unit of time which the scheduler is based on
  • Hardware generates a signal at a set frequency (a tick)
  • A Quantum is based on a set number of ticks

Picking a Quantum Size:

  • Very difficult
    • Large Quantum may starve small processes depending on the Scheduling
    • Small Quantum will cause overhead
    • Choice may also depend on the aspects of the system
    • Some mathematical black magic needs to happen here.

Round Robin Scheduling:

  • Processes are added to a Queue (similar to FCFS)
  • Execution is broken into “slots” (A Quantum)
    • Each process runs for a specified length of time
  • At each scheduling interval, the next process in the queue is picked

Round Robin Scheduling

  • Each Process gets an equal turn at execution
1 2 3 4 5 6
P1 P2 P3 P1 P2 P3

Roudn Robin Scheduling: Example

Process Start Time Job Length
1 0 5
2 1 10
3 2 1

Round Robin Scheduling

RR Task 2

Round Robin Scheduling

RR Task 2

Round Robin Scheduling

RR Task 2

Round Robin Scheduling

RR Task 2

Round Robin Scheduling

RR Task 2

Round Robin Scheduling

  • Advantages:
    • Starvation Free, All process get an equal opportunity to run
    • Decent Throughput.
  • Disadvantages:
    • Higher overhead, due to context switching
    • Higher overall Turnaround and Waiting time.

Round Robin with Priority Scheduling.

  • Default Scheduler used in MBED / CMSIS RTOS
  • As Round Robin. However higher priority tasks are favoured.
  • It is also possible for a high priority task to Interrupt the current process.

Round Robin and the Quanum Size:

  • The Quantum size can have an effect on the scheduling efficency.
  • Lets see what happens in our example if we use a quantim size of 2
Process Start Time Job Length
1 0 5
2 1 10
3 2 1

Round Robin and the Quantum Size:

TODO: Add Photo

Round Robin Tasks

  • Calculate Turnaround and Waiting times for the following processes
    • Using a Quantum of 1
    • Using a Quantum of 2
Process Arrives At Execute Time
A 1 1
B 2 5
C 3 3

Scheduling Priority

Priority in Scheduling

  • Static: Priority of task is Set at compile time
    • Advantage: Predictability
    • Disadvantage: May suffer propriety inversion
  • Dynamic: Each task can change its priority
    • Advantage: Can help avoid Priority Inversion

Priority Inversion

  • A Low Priority task blocks an important one
    • Usually based around resource sharing
    • Low priority task Locks the resource
    • High priority needs access to the resource but cannot due to the Lock.
  • Difficult to detect and debug.

Priority Inversion

  • Three Tasks
    • Task One: High Priority
    • Task Two: Medium Priority
    • Task Three: Low Priority
  • Lets assume that there is also a shared resource on the system.
    • For Thread Safety, this resource can be “Locked” buy the thread using it.

Priority Inversion

  • Thread One, Starts and Gains Control of the Shared Resorce.
  • Thread Two, Starts: Prempting Thread one to become the running thread
  • Thread Three: Starts, Prempts then is Blocked when it trys to access Shard Resource.
    • Threfore Thread Three put in Waiting State
  • Thread Two is the next Available task, so runs to completion
  • Thread One, then runs untill the resource is unlocked
    • At this point Thread Three can Prempt and finish.

Priority Inversion

Priority Inversion

Priority Inversion Solution

  • Temporarily raise priority of task 3 to that of task one
  • This allows the task to complete, and release the lock on the resource.

Setting Thread priority

Setting Thread Priority

  • Lets Examine Thread Priority with an Example

Thread Priority Code

int main() {

  // put your setup code here, to run once:
  threadOne.start(led1Thread);
  threadTwo.start(led2Thread);
  threadThree.start(led3Thread);
  
  wait_ms(osWaitForever);
}

Running with default priority

  • Everything Behaves as expected.
  • All tasks are given Equal opportunity to run

How does this fit with our Round Robin?

SPARKBOARD!

Modifying the Priority of the Threads

  • Set our first task to a Different Priority
  • Visually this makes no difference.
    • What about the Print Statements

What about the Scheduler Now

SPARKBOARD!

What about Blocking Tasks?

  • What happens if one of our threads runs a high intensity task?
  • Sheduler “Tick” is ~1ms
void blockingThread(){
 while(1){
   led1 = !led1;
   for (int x=0; x<10000; x++){
     //Delay of Approx One Second
     wait(0.0001);
   }
 }
}

Changing the Prioirty

  • Standard Prioirity: Runs as Before
  • Low Prioirty: Runs as Before
  • High Prioirty: No other Threads get a chance to run

Changing the priority

threadOne.set_priority(osPriorityLow);

And the Scheduler Now

SPARKBOARD

Bonus Example:

  • There is a second thread priority example on Github.
  • Try running the code and see what effect priority has here.

Summary

Summary

  • This session we discussed scheduling algorithms
    • Preemptive Round Robin is used
  • Also had a look a threading priority
  • Next we will look at issues with sharing data in a multi threaded environment.
// reveal.js plugins