Preemptive Scheduling

Similar documents
Real-Time Programming

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

6/20/2018. Lecture 2: Platforms & RTOS. Outline. Lab Setup (20 min) Labs work. Lecture: Platform + RTOS

Micrium OS Kernel Labs

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

Process Coordination and Shared Data

Putting it All Together

Threads Chapter 5 1 Chapter 5

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

CS/ECE 6780/5780. Al Davis

Page 1. Lab 5 Logistics. Implicit Threads. Explicit Thread Semantics. Problem not enough interrupt pins CS/ECE 6780/5780. Al Davis

Lecture notes Lectures 1 through 5 (up through lecture 5 slide 63) Book Chapters 1-4

AN HONORS UNIVERSITY IN MARYLAND UMBC. AvrX. Yousef Ebrahimi Professor Ryan Robucci

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

Zilog Real-Time Kernel

ELEC 377 Operating Systems. Week 1 Class 2

embos Real-Time Operating System embos plug-in for IAR C-Spy Debugger Document: UM01025 Software Version: 3.0 Revision: 0 Date: September 18, 2017

Lab 8 Real-time OS - 1

Announcement. Exercise #2 will be out today. Due date is next Monday

EECS 482 Introduction to Operating Systems

Lecture 3: Concurrency & Tasking

SMD149 - Operating Systems

embos Real-Time Operating System embos plug-in for IAR C-Spy Debugger Document: UM01025 Software Version: 3.1 Revision: 0 Date: May 3, 2018

Introduction to Real-Time Operating Systems


Stellaris Robotic Evaluation Board and Micriµm µc/os-iii

ZiLOG Real-Time Kernel Version 1.2.0

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

2. Introduction to Software for Embedded Systems

Micriμm. Getting Started with Micriμm s. Matt Gordon, Sr. Applications Engineer. 9L05I Renesas Electronics America Inc.

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Threads Implementation. Jo, Heeseung

Exam TI2720-C/TI2725-C Embedded Software

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

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

ARM Cortex-M and RTOSs Are Meant for Each Other

Embedded System Curriculum

Designing Responsive and Real-Time Systems

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

OPERATING SYSTEM. PREPARED BY : DHAVAL R. PATEL Page 1. Q.1 Explain Memory

EECS 482 Introduction to Operating Systems

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Lecture 5: Synchronization w/locks

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Nios II. uc/os-ii porting with Nios II Altera Corporation

Lecture 2: September 9

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Embedding OS in AVR microcontrollers. Prof. Prabhat Ranjan DA-IICT, Gandhinagar

Process Scheduling Part 2

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

CS 326: Operating Systems. CPU Scheduling. Lecture 6

CS 326: Operating Systems. Process Execution. Lecture 5

ELC4438: Embedded System Design ARM Cortex-M Architecture II

Inter-Process Communication and Synchronization of Processes, Threads and Tasks: Lesson-1: PROCESS

What s in a process?

GLOSSARY. VisualDSP++ Kernel (VDK) User s Guide B-1

Processes and Non-Preemptive Scheduling. Otto J. Anshus

ò Paper reading assigned for next Tuesday ò Understand low-level building blocks of a scheduler User Kernel ò Understand competing policy goals

CSE 153 Design of Operating Systems Fall 2018

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

CSCI 447 Operating Systems Filip Jagodzinski

238P: Operating Systems. Lecture 14: Process scheduling

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Chapter 09. Programming in Assembly

CS370 Operating Systems

Task Based Programming Revisited Real Time Operating Systems

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

Introduction to Embedded Systems. Lab Logistics

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CSCE Introduction to Computer Systems Spring 2019

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Module 1. Introduction:

Process- Concept &Process Scheduling OPERATING SYSTEMS

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Sistemi in Tempo Reale

CSE 410 Final Exam 6/09/09. Suppose we have a memory and a direct-mapped cache with the following characteristics.

10/10/ Gribble, Lazowska, Levy, Zahorjan 2. 10/10/ Gribble, Lazowska, Levy, Zahorjan 4

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

What is an Operating System? A Whirlwind Tour of Operating Systems. How did OS evolve? How did OS evolve?

Process Description and Control. Chapter 3

Threads and Concurrency

The Kernel Abstraction

Threads and Concurrency

Programming in Real-Time OS (uc/os-ii)

