Operating system concepts. Task scheduling

Similar documents
Implementing Task Schedulers (1) Real-Time and Embedded Systems (M) Lecture 10

Chapter 5: CPU Scheduling. Operating System Concepts 9 th Edit9on

Scheduling. CSC400 - Operating Systems. 7: Scheduling. J. Sumey. one of the main tasks of an OS. the scheduler / dispatcher

CS 326: Operating Systems. CPU Scheduling. Lecture 6

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Subject Name: OPERATING SYSTEMS. Subject Code: 10EC65. Prepared By: Kala H S and Remya R. Department: ECE. Date:

Recap. Run to completion in order of arrival Pros: simple, low overhead, good for batch jobs Cons: short jobs can stuck behind the long ones

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

CPU Scheduling. Daniel Mosse. (Most slides are from Sherif Khattab and Silberschatz, Galvin and Gagne 2013)

CPU Scheduling. Basic Concepts. Histogram of CPU-burst Times. Dispatcher. CPU Scheduler. Alternating Sequence of CPU and I/O Bursts

Chapter 5: CPU Scheduling. Operating System Concepts 8 th Edition,

Operating Systems. Scheduling

Chapter 5: CPU Scheduling. Operating System Concepts Essentials 8 th Edition

Scheduling of processes

Operating systems and concurrency (B10)

Multi-Level Feedback Queues

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

Computer Systems Laboratory Sungkyunkwan University

Chapter 5: CPU Scheduling

Operating Systems. Process scheduling. Thomas Ropars.

Chapter 5: CPU Scheduling

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Practice Exercises 305

Last Class: Processes

Chapter 5: CPU Scheduling

High level scheduling: Medium level scheduling: Low level scheduling. Scheduling 0 : Levels

CS3733: Operating Systems

CS307: Operating Systems

W4118: advanced scheduling

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

real time operating systems course

Chapter 6: CPU Scheduling

CPU Scheduling. CSE 2431: Introduction to Operating Systems Reading: Chapter 6, [OSC] (except Sections )

Course Syllabus. Operating Systems

Chap 7, 8: Scheduling. Dongkun Shin, SKKU

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

CS370 Operating Systems

Scheduling. Today. Next Time Process interaction & communication

CSE 4/521 Introduction to Operating Systems

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Uniprocessor Scheduling. Basic Concepts Scheduling Criteria Scheduling Algorithms. Three level scheduling

Chapter 6: CPU Scheduling

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Operating Systems ECE344. Ding Yuan

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

8: Scheduling. Scheduling. Mark Handley

Multiprocessor and Real-Time Scheduling. Chapter 10

Announcements. Program #1. Program #0. Reading. Is due at 9:00 AM on Thursday. Re-grade requests are due by Monday at 11:59:59 PM.

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Chapter 6: CPU Scheduling

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Chapter 5: Process Scheduling

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Chapter 6: CPU Scheduling

CS370 Operating Systems

Process management. Scheduling

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Two Real-Time Operating Systems and Their Scheduling Algorithms: QNX vs. RTLinux

NuttX Realtime Programming

Chapter 5 Process Scheduling

Operating Systems CS 323 Ms. Ines Abbes

CPU Scheduling (Part II)

Process- Concept &Process Scheduling OPERATING SYSTEMS

Operating Systems. Lecture Process Scheduling. Golestan University. Hossein Momeni

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

CHAPTER 2: PROCESS MANAGEMENT

Resource Access Control (2) Real-Time and Embedded Systems (M) Lecture 14

Round Robin (RR) ACSC 271 Operating Systems. RR Example. RR Scheduling. Lecture 9: Scheduling Algorithms

Scheduling II. Today. Next Time. ! Proportional-share scheduling! Multilevel-feedback queue! Multiprocessor scheduling. !

Titolo presentazione. Scheduling. sottotitolo A.Y Milano, XX mese 20XX ACSO Tutoring MSc Eng. Michele Zanella

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Lecture 5 / Chapter 6 (CPU Scheduling) Basic Concepts. Scheduling Criteria Scheduling Algorithms

CS4514 Real Time Scheduling

Chapter 5: Process Scheduling

Priority-driven Scheduling of Periodic Tasks (2) Advanced Operating Systems (M) Lecture 5

SMD149 - Operating Systems

Class average is Undergraduates are performing better. Working with low-level microcontroller timers

Properties of Processes

Task Scheduling of Real- Time Media Processing with Hardware-Assisted Virtualization Heikki Holopainen

Chapter 5 Scheduling

