• 沒有找到結果。

Operating System: Chap5 Process Scheduling

N/A
N/A
Protected

Academic year: 2021

Share "Operating System: Chap5 Process Scheduling"

Copied!
56
0
0

加載中.... (立即查看全文)

全文

(1)

Operating System:

Chap5 Process Scheduling

National Tsing-Hua University

2016, Fall Semester

(2)

Overview

 Basic Concepts

 Scheduling Algorithms

 Special Scheduling Issues

 Scheduling Case Study

(3)

Basic Concepts

 The idea of multiprogramming:

 Keep several processes in memory. Every time one process has to wait, another process takes over the use of the CPU

 CPU-I/O burst cycle: Process execution consists of a cycle of CPU execution and I/O wait (i.e., CPU burst and I/O burst).

 Generally, there is a large number of short CPU bursts, and a small number of long CPU bursts

 A I/O-bound program would typically has many very short

CPU bursts

(4)

CPU – I/O Burst Cycle

(5)

Histogram of CPU-Burst Times

(6)

CPU Scheduler

new task

scheduler ready

running

waiting terminating

tasks signal

events

 Selects from ready queue to execute (i.e.

allocates a CPU for the selected process)

(7)

Preemptive vs. Non-preemptive

 CPU scheduling decisions may take place when a process:

1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready

4. Terminates

Non-preemptive scheduling:

 Scheduling under 1 and 4 (no choice in terms of scheduling)

 The process keeps the CPU until it is terminated or switched to the waiting state

 E.g., Window 3.x

Preemptive scheduling:

(8)

Preemptive Issues

 Inconsistent state of shared data

 Require process synchronization (Chap6)

 incurs a cost associated with access to shared data

 Affect the design of OS kernel

 the process is preempted in the middle of critical changes (for instance, I/O queues) and the kernel (or the device driver) needs to read or modify the same structure?

 Unix solution: waiting either for a system call to

complete or for an I/O block to take place before

(9)

Dispatcher

 Dispatcher module gives control of the CPU to the process selected by scheduler

 switching context

 jumping to the proper location in the selected program

 Dispatch latency – time it takes for the dispatcher to stop one process and start another running

 Scheduling time

 Interrupt re-enabling time

(10)

Scheduling Algorithms

(11)

Scheduling Criteria

CPU utilization

 theoretically: 0%~100%

 real systems: 40% (light)~90% (heavy)

Throughput

 number of completed processes per time unit

Turnaround time

 submission ~ completion

Waiting time

 total waiting time in the ready queue

Response time

(12)

Algorithms

 First-Come, First-Served (FCFS) scheduling

 Shortest-Job-First (SJF) scheduling

 Priority scheduling

 Round-Robin scheduling

 Multilevel queue scheduling

Multilevel feedback queue scheduling

(13)

FCFS Scheduling

 Process (Burst Time) in arriving order:

P1 (24), P2 (3), P3 (3)

 The Gantt Chart of the schedule

 Waiting time: P1 = 0, P2 = 24, P3 = 27

Average Waiting Time (AWT): (0+24+27) / 3 = 17

(14)

FCFS Scheduling

 Process (Burst Time) in arriving order:

P2 (3), P3 (3), P1 (24)

 The Gantt Chart of the schedule

 Waiting time: P1 = 6, P2 = 0, P3 = 3

Average Waiting Time (AWT): (6+0+3) / 3 = 3

(15)

Shortest-Job-First (SJF) Scheduling

 Associate with each process the length of its next CPU burst

 A process with shortest burst length gets the CPU first

 SJF provides the minimum average waiting time (optimal!)

 Two schemes

Non-preemptive – once CPU given to a process, it cannot be preempted until its completion

Preemptive – if a new process arrives with shorter burst

length, preemption happens

(16)

Non-Preemptive SJF Example

Process Arrival Time Burst Time

P1 0 7

P2 2 4

P3 4 1

P4 5 4

Ready queue: t=0

P1 (7)

0

Schedule

(17)

Non-Preemptive SJF Example

Process Arrival Time Burst Time

P1 0 7

P2 2 4

P3 4 1

P4 5 4

Ready queue: t=0

P1 (7)

P1

0 7

Schedule

(18)

Non-Preemptive SJF Example

