Upload
chien-hung-pan
View
1.864
Download
0
Embed Size (px)
DESCRIPTION
https://github.com/starnight/simple-scheduler Video: https://www.youtube.com/watch?v=dm18UEsgOow
Citation preview
The Simple Scheduler in Embedded System
A simple scheduler module implemented in C
StarNight @ OSDC.TW 2014
Who am I?
潘建宏 / Jian-Hong Pan (StarNight)About Me : http://about.me/StarNight
出沒在~
GitHub : starnightPTT : zack2004plurk : StarNightFacebook : Jian-Hong Pan
目前繼續在種花店當個打雜園丁 ~
Outline
● History
● OS concepts learned from textbooks
● Process & Scheduler
● What if …
● Simple Scheduler
● Simple Oscilloscope
很久很久以前~
Long Long time ago ~
很久很久以前~
Long Long time ago ~
There is a robot in every boy’s mind ~
每個男孩心中,都有一個機器人~
There is an operating system in every computer scientist’s mind ~
上了大學之後,同學告訴我每位資工人的心中都有個 OS OS ~
However, I am graduated fromdepartment of mechanical engineering ~
但我是機械系畢業的~
For the expectation in my childhood, not only I attended the required classes, but also I sat
in the operating system class ~
為了男孩心中的機器人所以除了本科外,我旁聽 ~
上課也可以很熱血!!!
Reference from internet
欠的總是要還~
其實就是因為旁聽沒繳作業,所以畢業三年多後,
要補寫作業跟老師謝罪 XD
My First Impression of OS
Operating System
Input Output
keyboardmousemicrophonetouchpadcomunication (in)...
screenheadsetcomunication (out)...
There could be an OS in a robot, too!
Operating System
Input Output
GPIsensorscommand (in)comunication (in)...
GPOmotorscommand (out)comunication (out)...
Thinking furthermore
Apps
Operating System
Input Output
drivers scheduler etc ...
ETC
ready queue
data structure
Simple Data Structure @lightning talk COSCUP 2013
Data
Text
Heap
Stackmax
base 0
Process in Memory
Temporary data
Dynamic allocated memoryGlobal variables
Program code
Reference: Figure 3.1 of Operating System Concept, 8th
Usual Diagram of Process State
new
ready
terminated
running
Waiting
admitted exitinterrupt
scheduler dispatchI/O or event waitI/O or event
completion
Reference: Figure 3.2 of Operating System Concept, 8th
Process Control Block (PCB)
Process State
Process Number
Program Counter
Registers
Memory Limits
List of Open Files
Reference: Figure 3.3 of Operating System Concept, 8th
Scheduling when 4 Events Occurs
new
ready
terminated
running
Waiting
admitted exitinterrupt
scheduler dispatchI/O or event waitI/O or event
completion
Reference: Figure 3.2 of Operating System Concept, 8th
1
2
34
For Multitasking
For Multiprogramming
Scheduling Algorithms
● First-Come, First-Served (or FIFO)
● Round Robin
● Shortest Job First
● Shortest Remaining Time First
● Priority Scheduling
● Multilevel Queues
What if ...
● The scheduled job (process) is as small & simple as a function, even is a function.
● Because of being small, the job will not be interrupted by timeout.
● Because of being simple, the job will not wait for the I/O or event. There is another job for I/O or event completion.
Running Waiting Ready
BeforeI/O or event
DuringI/O or event
AfterI/O or event
Compare Jobs’ Status Changing
Job #1 Job #2 Job #3
General
SimpleScheduler
TimeToDoAsync DoAsync AfterAsync
In other words
● Break a single process into several jobs.
● Each job will not be interrupted.
● A job terminate immediately if it is finished.
● Do not save job’s state during context switching.
This is not new idea
Related concepts:
● Functional programming
● Asynchronous function in Javascript
● Events of GUI programs
● Function pointer in C
ToDoAsyncFunc(pArgument, pAfterFunc)
How do it keep the jobs until they are executed?
Save the jobs in somewhere,
maybe a queue.
How about make it like the ready queueof scheduler !?!?
What is Simple Scheduler
● Simple Scheduler does "First In, First Out" (FIFO) scheduling works with no priority and non-preemptive scheduling scheme.
● It is the "Functions", which could also be called callback functions or jobs, that Simple Scheduler schedules.
You can have it from
GitHub → StarNight → simple scheduler
https://github.com/starnight/simple-scheduler
PS. Wiki included
Job’s State in Simple Scheduler
new
ready
terminated
running
admitted exit
scheduler dispatch
Ready Queue
interrupt
scheduler dispatch
Waiting
I/O or event waitI/O or eventcompletion
Small & non-preemptive
Simple & non-blocking I/O
Job’s State in Simple Scheduler
new
ready
terminated
running
scheduler dispatch
Ready Queue
admitted exitDo scheduling
Timing Diagram
Job #1 Job #2
Ready Queue:First In, First Out (FIFO)
Timing Diagram
Job #1 Job #2
Ready Queue:
Job #1
Timing Diagram
Job #1 Job #2
Ready Queue:
Job #1 admits one Job #2Job #2
Job #2
Timing Diagram
Job #2 Job #2
Ready Queue:
Timing Diagram
Job #2 Job #2
Ready Queue:
Job #2
Timing Diagram
Job #2
Ready Queue:
Timing Diagram
Job #2
Ready Queue:
An interrupt occurs during scheduling
Timing Diagram
Job #2
Ready Queue:
The interrupt admits one Job #3
Job #3
Job #3
Timing Diagram
Job #2
Ready Queue:
Job #3
Timing Diagram
Job #2 Job #3
Ready Queue:
Job #2
Timing Diagram
Job #2
Ready Queue:
Job #3
Another interrupt occursduring Job #2 executing
Timing Diagram
Job #2
Ready Queue:
Job #3
The interrupt admits one Job #1
Job #1
Job #1
Timing Diagram
Job #2 Job #1
Ready Queue:
Job #3
Timing Diagram
Job #3
Ready Queue:
Job #1
Timing Diagram
Job #1
Ready Queue:
Job #3
Job #3
Timing Diagram
Ready Queue:
Job #1
Timing Diagram
Ready Queue:
Job #1
Job #1
Timing Diagram
Job #1
Ready Queue:
Job #1 admitsone Job #2
Job #2
Job #2
Timing Diagram
Job #2
Ready Queue:
Timing Diagram
Job #2
Ready Queue:
Job #2
Timing Diagram
Ready Queue:
Timing Diagram
Ready Queue:
When does it admit job?
● Booting (Before scheduler start to run)
● A Job is running
● During interrupt
哥schedule的不是Process, 是Function
It is the functions, not the processes, that simple scheduler schedules.
typedef void (*SS_CB)(void *);
● The job could be pass one parameter's pointer or NULL representing no parameter.
● The pointer of the parameter could be a native variable pointer or even a struture pointer.
Prototype of the Scheduled Function
/* Packaged callback function (PCB). */typedef struct _SS_PACKAGED_CALLBACK { SS_CB cb; void *p;} SS_PCB;
PCB Structure Type
● cb : The callback function pointer of the job.
● p : The argument pointer going to be passed into the job.
Ready Queue
PCB of Job #3
PCB of Job #2
PCB of Job #1
FI
FO
PCB vs PCB
Process State
Process Number
Program Counter
Registers
Memory Limits
List of Open Files
SS_CB cb
Callback function pointer
void *p
Parameter pointerfor the callback function
Original PCB Simple Scheduler PCB
Public Functions
● SSInit : Initial the scheduler.
● SSAdmitJob : Admit a job (callback function) into the ready queue.
● SSMainLoop : Main loop for system scheduling.
● SSBreak : Break scheduling.
● SSConsumeLeft : Consume left jobs in ready queue.
● SSDebug : Have the debug information of the scheduler.
SSBreak(BC)
SSBreak(B)
Scheduler’s State in Simple Scheduler
Boot
Run
Break & Consume Left
SSConsumeLeft(n) orSSBreak(BC)
SSBreak(B)Break
B: SS_BREAKSCHEDULINGBC: SS_BREAKANDCONSUMELEFT
SSMainLoop()
有了
Simple Scheduler
那就用用看吧!
ADC & UART & Timer Labs+
Scheduler
實作一個窮到只剩下 $$ Scheduler的OS kernel
Simple OS for Simple Oscilloscope
https://github.com/starnight/Simple-Oscilloscope
樸實無華 X 極簡
As simple as possible ~
Architecture
Scheduler
Direvers Timer
App #1 App #N
Kernel
User
Fill Modules into Architecture
Simple Scheduler
ADC USART
SysTimer
ADCSample
Process
Kernel
User
Timer
USARTCommunicate
MODBUS - like
PlatformDependentDrivers
Usage of Simple Scheduler
● SSInit()
● Admit leading jobs, ex: Wait command job.
● SSMainLoop()
● Interrupts admit jobs○ USART RX:
■ Admits a job when predefined RX queue is full.
○ USART TX: ■ Admits a job when predefined TX queue is empty.
Usage of Simple Scheduler (Cont.)
● Jobs admit jobs (Ex: Modbus-like package)○ Wait Commad:
■ Admits a wait command job if there is no command.■ Admits a check station job if there is a command.
○ Check station job:■ Admits a wait command job if not matched address.■ Parses commad and admits a corresponding job.
○ Corresponding job:■ ...
Simple Oscilloscpe - MCU Side
Simple Oscilloscpe - Wave Gen. Side
MCU SideATMega328P
Wave Generator Side
Oscilloscope Console by python + pyserial + matplotlib
畢業之後,
還可以重新仔細回味校園裡所學,
並將這些知識融合做出成果,
不也是一種 小確幸 ~
● MVMC & ACAL Labs @ NCU○ 老師們與歷屆學長、同學和學弟們
● 作業系統Operating Systems on Share Course
● 我旁聽的作業系統課 @ NCU
● Textbook: Operating System Concepts
● Open Source Groups
Thanks to
Thank you ~ and Q & A
GitHub → starnight → simple-scheduler
Between Running and Ready State
ready running
interrupt
scheduler dispatch
Ready * NRunning Running
Job’s state according to time:
Time
Interrupted by timeoutFor Multitasking
The Waiting State
Waiting
I/Oevent
waitI/Oevent
completion
WaitingRunning Ready
Job’s state according to time:
Time
For Multiprogramming
Diagram showing CPU switch from process to process
Reference: Figure 3.4 of Operating System Concept, 8th
● SS_READYQUEUEOK
○ The ready queue works fine.
● SS_READYQUEUEEMPTY
○ The ready queue is empty.
● SS_READYQUEUEFULL
○ The ready queue is full.
Ready Queue StatusReady Queue
PCB of Job #3
PCB of Job #2
PCB of Job #1
FI
FO
Job’s State in Simple Scheduler
new
ready
terminated
running
admitted exit
scheduler dispatch
Ready Queue
SSAdmitJob
SSMainLoop
Scheduler Status
● SS_RUNSCHEDULING○ The scheduler is running.
● SS_BREAKSCHEDULING○ The scheduler is or is going to be broken into stop.
● SS_BREAKANDCONSUMELEFT○ The scheduler is or is going to be broken into stop
and consume the left jobs in ready queue.
Represented by SS_SSTATUS → run
Recall thinking furthermore
Apps
Operating System
Input Output
drivers scheduler etc ...
ETC
ready queue
data structure