CSC369 Lecture 2. Larry Zhang

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Multiprocessor and Real- Time Scheduling. Chapter 10

MARUTHI SCHOOL OF BANKING (MSB)

CSC 2405: Computer Systems II

BASICS OF THE RENESAS SYNERGY PLATFORM

Transcription:

Preemptive Scheduling Lecture 18 18-1

Big Picture Methods learned so far We ve been using a foreground/background system Interrupt service routines run in foreground Task code runs in background Limitations Must structure task functions to run to completion, regardless of natural program structure can only yield processor at end of task Response time of task code is not easily controlled, in worst case depends on how long each other task takes to run What we will learn next How to share processor flexibly among multiple tasks, while not requiring restructuring of code Goal: share MCU efficiently : To simplify our program design by allowing us to partition design into multiple independent components PCs/Workstations/Servers: To allow multiple users to share a computer system 18-2

Example: Secure Answering Machine (SAM) FLASH MEMORY ARRAY PAGE SIZE = BUFFER SIZE BUFFER 1 BUFFER 2 I/O INTERFACE SI SO SCK Testing the limits of our cooperative round-robin scheduler Secure Answering Machine Stores encrypted voice messages in serial Flash memory Want to delete messages fully, not just remove entry from directory (as with file systems for PCs) Also have a user interface: LCD, switches 18-3

SAM Delete Function and Timing "#$ #&'(),'--./0/1 # 2,'--34/563 35 2 / 7 8,,49:;/ 98,, > 8,, 8,, 749:;/ 7 8,, @ 8,, 7 @ 2! *!!()!!! <)!!!,,, =? <! $ A)! <! 18-4

Cooperative RR Scheduler? Since task must Run To Completion The delete function could take up to five seconds to run, halting all other tasks (but interrupts run) Other software needs to keep running, so break this into pieces. Run one piece at a time. How to split? Each piece ends where processor waits for user (e.g. debounced switch) or other devices (Flash, LCD). How to control execution of pieces? 1. Use a task per piece, use calls to Reschedule_Task and Disable_Task as needed Need 13 different tasks (12 shown here) 2. Use a state machine within one task LCD( Cancelled ) LCD( Are You Sure? ) get_debounced _switch(&k, 5); switch on k LCD( Erasing ) Flash_to_Buffer(DIR_PAGE) Read_Buffer(dir) // Find offsets // erase dir. entry Write_to_Buffer(dir); Buffer_to_Flash(DIR_PAGE) Flash_to_Buffer(data_page) // Overwrite msg in buffer Buffer_to_Flash(data_page); LCD( Timed Out ) 18-5

State Machine in One Task switch(cur_state) 1: LCD( Are You Sure? ); cur_state = 2; 2: if (LCD_Done) { get_debounced _switch(&k, 5); cur_state = 3; } 3: if (debounce_done) { if (k == CANCEL_KEY) { LCD( Cancelled ); cur_state = 99; } else if (k == TIMEOUT) { LCD( Timed Out ); cur_state = 99; } else { LCD( Erasing ); cur_state = 4; } } 4: if (LCD_Done) { Flash_to_Buffer( DIR_PAGE); cur_state = 5; } 5: if (Flash_done) { Read_Buffer( dir); cur_state = 6; } return 18-6

Daydreaming Some functions are causing trouble for us they use slow devices which make the processor wait LCD: controller chip on LCD is slow DataFlash: it takes time to program Flash EEPROM Switch debouncing: physical characteristics of switch, time-outs Wouldn t it be great if we could Make those slow functions yield the processor to other tasks? Not have the processor start running that code again until the device is ready? Maybe even have the processor interrupt less-important tasks? Avoid breaking up one task into many tasks, or a state machine? Open ourselves up to a whole new species of bugs, bugs which are very hard to duplicate and track down? 18-7

Preemptive Scheduling Kernel What we need is a kernel Shares the processor among multiple concurrently running tasks/threads/processes Can forcibly switch the processor from thread A to B and resume B later (preemption) Can resume threads when their data is ready Can simplify inter-thread communication by providing mechanisms The heart of any operating system Terminology: Kernel Mode PCs and workstations don t expose all of the machine to the user s program Only code in kernel or supervisor mode have full access Some high-end embedded processors have a restricted mode (e.g. ARM, MIPS) 18-8