Process Arrival Time Burst Time

P1 0 7

P2 2 4

P3 4 1

P4 5 4

Ready queue: t=7

P1 0

P2 (4) P3 (1) P4 (4)

7 Schedule

P3

8

(19)

Non-Preemptive SJF Example

Process Arrival Time Burst Time

P1 0 7

P2 2 4

P3 4 1

P4 5 4

Ready queue: t=8

P1 0

P2 (4) P4 (4)

7 Schedule

P3 8

P2

12

(20)

Non-Preemptive SJF Example

Process Arrival Time Burst Time

P1 0 7

P2 2 4

P3 4 1

P4 5 4

Ready queue: t=12

P1 0

P2 P4

P3

7 8 12 16

Schedule

Wait time = completion time – arrival time – run time (burst time)

P4 (4)

(21)

Preemptive SJF Example

Process Arrival Time Burst Time

P1 0 7

P2 2 4

P3 4 1

P4 5 4

Ready queue: t=0

P1 (7)

0

Schedule

P1

7

(22)

Preemptive SJF Example

Process Arrival Time Burst Time P1 0 7

P2 2 4 P3 4 1 P4 5 4

Ready queue: t=2

P1 0

P2 (4)

2 Schedule

P1 (5)

P2

6

(23)

Preemptive SJF Example

Process Arrival Time Burst Time P1 0 7

P2 2 4 P3 4 1 P4 5 4

Ready queue: t=4

P1 0

P2 (2)

P3 2

Schedule

P1 (5)

4

P3(1)

P2

5

(24)

Preemptive SJF Example

Process Arrival Time Burst Time P1 0 7

P2 2 4 P3 4 1 P4 5 4

Ready queue: t=5

P1 0

P2 (2) P4 (4) Schedule

P1 (5)

P3

2 4

P2

5

P2

7

(25)

Preemptive SJF Example

Process Arrival Time Burst Time P1 0 7

P2 2 4 P3 4 1 P4 5 4

Ready queue: t=7

P1 0

P4 (4) Schedule

P1 (5)

P3

2 4

P2

5

P2 7

P4

11

(26)

Preemptive SJF Example

Process Arrival Time Burst Time P1 0 7

P2 2 4 P3 4 1 P4 5 4

Ready queue: t=11

P1 0

Schedule

P1 (5)

P3

2 4

P2

5

P2 7

P4 P1

11 16

Wait time = completion time – arrival time – run time (burst time)

(27)

Approximate Shortest-Job-First (SJF)

 SJF difficulty: no way to know length of the next CPU burst

Approximate SJF : the next burst can be

predicted as an exponential average of the measured length of previous CPU bursts

1 1

1

α ...

α ) 1

α ( α ) 1

α + ( − 1 + − 2 2 +

= t n t n t n

Commonly,

α =

n n

n α t ( 1 α ) τ τ

+ 1 = + − history

new one

(28)

Exponential predication of next CPU burst

2 4 6 8 10 12 burst length

time

CPU burst t

目目目目目目目目目目

t0 t1 t2 t3 t4 t5 t6

6 4 6 4 13 13 13

(29)

Priority Scheduling

 A priority number is associated with each process

 The CPU is allocated to the highest priority process

 Preemptive

 Non-preemptive

 SJF is a priority scheduling where priority is the predicted next CPU burst time

 Problem: starvation (low priority processes never execute)

 e.g. IBM 7094 shutdown at 1973, a 1967-process never run)

 Solution: aging (as time progresses increase the priority

of processes)

(30)

Round-Robin (RR) Scheduling

 Each process gets a small unit of CPU time (time quantum), usually 10~100 ms

 After TQ elapsed, process is preempted and added to the end of the ready queue

 Performance

 TQ large  FIFO

 TQ small  (context switch) overhead increases

(31)

RR Scheduling (TQ = 20)

(32)

Multilevel Queue Scheduling

 Ready queue is partitioned into separate queues

 Each queue has its own scheduling algorithm

 Scheduling must be done between queues

 Fixed priority scheduling: possibility of starvation

 Time slice – each queue gets a certain amount of CPU time

(e.g. 80%, 20%)

(33)

