o perating s ystems and a rchitectures cs-m98 p art 7 s cheduling benjamin mora 1 swansea university...

39
OPERATING SYSTEMS AND ARCHITECTURES CS-M98 PART 7 SCHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

Upload: nyasia-chinnock

Post on 15-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

OPERATING SYSTEMS AND ARCHITECTURES CS-M98 PART 7 SCHEDULING

Benjamin Mora 1Swansea University

Dr. Benjamin Mora

Page 2: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

CONTENT

Main Principles behind Scheduling.

Scheduling Algorithms.

Comparison of Several Algorithms.

2Benjamin MoraSwansea University

Page 3: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

MAIN PRINCIPLES

3Benjamin MoraSwansea University

Page 4: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

REMINDER: PROCESS STATES

4Benjamin MoraSwansea University

Page 5: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING Scheduling consists of determining which processes are to

be run by the processor.Mainly for multitasked environments.Mainly Pre-emptive.

The scheduler is the part of the OS managing scheduling. The scheduling algorithm is the algorithm used by the

scheduler. Process/Context Switching consists of switching between

tasks.Consume resources

5Benjamin MoraSwansea University

Page 6: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

WHEN SCHEDULING When a process blocks (e.g. on I/O, a semaphore, or

voluntarily). When a process terminates. When a new process is created. When an interrupt occurs

e.g. blocking I/O operation now ready, so might want to activate waiting process.

End of a quantumcurrent process has had its time slot, OS will preempt it and run

another process. Note that some operating systems and some scheduling algorithms are non-preemptive, so this never happens.

6Benjamin MoraSwansea University

Page 7: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

DECISION MODE Non-Multitask (Non-preemptive)

Processes will be run sequentially.

Co-operative (Non-preemptive)The process blocks itself when needed (termination, I/O, OS

request).

PreemptiveThe OS interrupts Processes. The process can be interrupted by the OS.When quantum time lapsed.More overhead.More interactive.

7Benjamin MoraSwansea University

Page 8: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

DIFFERENCES BETWEEN PROCESSES Different processes: Different goals!

OS must have a good strategy.Adaptive mechanism.

CPU-bound processes.

Mainly computations, only a few I/O operations.More time can be allocated to these processes.

I/O-bound processes.

Most time waiting for events.

8Benjamin MoraSwansea University

Page 9: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING ALGORITHM TYPE Different Operating Systems, different goals! Interactive Environment:

Interact with the user. Personal computers.

Preemptive scheduling.Event programming.

Batch Environment:Send jobs to the system.

Data centers, supercomputers, etc…Global performances improved.Can be cooperative, preemptive with long delays.

9Benjamin MoraSwansea University

Page 10: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SUB-SCHEDULERS Long-Term Scheduler

Not Frequently Used, only on coarse-grained programs.Decides which programs to add to the pool.

Mid-Term Scheduler

For virtual memory management.

Short-Term Scheduler

Often called, makes fast decisions.Favor short tasks (real-time programs).

10Benjamin MoraSwansea University

Page 11: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING ALGORITHM TYPEReal-Time Operating Systems:

Ensuring/Meeting/Predicting deadlines.Upper bound execution time.Required efficient programming methodologies!

Planes, nuclear power plants, consoles, multimedia systems, etc…

Process environment better known from the start.

11Benjamin MoraSwansea University

Page 12: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING ALGORITHM TYPE Hard Real-Time. Deadlines must be met!

System failing otherwise Deterministic and reliable.

Though immediate detection & recovery must be implemented. Paging, Swapping & dynamic memory allocations usually not possible.

Proven/normalized/deterministic compilers. Interrupts minimized.

Planes, power plants, critical systems. Worst Case Executation Time must be known!

Soft Real-Time. Deadlines should be met! Relaxed constraints. Priority given to real-time processes. Results sometimes discarded if not on time. Multimedia systems. Consoles. Phones.

12Benjamin MoraSwansea University

Page 13: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING ALGORITHM TYPEFirm Real-Time. Intermediate between soft and

hard

Deadlines should be met. Any failure will degrade performance. “A few” misses could be accepted.

Results are invalid if obtained after deadline! The opportunity is lost

Interactive systems E.g, RealTime financial systems. High-Frequency Trading

Loss of money

13Benjamin MoraSwansea University

Page 14: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING OBJECTIVESFairness.

Make sure each process gets its fair share of the CPU, and no process suffers indefinite postponement.Priority settings possible.Balance resource usage.

Efficiency. Keep the CPU and the other parts of the system as busy

as possible.Reduced overhead.

14Benjamin MoraSwansea University

Page 15: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING OBJECTIVESBatch Systems:

Maximize throughput/jobs per hour. Turnaround time. Minimize time between submission

and termination.Maximize CPU utilization.

Interactive systems:Minimize response time.Increase productivity.

Real-Time Systems:Quite specific to the context.

15Benjamin MoraSwansea University

Page 16: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING ALGORITHMS

16Benjamin MoraSwansea University

Page 17: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SCHEDULING ALGORITHMSFirst come, first served.Round Robin.Priority scheduling.Shortest process next.Shortest remaining time & Highest Response Ratio

Next.Multilevel Feedback queues.Early Unix Priority Scheduling.Fair Share Scheduling.

17Benjamin MoraSwansea University

Page 18: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

FIRST COME, FIRST SERVED FIFO list of ready processes. When a process terminates its execution, the next process

in the queue is executed. Nonpreemptive. High response time. Minimum overhead. Penalize short processes. No starvation.