Operating Systems (for PCs and Workstations) Two perspectives Extended Machine top-down view (using abstractions) File System: make a magnetic platter, read/write head, spindle motor and head servo look like a hierarchical collection of directories containing files and other directories Virtual Memory: make a disk and 512 MB of RAM look like 4 GB of RAM Resource Manager bottom-up view Share access to resources Keep them from interfering Common PC/Workstation operating system features Process management share the processor Process synchronization and communication Memory management File management Protection Time management I/O device access For embedded systems, we care mostly about preemptive thread management sharing the processor 18-9

What Execution State Information Exists? A program, process or thread in execution which has state information Current instruction identified with program counter Call stack identified with stack pointer Arguments, local variables, return addresses, dynamic links Other CPU state Register values (anything which will be shared and could be affected by the other processes) general purpose registers, stack pointer, etc. Status flags (zero, carry, interrupts enabled, carry bit, etc.) Other information as well Open files, memory management info, process number, scheduling information Ignore for now 0x0000!#$!"#! 0xFFFF 18-10

Processes vs. Threads Process No information is visible to other processes (nothing is shared) Thread Shares address space and code with other threads (also called lightweight process) One big side effect: context switching time varies Switching among processes requires swapping large amounts of information Switching among threads requires swapping much less information (PC, stack pointer and other registers, CPU state) and is much faster For this discussion, concepts apply equally to threads and processes 18-11

Maintaining State for Multiple Threads Store this thread-related information in a task/thread control block (TCB) process control block = PCB Shuffling information between CPU and multiple TCBs lets us share processor Consider case of switching from thread A to thread B Assume we have a call stack for each thread Assume we can share global variables among the two threads Standard for threads For M16C architecture, SB register is same for both threads 0x0000 #$ #$!"#! 0xFFFF 18-12

Step 1. Copy CPU State into TCB A CPU is initially executing task A, so save this information in TCB A 0x0000 #$ #$!"#! 0xFFFF 18-13

Step 2. Reload Old CPU State from TCB B Reloading a previously saved state configures the CPU to execute task B from where it left off This context switching is performed by the dispatcher code Dispatcher is typically written in assembly language to gain access to registers not visible to C programmer 0x0000 #$ #$!"#! 0xFFFF 18-14

Thread States Now that we can share the CPU, let s do it! Define five possible states for a thread to be in New just created, but not running yet Running instructions are being executed (only one thread can be running at a time!) Waiting/Blocking thread is waiting for an event to occur Ready process is not waiting but not running yet (is a candidate for running) Terminated process will run no more What the task needs happens Waiting Task needs something to happen This is highest priority ready task New Ready Running Terminated This isn t highest priority ready task 18-15

Thread Queues Create a queue for each state (except running) Now we can store thread control blocks in the appropriate queues Kernel moves tasks among queues/processor registers as needed What the task needs happens Waiting Waiting Task needs something to happen This is highest priority ready task New Ready Running This isn t highest priority ready task Terminated 18-16

Example Dispatcher Code Use interrupt to trigger a context switch Timer tick Break instruction Recall the interrupt sequence of activities Clear request bit of the active interrupt @, #!( (<( B!(,"(@ @# #, #@#, 2383 Save FLG in temporary register in CPU Clear flags in FLG: I (interrupt enable), D (debug flag), and U (stack pointer select) Push temporary register (holding old FLG) onto stack Save PC (20 bits) on stack & & &( &( &( & ' ' 18-17

Example Dispatcher Code to Save Context @=$! =$#38(! =$9!(!C!D =$9 ( C!D =$9<(<C!D =$9B(BC!D @@=$9! =$9!(!C!D =$ ( C!D =$78(,"C!D @@=$@#C!D @@=",C!D @@="@#,C!D =$4E:(@C!D,! @ #38 9! 9 9< 9B,!," $$,:, #' $",,, #' @@"",:, #'@ ($#$ $###$ 18-18