Multilevel Feedback Queue Scheduling

 A process can move between the various queues; aging can be implemented

 Idea: separate processes according to the characteristic of their CPU burst

 I/O-bound and interactive processes in higher priority queue  short CPU burst

 CPU-bound processes in lower priority queue

long CPU burst

(34)

Multilevel Feedback Queue Example

 A new job enters Q 0. Algorithm: FCFS. If it does not finish in 8 ms CPU time, job is moved to Q 1

 At Q 1 is again served FCFS and receives 16 ms TQ.

If it still does not finish in 16 ms, it is preempted

(35)

Multilevel Feedback Queue

 In general, multilevel feedback queue scheduler is defined by the following parameters:

 Number of queues

 Scheduling algorithm for each queue

 Method used to determine when to upgrade a process

 Method used to determine when to

demote a process

(36)

Evaluation Methods

 Deterministic modeling – takes a particular predetermined workload and defines the

performance of each algorithm for that workload

 Cannot be generalized

 Queueing model – mathematical analysis

 Simulation – random-number generator or trace tapes for workload generation

 Implementation – the only completely accurate way

for algorithm evaluation

(37)

Review Slides ( I )

 Preemptive scheduling vs Non-preemptive scheduling?

 Issues of preemptive scheduling

 Turnaround time? Waiting time? Response time?

Throughput?

 Scheduling algorithms

 FCFS

 Preemptive SJF, Nonpreemptive SJF

 Priority scheduling

 RR

Multilevel queue

(38)

Multi-Processor Scheduling

Multi-Core Processor Scheduling

Real-Time Scheduling

(39)

Multi-Processor Scheduling

Asymmetric multiprocessing :

 all system activities are handled by a processor (alleviating the need for data sharing)

 the others only execute user code (allocated by the master)

 far simple than SMP

Symmetric multiprocessing (SMP):

 each processor is self-scheduling

 all processes in common ready queue, or each

has its own private queue of ready processes

(40)

Processor affinity

Processor affinity: a process has an affinity for the processor on which it is currently running

 A process populates its recent used data in cache memory of its running processor

 Cache invalidation and repopulation has high cost

 Solution

soft affinity :

possible to migrate between processors

hard affinity:

Core1 Cache Core2 Cache

Core1 Core2

(41)

NUMA and CPU Scheduling

NUMA (non-uniform memory access):

 Occurs in systems containing combined CPU and memory boards

 CPU scheduler and memory-placement works together

 A process (assigned affinity to a CPU) can be allocated

memory on the board where that CPU resides

(42)

Load-balancing

 Keep the workload evenly distributed across all processors

 Only necessary on systems where each processor has its own private queue of eligible processes to execute

 Two strategies:

Push migration : move (push) processes from overloaded to idle or less-busy processor

Pull migration : idle processor pulls a waiting task from a busy processor

 Often implemented in parallel

 Load balancing often counteracts the benefits of

(43)

Multi-core Processor Scheduling

Multi-core Processor:

 Faster and consume less power

 memory stall: When access memory, it spends a significant amount of time waiting for the data become available. (e.g.

cache miss)

Multi-threaded multi-core systems:

 Two (or more) hardware threads are assigned to each core (i.e. Intel Hyper-threading)

 Takes advantage of memory stall to make progress on

another thread while memory retrieve happens

(44)

Multi-core Processor Scheduling

 Two ways to multithread a processor:

coarse-grained: switch to another thread when a memory stall occurs. The cost is high as the instruction pipeline

must be flushed.

fine-grained (interleaved): switch between threads at the boundary of an instruction cycle. The architecture design includes logic for thread switching – cost is low.

 Scheduling for Multi-threaded multi-core systems

 1st level: Choose which software thread to run on each hardware thread (logical processor)

 2nd level: How each core decides which hardware thread

(45)

Real-Time Scheduling

 Real-time does not mean speed, but keeping deadlines

 Soft real-time requirements:

 Missing the deadline is unwanted, but is not immediately critical

 Examples: multimedia streaming

 Hard real-time requirements:

 Missing the deadline results in a fundamental failure

Examples: nuclear power plant controller

(46)