Job Scheduling. CS170 Fall 2018

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

CS 318 Principles of Operating Systems

Chap 7, 8: Scheduling. Dongkun Shin, SKKU

Real-Time Systems Hermann Härtig Real-Time Operating Systems Brief Overview

Real-Time Systems. Real-Time Operating Systems

Chapter 5 CPU scheduling

Multimedia Systems 2011/2012

CPU Scheduling: Objectives

Scheduling in the Supermarket

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Scheduling

CS Computer Systems. Lecture 6: Process Scheduling

Comparison of soft real-time CPU scheduling in Linux kernel 2.6 series with Solaris 10

238P: Operating Systems. Lecture 14: Process scheduling

by Maria Lima Term Paper for Professor Barrymore Warren Mercy College Division of Mathematics and Computer Information Science

ALL the assignments (A1, A2, A3) and Projects (P0, P1, P2) we have done so far.

Learning Outcomes. Scheduling. Is scheduling important? What is Scheduling? Application Behaviour. Is scheduling important?

Scheduling. Scheduling 1/51

Announcements. Program #1. Reading. Due 2/15 at 5:00 pm. Finish scheduling Process Synchronization: Chapter 6 (8 th Ed) or Chapter 7 (6 th Ed)

Transcription:

Operating system concepts Task scheduling

Task scheduling (thread scheduling) Target of scheduling are ready tasks ACTIVE TASK BLOCKED TASKS PASSIVE TASKS READY TASKS Active task currently running on processor Ready tasks ready to be put on processor Blocked tasks wait for some resource (IO, semaphore) Passive tasks finished or not started tasks 2

Task scheduling problem System has more ready tasks than processors Main problem: how to divide processor(s) time to available ready tasks? How to schedule different tasks? tasks have different properties and expectations from the scheduler use per task type handling, or use same principles for all (general principles)? how to measure scheduling quality? scheduling metric? 3

Scheduling environments Different environments have different tasks, different requirements, different primary objectives Real-Time Systems (RTS), Embedded Systems monitor, control real process time management is crucial Personal Computers, Workstations user oriented (interactivity) Servers service oriented, process requests from different clients Mobile devices (handhelds, phones, multimedia players) single user and mostly only single task at a time 4

Scheduling quality In RTS, failure in scheduling can have serious consequences! In normal (other) systems (example quality measures) multimedia player must timely provide audio/video subsystems with data or perception of quality may not be satisfactory user interface should respond timely to user commands e.g. mouse movements, text/commands entry, mathematic calculations, data compression, file transfers and similar activities that take time to complete could be delayed if required by other task types, with very small or no observable penalty for user/system 5

Task types Tasks may be divided into categories by many criteria's How a task should be scheduled categories: normal tasks the user programs perform standard operations don t require special privileges don t have strict time constrains may use system resources through operating system system tasks perform system operations (services) require special privileges might have some (soft) time constrains time constrained tasks (RT tasks, multimedia players) deadline (must) be met or system failure happens if a task is periodic, it must get enough time to finish periodic processing before the start of next period ( implicit deadline ) 6

RT periodic task characteristic times t a t r t s t e t a +T t d t Legend: t a arrival time t r ready time t s start time t e end time t d deadline T period task processing implicit deadline 7

Task scheduling decisions Static scheduling behavior is defined before system is started decisions may be hard coded in system statically define task execution order and follow it assign priorities to tasks at task creation and schedule tasks by priority (always the highest priority task first) Dynamic scheduling decisions are made at run time system state is evaluated (including tasks) and next task to be scheduled is chosen examples: consumed CPU time for all tasks is compared and task with lowest consumption is chosen (to obtain fairness) deadlines are compared and task with nearest deadline is chosen (to meet all constraints) 8

Basic scheduling principles First Come First Served FCFS usually known as: First In First Out FIFO adequate for servers requests are processed in receiving order Priority based scheduling tasks with higher priority have precedence over lower priority tasks a higher priority task will always preempt lower prior. task! adequate for RTS, priority reflects task relevance Time share based scheduling Round Robin and similar tasks share (fairly) processors time adequate for multi-user workstations and servers e.g. terminal based servers (basic shell or with GUI) 9

Principal RTS scheduling principles Rate Monotonic Scheduling RMS (mostly used!) also known as Rate Monotonic Priority Assignment RMPA assigns priorities to tasks according to their period lengths frequent tasks (with shorter periods) get higher priority RMS only predefines task priorities (base priority) actual scheduling is performed later using priorities Deadline Driven Scheduling DDS task with nearest deadline is scheduled first (aka EDF, EDD) Sporadic scheduling periodic task within single period can run budget time with defined higher priority, and if that is not enough, the rest of processing in this period is performed with lower priority 10