Restore Context =$$38(! @ $38 =$!C!D(9! 9! =$ C!D(9 9 =$<C!D(9< 9< =$BC!D(9B 9B =$ C!D( =$,"C!D(78 78 =$@C!D(4E: E: @="@#,C!D "",7;: @=",C!D $",7; @=$@#C!D $",: =$!C!D(!,!, @ =3$:7;, #' 18-19

Thread State Control Use OS scheduler to keep track of threads and their states For each state, OS keeps a queue of TCBs for all processes in that state Moves TCBs between queues as thread state changes OS s scheduler chooses among Ready threads for execution based on priority Scheduling Rules Only the thread itself can decide it should be waiting (blocked) A waiting thread never gets the CPU. It must be signaled by an ISR or another thread. Only the scheduler moves tasks between ready and running What changes the state of a thread? The OS receives a timer tick which forces it to decide what to run next The thread voluntarily yields control The thread requests information which isn t ready yet 18-20

Overview of Data Structures for Scheduler )& )", "!- #" " #!!.! - & # &" *!$!&# " 0 ' / Add Next, Prev pointers in each TCB to make it part of a doubly linked list Keep track of all TCBs Create a pointer for each queue: Ready, Wait, New Create a pointer for the currently running task s TCB 18-21

Example: Context Switch Thread A is running, and scheduler decides to run thread C instead. For example, thread A is still able to run, but has lower priority than thread C. Start by copying CPU state into TCB A )& )", " 0 ' / 18-22

Example: Context Switch Insert TCB A into ready queue by modifying appropriate pointers )& )" ", 0 ' / 18-23

Example: Context Switch Remove thread C from the ready queue and mark it as the thread to run next )& )" ", 0 ' / 18-24

Example: Context Switch Copy thread C s state information back into the CPU and resume execution )& )" ", 0 ' / 18-25

uc/os-ii Real-time kernel Portable, scalable, preemptive RTOS Ported to over 90 processors Pronounced microc OS two Written by Jean J. Labrosse of Micrium, http://ucos-ii.com Implementation is different from material just presented for performance and feature reasons CPU state is stored on thread s own stack, not TCB TCB keeps track of boundaries of stack space TCB also tracks events and messages and time delays 18-26

TCB for uc/os-ii @, # #" 5EE30F5E38E ': F: # @, #'F F5E38/ : F:,",38 F 5EE30F5E38E '8 F: ", #'G F 4.3B<65E38E 'EHFEH, ' #'" F 4.3?65E385@ F3'@ @" 5E3' / F 4.3?65E384F3'4!==?))B)F # #"F5E38. F: 38 38 F # #"F5E38:F: @38 F 5E/I/.3F5E38/ : F: # "#'F F5E38F#,5E": 5EJ: F 4.3?65E38F." #' '( $, F 4.3K65E38E F3' F 4.3K65E38:F3'@!-- (?B--$ F 855/.5E389LF4# $ ',F 25E38 18-27

Data Structures for uc/os-ii OSTCBCur - Pointer to TCB of currently running task OSTCBHighRdy - Pointer to highest priority TCB ready to run OSTCBList - Pointer to doubly linked list of TCBs OSTCBPrioTbl[OS_LOWEST_ PRIO 1] - Table of pointers to created TCBs, ordered by priority OSReadyTbl - Encoded table of tasks ready to run OSPrioCur Current task priority OSPrioHighRdy Priority of highest ready task OSTCBFreeList - List of free OS_TCBs, use for creating new tasks 3 5 12 13 14 1 1 18-28

Dispatcher for uc/os-ii 5E E$A :6EM 9!(9 (9<(9B(!( (E8(78 5I=> 5E38(! 5E38NO5E38E ': -E #'@ E3 4E:(C!D,"5E3'E$M' PE9 5E3'E$M' 5E38-5E38M9 5I=> 5E38M9(5E38 5E:-5E:M9 5I=> 5E:M9(5E: E #': -5E38M9NO5E38E ': 5I=> 5E38M9(! C!D(4E: 9 @#, $ 'Q #' :5: 9!(9 (9<(9B(!( (E8(78 9/43 18-29

Preemptive vs. Non-Preemptive Non-preemptive kernel/cooperative multitasking Each task must explicitly give up control of CPU E.g. return from task code, call yield function Asynchronous events are handled by ISRs ISR always returns to interrupted task Can use non-reentrant code (covered later) Task level response time can be slower as slowest task must complete Generally don t need semaphores Preemptive kernel At each scheduling point, the highest priority task ready to run is given CPU control If a higher priority task becomes ready, the currently running task is suspended and moved to the ready queue Maximum response time is less than in non-preemptive system Non-reentrant code should not be used Shared data typically needs semaphores 18-30