18Benjamin MoraSwansea University

Page 19: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

ROUND ROBIN (TIME SLICING) FIFO list. Widely used.

SimpleFairpreemptive.minimum overhead.no starvation.good response time for short processes

A quantum of time is defined.Must be carefully chosen. Depends on the processor type.

19Benjamin MoraSwansea University

P1

P2

P3

P4

FIFO List10 ms

10 ms

10 ms

10 ms

10 ms

10 ms

10 ms

Page 20: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

PRIORITY SCHEDULINGAdding priorities to processes is sometimes

required.Highest priority processes are run.Several priority queues have to be handled.Round-Robin can be performed inside each queue.

May lead to starvation.The OS has to detect starvation.Priority may be decreased.

20Benjamin MoraSwansea University

Page 21: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SHORTEST PROCESS NEXT Select the expected shortest process.

High throughput.High overhead.Better for short processes.

Non-preemptive.

Requires estimating the amount of time required by each process.Can be estimated by the user for batch jobs.Can be done based on statistics acquired from the running

processes.

21Benjamin MoraSwansea University

Page 22: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

RUNNING TIME PREDICTION

WithSn: Predicted evaluation of running time for the nth time the

process uses the CPU.Ti: Time used for the ith execution of the process.

To give more importance to the recent executions of the process, one can use:

22Benjamin MoraSwansea University

nn

n

inn Sn

nTn

TS11

1

11

nnn STS )1(1

Page 23: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SHORTEST REMAINING TIME, HIGHEST RESPONSE RATIO NEXT

Based on the same idea as Short Process Next.

Shortest Remaining Time:Preemptive version of Short Process Next.

Highest Response Ratio Next:Non-Preemptive.Minimization of the normalized turn around time.Minimize starvation.Priority =1+ (waiting_time/estimated_run_time)

23Benjamin MoraSwansea University

Page 24: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

MULTILEVEL FEEDBACK QUEUES

24Benjamin MoraSwansea University

Page 25: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

MULTILEVEL FEEDBACK QUEUESBased on multi-level queues.

Lowest levels have increased priorities, but also shortest quantum times.

A new process enters the lowest level queue and may preempt the current running process.

All levels but the last one (round-robin) are usually first come, first served.

25Benjamin MoraSwansea University

Page 26: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

MULTILEVEL FEEDBACK QUEUESMain characteristics:

Preemptive.Throughput and response times OK, although not best.Possibly high overhead.May favor I/O bound processes.Possible starvation.

26Benjamin MoraSwansea University

Page 27: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

EARLY UNIX PRIORITY SCHEDULINGEvery active process has a scheduling priority.

The CPU is given to the process having the highest priority The lowest priority value in implementation!

User process priorities change every quantum.Cannot go beyond a specific priority.Not for super-user processes.

27Benjamin MoraSwansea University

Page 28: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

EARLY UNIX PRIORITY SCHEDULING Each process control block contains:

the CPU usage of the process. (cpu)a scheduling priority. (prio)

Rules: If a process does not use its entire time slice, the algorithm will

tend to run sooner.At every clock interupt, the CPU usage is incremented.At the end of every quantum (60 time units in example):

cpu = cpu / 2prio = cpu / 2 + BASE_LEVEL_PRIORITY;

28Benjamin MoraSwansea University

Page 29: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

EARLY UNIX PRIORITY SCHEDULING

29Benjamin MoraSwansea University

Page 30: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

SYSTEM VR4 AND SOLARIS

30Benjamin MoraSwansea University

Page 31: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

FAIR SHARE SCHEDULING Fixed distribution of CPU usage among users and groups.

A weight is given to every user/group. A grp_cpu variable must be added per user/group

After every quantum, for every process:

31Benjamin MoraSwansea University

cpu = cpu / 2;grp_cpu = grp_cpu / 2;prio = cpu / 2 + weight*(grp_cpu/2)+ BASE_LEVEL_PRIORITY;

Page 32: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

FAIR SHARE SCHEDULING : EXAMPLE

32Benjamin MoraSwansea University

=

Page 33: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

COMPLETELY FAIR SCHEDULER Linux 2.6.23.

Not O(1) Scheduler.

Schedule classes.

Different scheduling policies:

SCHED_NORMAL, SCHED_BATCH, SCHED_IDLE.Real-time scheduler: SCHED_RR and SCHED_FIFO.

Uses a time-ordered red-black tree.

Nano-second granularity.

33Benjamin MoraSwansea University

Page 34: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

COMPARISON OF SEVERAL ALGORITHMS

34Benjamin MoraSwansea University

Page 35: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

ASSUMPTIONS 5 Processes arrive at time 0.

The CPU ready queue is empty at time 0.

The time needed for context switches can be ignored.

35Benjamin MoraSwansea University

Page 36: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

ASSUMPTIONS In round robin scheduling, a process will release the CPU

voluntarily if it needs less than a quantum.

A newly arrived process is placed in the ready queue before a process whose quantum expires at the same time.

36Benjamin MoraSwansea University

Page 37: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

COMPARISON

37Benjamin MoraSwansea University

Page 38: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

CRITERIA / ANALYSISAverage waiting time.

Throughput.number of jobs finished in 30 ms.

Fairness/Efficiency:number of processes getting the CPU after 30 ms.

38Benjamin MoraSwansea University

Page 39: O PERATING S YSTEMS AND A RCHITECTURES CS-M98 P ART 7 S CHEDULING Benjamin Mora 1 Swansea University Dr. Benjamin Mora

COMPARISON

39Benjamin MoraSwansea University