RMPA and DDS examples T 3 RMPA D 3 D 2 D 1 T 1 T 3 T 2 DDS/EDF t D 3 D 2 D 1 T 1 T 2 t 11

Sporadic scheduling example Image from: http://www.qnx.com/developers/docs/6.3.0sp3/neutrino/sys_arch/kernel.html 12

Mostly implemented scheduling principle Non-RTS have distinction between normal tasks and RT tasks scheduling is different! RT task scheduling (implemented also on non-rts) priority is the main scheduling principle higher priority task always preempts lower ones tasks are organized into priority levels tasks with same priority are in the same level (queue) if more than one thread of the same priority exists then scheduler will schedule them by principle: FIFO first task will execute until its completed or until it becomes blocked (not ready) Round Robin each task will be given only a time slice of CPU before it is put at the end of queue, and first one from the queue is chosen next 13

Priority levels typical ready task organization n I idle task ready tasks with currently lowest priority k G H priority (lower number has higher priority j i+1 i i-1 F pointer to first task in level C D E pointer to last task in level 2 1 0 A B ready tasks with currently highest priority highest priority level (currently no task of this priority exist in system) 14

Example system state and scheduling Ready task 0 1 Task A 1 Ready task Active task 0 1 Ready task Active task 0 1 Active task 2 3 Task C 3 Task D 3 Task E 3 Task A 1 : wait(s 1) 2 3 Task C 3 Task D 3 Task E 3 Task C 3 : signal(s 0) 2 3 Task B 2 Task C 3 Task D 3 Task E 3 Semaphore Semaphore Semaphore S 0 Task B 2 S 0 Task B 2 S 0 S 1 S 1 Task A 1 S 1 Task A 1 S 2 Task F 3 a) S 2 Task F 3 b) S 2 Task F 3 c) After timer interrupt (RR) Task C 3 finishes Ready task 0 1 Active task Ready task 0 1 Active task Ready task 0 1 Active task 2 3 Task D 3 Task E 3 Task C 3 2 3 Task D 3 Task E 3 Task D 3 : signal(s 2) 2 3 Task D 3 Task E 3 Task F 3 Semaphore Semaphore Semaphore S 0 Task B 2 S 0 Task B 2 S 0 Task B 2 S 1 Task A 1 S 1 Task A 1 S 1 Task A 1 S 1 Task F 3 d) S 2 Task F 3 e) S 2 f) 15

Example scheduling priority levels Ready task Ready task 0 Active task 0 Active task 1 Task A 1 1 2 3 Task C 3 Task D 3 Task E 3 Task A 1 : wait(s 1 ) 2 3 Task C 3 Task D 3 Task E 3 Semaphore Semaphore S 0 Task B 2 S 0 Task B 2 S 1 S 1 Task A 1 S 2 Task F 3 a) S 2 Task F 3 b) superscripts indicate tasks priority 16

Example scheduling Ready task 0 1 Ready task Active task 0 1 Active task 2 3 Task C 3 Task D 3 Task E 3 Task C 3 : signal(s 0 ) 2 3 Task B 2 Task C 3 Task D 3 Task E 3 Semaphore Semaphore S 0 Task B 2 S 0 S 1 Task A 1 S 1 Task A 1 S 2 Task F 3 b) S 2 Task F 3 c) 17

Example scheduling Ready task Ready task 0 Active task 0 Active task 1 1 2 3 Task C 3 Task D 3 Task E 3 After timer interrupt (RR) 2 3 Task D 3 Task E 3 Task C 3 Semaphore Semaphore S 0 Task B 2 S 0 Task B 2 S 1 Task A 1 S 1 Task A 1 S 2 Task F 3 b) S 1 Task F 3 d) 18

Example scheduling Ready task 0 1 2 3 Semaphore Task C 3 Task D 3 Task E 3 Active task 0 Task C 3 finishes Ready task 1 2 3 Task D 3 Task E 3 Semaphore Active task S 0 Task B 2 S 0 Task B 2 S 1 Task A 1 S 1 Task A 1 S 2 Task F 3 b) S 2 Task F 3 e) 19