Real-Time Scheduling Algorithms

 FCFS scheduling algorithm – Non-RTS

 T1 = (0, 4, 10) == (Ready, Execution, Deadline)

 T2 = (1, 2, 4)

 Rate-Monotonic (RM) algorithm

 Shorter period, higher priority

 Fixed-priority RTS scheduling algorithm

 Earliest-Deadline-First (EDF) algorithm

 Earlier deadline, higher priority

 Dynamic priority algorithm

(47)

Rate-Monotonic (RM) Scheduling

 Fixed-priority schedule.

 All jobs of the same task have same priority.

 The task’s priority is fixed.

 The shorter period, the higher priority.

 Ex: T 1 =(4,1), T 2 =(5,2), T 3 =(20,5) (Period, Execution)

 ∵ period: 4 < 5 < 20

 ∴ priority: T 1 > T 2 > T 3

(48)

Early Deadline First (EDF) Scheduler

 Dynamic-priority scheduler

 Task’s priority is not fixed

 Task’s priority is determined by deadline.

 Ex: T 1 =(2,0.9), T 2 =(5,2.3)

 time:

0.9 0.9 0.9 0.9 0.9

0

T 1

0.9 2 2.9 4.1 5 6 6.9 8 8.9

delay

(49)

Review Slides (II)

 What is processor affinity?

 Real-time scheduler

 Rate-Monotonic

 Earliest deadline first

(50)

Operating System Examples

Solaris

Windows

Linux

(51)

Solaris Scheduler

 Priority-based multilevel feedback queue scheduling

 Six classes of scheduling:

real-time, system, time sharing,

interactive, fair share, fixed priority

 Each class has its own priorities and scheduling algorithm

 The scheduler converts the class- specific priorities into global

priorities

(52)

Solaris Scheduler Example ( time sharing, interactive )

 Inverse relationship between priorities and time slices:

the higher the priority, the smaller the time slice

 Time quantum expired: the new priority of a thread that has used its entire time quantum without blocking

 Return from sleep: the new priority of a thread that is

returning from sleeping (I/O wait)

(53)

Windows XP Scheduler

 Similar to Solaris: Multilevel feedback queue

 Scheduling: from the highest priority queue to lowest priority queue (priority level: 0 ~ 31)

 The highest-priority thread always run

 Round-robin in each priority queue

 Priority changes dynamically except for Real-Time class

real-time high above

normal normal below

normal idle priority

time-critical 31 15 15 15 15 15

highest 26 15 12 10 8 6

above normal 25 14 11 9 7 5

normal 24 13 10 8 6 4

below normal 23 12 9 7 5 3

lowest 22 11 8 6 4 2

class

relative

(54)

Linux Scheduler

 Preemptive priority based scheduling

 But allows only user mode processes to be preempted

 Two separate process priority ranges

 Lower values indicate higher priorities

Higher priority with longer time quantum

 Real-time tasks: (priority range 0~99)

 static priorities

 Other tasks: (priority range 100~140)

 dynamic priorities based on task interactivity

(55)

Linux Scheduler

 Scheduling algorithm

 A runnable task is eligible for execution as long as it has remaining time quantum

 When a task exhausted its time quantum, it is considered expired and not eligible for execution

 New priority and time quantum is given after a task is expired

(56)

Reading Material & HW

 Chap 5

 Problems

 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.10, 5.14, 5.15, 5.22

參考文獻

相關文件

* All rights reserved, Tei-Wei Kuo, National Taiwan University,

– Transfers operating system from mass storage to main memory. – Executes jump to

In terms of rehiring the same foreign worker, the employer needs to process document on his own prior to the worker's return to his or her home country (The employer does not need

Ayurvedic medicine aims to keep each individual’s bo dy in a health y ba lanc e accord in g to its uniqu e combina ti on of doshas... It is also a money -making business where th

Threads are assigned to processors by the scheduler for executions. Each processes have their own memory (for

5/4 System configuration and log systems 5/11 Network Address Translation (NAT)
. and Virtual Private

ƒ Preemptive scheduling – CPU scheduling is invoked whenever a process arrives at the ready queue, or the running process relinquishes the CPU.. ƒ Non-preemptive scheduling – CPU

ƒ Preemptive scheduling – CPU scheduling is invoked whenever a process arrives at the ready queue, or the running process relinquishes the CPU.. ƒ Non-preemptive scheduling – CPU