Example scheduling Ready task Ready task 0 Active task 0 Active task 1 1 2 3 Task D 3 Task E 3 Task D 3 : signal(s 2 ) 2 3 Task D 3 Task E 3 Task F 3 Semaphore Semaphore S 0 Task B 2 S 0 Task B 2 S 1 Task A 1 S 1 Task A 1 S 2 Task F 3 S 2 e) f) 20

Scheduling in non-real-time environments Scheduling normal tasks What is a normal task? task with soft time constraint, or with no time constrains soft time constraint allowed delay is e.g. > 100 ms nothing critical will happen if some time constraint occasionally is not met however, significant or frequent delays in responses will degrade user experience (satisfaction with computer system) worker tasks perform CPU intensive operations time to completion is measured in (at least several) seconds don t interact with user (at least not frequently) don t require IO devices, don t control or monitor them 21

Non-RT scheduling principles examples Fairness share CPU(s) time fair to all tasks in system respect tasks priorities higher priority = more CPU time Processor utilization the more the better more jobs are performed Tasks throughput finish more tasks ( favor short tasks ) Minimize queue waiting times shorten length of time slice, but Response time favor interactive tasks they mostly don t use CPU, but when they are activated (e.g. on keystroke) give them CPU as soon as possible Hot cache optimization on multiprocessors maintain same tasks on same processors they might find their data still in cache even after context change 22

Multilevel feedback queue Theoretical scheduling that is used in today s operating systems (basic ideas at least) is called multilevel feedback queue Multilevel feedback queue (from Wikipedia) In computer science, a multilevel feedback queue is a scheduling algorithm. It is intended to meet the following design requirements for multimode systems: Give preference to short jobs. Give preference to I/O bound processes. Quickly establish the nature of a process and schedule the process accordingly. 23

Multilevel feedback queue (from Wikipedia, cont) Multiple FIFO are used and the operation is as follows: A new process is positioned at the end of the top-level FIFO queue. At some stage the process reaches the head of the queue and is assigned the CPU. If the process is completed it leaves the system. If the process voluntarily relinquishes control it leaves the queuing network, and when the process becomes ready again it enters the system on the same queue level. If the process uses all the quantum time, it is pre-empted and positioned at the end of the next lower level queue. This will continue until the process completes or it reaches the base level queue. 24

Multilevel feedback queue (from Wikipedia, cont) At the base level queue the processes circulate in round robin fashion until they complete and leave the system. Optionally, if a process blocks for I/O, it is 'promoted' one level, and placed at the end of the next-higher queue. This allows I/O bound processes to be favored by the scheduler and allows processes to 'escape' the base level queue. In the multilevel feedback queue, a process is given just one chance to complete at a given queue level before it is forced down to a lower level queue. 25

Scheduling of normal tasks in Linux Linux uses Completely Fair Scheduler - CFS in use since kernel 2.6.23 (2007.) Basic CFS principles (theory) if there are N processors and M tasks (N < M), each task should get N/M percent of computing time for every task, wait runtime is tracked ( deserved time minus used time ) task with highest wait runtime is chosen by scheduler Example (all tasks have same priority nice level ): Task 1 gets time slice T. After the slice is consumed, scheduler updates wait runtimes for all tasks: wr 1 = wr 1 + T/N T wait runtime is reduced! wr 2 = wr 2 + T/N wait runtime is increased wr 3 = wr 3 + T/N wait runtime is increased Red-black trees are used for task organization wait runtime parameter defines task position 26

Win32 scheduling Thread base priority is calculated from its process priority class and thread priority level in range from 1 to 15 for normal tasks (16-31 are for RT) Priority can be boosted for: Foreground (in focus) thread IO bound threads (they rarely use CPU time) Priority can be lowered for CPU intensive threads but only to its base priority Threads are scheduled to run based on their scheduling priority highest priority thread gets most of the time others get little, just to prevent starvation The system treats all threads with the same priority as equal The system assigns time slices in a round-robin fashion to all threads with the highest priority 27

Comparing FIFO, RR and normal task scheduling Example (From book: Operacijski sustavi, L. Budin i ostali, 2010. (in Croatian)): four tasks A, B, C and D with processing times T P (A) = 5, T P (B) = 5, T P (C) = 3 and T P (D) = 2 (time units) priorities are shown on picture (lower number higher priority) 0 1 2 A 3 4 5 B C D N Idle 28

Comparing FIFO, RR and normal task scheduling A B C D Idle 0 5 10 15 t a) FIFO A ends B ends C ends D ends A B C D B C D B C B B Idle 0 5 10 15 t b) Round Robin time slice is 1 A ends D ends C ends B ends B, C and D have same priority and use RR A B C D A B C D A B C B B Idle 0 5 10 15 t c) Normal task scheduling higher priority task gets more time more time in this example is achieved with a longer time slice D ends C ends B ends A ends 29

Priority inversion problem (RT scheduling) a) high priority task A preempts lower priority task B (as excpected) p A B b) high priority task A is blocked while lower priority task B, who has locked resource continues (as excpected!) priority inversion! p A B t t c) high priority task A is blocked while lower priority task C further delays task A (not as excpected!) priority inversion! p A C B t 30

Priority inversion - solutions Priority inheritance protocol when a higher priority task A is blocked on resource that is owned by a lower priority task B, then temporarily boost priority of B to the level of A, until B releases the resource, then return task B to original priority (that it had before boosting) intermediate tasks will not further delay high priority task! Priority ceiling protocol as task acquires resource X, raise task s priority to level defined in e.g. array priority_ceiling[x] as task releases the resource, return previous priority to task Both protocols are embedded into synchronization functions (e.g. mutex_lock, mutex_unlock) Protocols are essential for RTS! 31

p Priority inheritance protocol example E D C B A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 t Event description: 1 task A, as only ready task start executing (active task) 2 task A acquire S 1 (vertical lines marks S 1 ownership) 3 task B starts and preempt task A (higher priority at the moment) 4 task C starts and preempt task B 5 task C acquire S 2 (horizontal lines marks S 1 ownership) 6 task C requires S 1 owned by task A and is blocked on it; task A inherit task C priority and continue its execution (with priority of task C) 7 task D starts and preempt task A 8 task E starts and preempt task D 9 task E requires S 2 owned by task C and is blocked on it; task C inherit task E priority, but since task C is blocked by task A, task A inherits new priority of task C priority of task E (implicitly task A inherits task E priority) 10 task A releases S 1, and original priority is returned to task A (as it was before acquiring resource S 1 ); task C is released, S 1 is given to it; task C continues execution (highest priority ready task inherited priority of blocked task E) 11 task C releases S 2, and original priority is returned to task C; task E is released, S 2 is given to it; task E continues executing 12 task E releases S 2 and continues executing 13 task E finishes; task D continues executing 14 task D finishes; task C continues executing 15 task C releases S 1 continues executing 16 task C finishes; task B continues executing 17 task B finishes; task A continues executing 18 task A finishes Source for example: nas_prio.c 32

Scheduling - summary RT tasks: Priority based scheduling is widely used as primary scheduling principle When more tasks with same highest priority are ready: FIFO or Round Robin are used FIFO and RR are implemented on most (all) systems on all systems the implementation is identical (in respect to scheduling decisions) Normal tasks: Fair-share principle is used (adjusted with priority) Higher priority provides more CPU time It doesn t guarantee immediate preemption of lower priority tasks Differently implemented on different systems 33

Task scheduling POSIX interface

Thread scheduling: policy and priority Scheduling is defined by: thread scheduling policy: SCHED_FIFO SCHED_RR SCHED_SPORADIC SCHED_OTHER thread priority number in system specific range int sched_get_priority_min(int policy); int sched_get_priority_max(int policy); Policy and priority may be set on thread creation and/or changed later 35

Setting scheduling parameters for new thread Parameter of type pthread_attr_t that is passed at thread creation (pthread_create) contain scheduling attributes Changing/setting scheduling parameters function pthread_attr_setinheritsched inherit parameters from parent thread (PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED)? pthread_attr_setschedpolicy set policy (SCHED_FIFO, SCHED_RR, SCHED_SPORADIC or SCHED_OTHER) pthread_attr_setschedparam set thread priority 36

Changing scheduling parameters for thread Change policy and priority: pthread_setschedparam( pthread_t thread, int policy, const struct sched_param *param) or just priority pthread_setschedprio( pthread_t thread, int prio); Equivalent behavior on process level: sched_setscheduler(pid, policy, param) sched_setparam(pid, param) 37

Synchronization that influence scheduling For mutex (pthread_mutex_lock/unlock) priority inheritance or priority ceiling can be set: int pthread_mutexattr_setprotocol( pthread_mutexattr_t *attr, int protocol); values for protocol: PTHREAD_PRIO_INHERIT priority inheritance PTHREAD_PRIO_PROTECT priority ceiling PTHREAD_PRIO_NONE Setting value for priority ceiling: int pthread_mutex_setprioceiling( pthread_mutex_t *mutex, int prioceiling, int *old_ceiling); 38