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 : starnight
PTT : zack2004
plur...
Outline
● History
● OS concepts learned from textbooks
● Process & Scheduler
● What if …
● Simple Scheduler
● Simple Oscil...
很久很久以前~
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 from
department of mechanical engineering ~
但我是機械系畢業的~
For the expectation in my childhood, not only
I attended the required classes, but also I sat
in the operating system clas...
上課也可
以很熱血
!!!
Reference from internet
欠的總是要還~
其實就是因為旁聽沒繳作業,
所以畢業三年多後,
要補寫作業跟老師謝罪 XD
My First Impression of OS
Operating
System
Input Output
keyboard
mouse
microphone
touchpad
comunication (in)
...
screen
he...
There could be an OS in a robot, too!
Operating
System
Input Output
GPI
sensors
command (in)
comunication (in)
...
GPO
mot...
Thinking furthermore
Apps
Operating
System
Input Output
drivers scheduler etc ...
ETC
ready
queue
data
structure
Simple Da...
Data
Text
Heap
Stack
max
base 0
Process in Memory
Temporary data
Dynamic allocated
memory
Global variables
Program code
Re...
Usual Diagram of Process State
new
ready
terminated
running
Waiting
admitted exit
interrupt
scheduler dispatch
I/O or even...
Process Control Block (PCB)
Process State
Process Number
Program Counter
Registers
Memory Limits
List of Open Files
Refere...
Scheduling when 4 Events Occurs
new
ready
terminated
running
Waiting
admitted exit
interrupt
scheduler dispatch
I/O or eve...
Scheduling Algorithms
● First-Come, First-Served (or FIFO)
● Round Robin
● Shortest Job First
● Shortest Remaining Time Fi...
What if ...
● The scheduled job (process) is as small &
simple as a function, even is a function.
● Because of being small...
Running Waiting Ready
Before
I/O or event
During
I/O or event
After
I/O or event
Compare Jobs’ Status Changing
Job #1 Job ...
In other words
● Break a single process into several jobs.
● Each job will not be interrupted.
● A job terminate immediate...
This is not new idea
Related concepts:
● Functional programming
● Asynchronous function in Javascript
● Events of GUI prog...
How do it keep the jobs until they
are executed?
Save the jobs in somewhere,
maybe a queue.
How about make it like the rea...
What is Simple Scheduler
● Simple Scheduler does "First In, First Out"
(FIFO) scheduling works with no priority and
non-pr...
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
schedu...
Job’s State in Simple Scheduler
new
ready
terminated
running
scheduler dispatch
Ready Queue
admitted exit
Do 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 occurs
during 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 admits
one 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 po...
/* Packaged callback function (PCB). */
typedef
struct _SS_PACKAGED_CALLBACK {
SS_CB cb;
void *p;
} SS_PCB;
PCB Structure ...
PCB vs PCB
Process State
Process Number
Program Counter
Registers
Memory Limits
List of Open Files
SS_CB cb
Callback funct...
Public Functions
● SSInit : Initial the scheduler.
● SSAdmitJob : Admit a job (callback function) into
the ready queue.
● ...
SSBreak(BC)
SSBreak(B)
Scheduler’s State in Simple Scheduler
Boot
Run
Break &
Consume Left
SSConsumeLeft(n) or
SSBreak(BC)...
有了
Simple Scheduler
那就用用看吧!
ADC & UART & Timer Labs
+
Scheduler
實作一個窮到只剩下 $$
Scheduler的OS kernel
Simple OS for Simple Oscilloscope
https://github.com/...
樸實無華 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
MO...
Usage of Simple Scheduler
● SSInit()
● Admit leading jobs, ex: Wait command job.
● SSMainLoop()
● Interrupts admit jobs
○ ...
Usage of Simple Scheduler (Cont.)
● Jobs admit jobs (Ex: Modbus-like package)
○ Wait Commad:
■ Admits a wait command job i...
Simple Oscilloscpe - MCU Side
Simple Oscilloscpe - Wave Gen. Side
MCU Side
ATMega328P
Wave Generator Side
Oscilloscope Console by python + pyserial + matplotlib
畢業之後,
還可以重新仔細回味校園裡所學,
並將這些知識融合做出成果,
不也是一種 小確幸 ~
● MVMC & ACAL Labs @ NCU
○ 老師們與歷屆學長、同學和學弟們
● 作業系統Operating Systems on Share Course
● 我旁聽的作業系統課 @ NCU
● Textbook: Operating...
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 ...
The Waiting State
Waiting
I/O
event
waitI/O
event
completion
WaitingRunning Ready
Job’s state according to time:
Time
For ...
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...
Job’s State in Simple Scheduler
new
ready
terminated
running
admitted exit
scheduler dispatch
Ready Queue
SSAdmitJob
SSMai...
Scheduler Status
● SS_RUNSCHEDULING
○ The scheduler is running.
● SS_BREAKSCHEDULING
○ The scheduler is or is going to be ...
Recall thinking furthermore
Apps
Operating
System
Input Output
drivers scheduler etc ...
ETC
ready
queue
data
structure
Upcoming SlideShare
Loading in...5
×

The Simple Scheduler in Embedded System @ OSDC.TW 2014

2,356

Published on

https://github.com/starnight/simple-scheduler
Video: https://www.youtube.com/watch?v=dm18UEsgOow

Published in: Technology
2 Comments
13 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,356
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
41
Comments
2
Likes
13
Embeds 0
No embeds

No notes for slide

The Simple Scheduler in Embedded System @ OSDC.TW 2014

  1. 1. The Simple Scheduler in Embedded System A simple scheduler module implemented in C StarNight @ OSDC.TW 2014
  2. 2. Who am I? 潘建宏 / Jian-Hong Pan (StarNight) About Me : http://about.me/StarNight 出沒在~ GitHub : starnight PTT : zack2004 plurk : StarNight Facebook : Jian-Hong Pan 目前繼續在種花店當個打雜園丁 ~
  3. 3. Outline ● History ● OS concepts learned from textbooks ● Process & Scheduler ● What if … ● Simple Scheduler ● Simple Oscilloscope
  4. 4. 很久很久以前~ Long Long time ago ~ 很久很久以前~ Long Long time ago ~
  5. 5. There is a robot in every boy’s mind ~ 每個男孩心中, 都有一個機器人~
  6. 6. There is an operating system in every computer scientist’s mind ~ 上了大學之後, 同學告訴我每位資工人的 心中都有個 OS OS ~
  7. 7. However, I am graduated from department of mechanical engineering ~ 但我是機械系畢業的~
  8. 8. For the expectation in my childhood, not only I attended the required classes, but also I sat in the operating system class ~ 為了男孩心中的機器人 所以除了本科外,我旁聽 ~
  9. 9. 上課也可 以很熱血 !!! Reference from internet
  10. 10. 欠的總是要還~ 其實就是因為旁聽沒繳作業, 所以畢業三年多後, 要補寫作業跟老師謝罪 XD
  11. 11. My First Impression of OS Operating System Input Output keyboard mouse microphone touchpad comunication (in) ... screen headset comunication (out) ...
  12. 12. There could be an OS in a robot, too! Operating System Input Output GPI sensors command (in) comunication (in) ... GPO motors command (out) comunication (out) ...
  13. 13. Thinking furthermore Apps Operating System Input Output drivers scheduler etc ... ETC ready queue data structure Simple Data Structure @ lightning talk COSCUP 2013
  14. 14. Data Text Heap Stack max base 0 Process in Memory Temporary data Dynamic allocated memory Global variables Program code Reference: Figure 3.1 of Operating System Concept, 8th
  15. 15. Usual Diagram of Process State new ready terminated running Waiting admitted exit interrupt scheduler dispatch I/O or event wait I/O or event completion Reference: Figure 3.2 of Operating System Concept, 8th
  16. 16. 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
  17. 17. Scheduling when 4 Events Occurs new ready terminated running Waiting admitted exit interrupt scheduler dispatch I/O or event wait I/O or event completion Reference: Figure 3.2 of Operating System Concept, 8th 1 2 34 For Multitasking For Multiprogramming
  18. 18. Scheduling Algorithms ● First-Come, First-Served (or FIFO) ● Round Robin ● Shortest Job First ● Shortest Remaining Time First ● Priority Scheduling ● Multilevel Queues
  19. 19. 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.
  20. 20. Running Waiting Ready Before I/O or event During I/O or event After I/O or event Compare Jobs’ Status Changing Job #1 Job #2 Job #3 General Simple Scheduler Time ToDoAsync DoAsync AfterAsync
  21. 21. 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.
  22. 22. This is not new idea Related concepts: ● Functional programming ● Asynchronous function in Javascript ● Events of GUI programs ● Function pointer in C ToDoAsyncFunc(pArgument, pAfterFunc)
  23. 23. 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 queue of scheduler !?!?
  24. 24. 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.
  25. 25. You can have it from GitHub → StarNight → simple scheduler https://github.com/starnight/simple-scheduler PS. Wiki included
  26. 26. Job’s State in Simple Scheduler new ready terminated running admitted exit scheduler dispatch Ready Queue interrupt scheduler dispatch Waiting I/O or event wait I/O or event completion Small & non-preemptive Simple & non-blocking I/O
  27. 27. Job’s State in Simple Scheduler new ready terminated running scheduler dispatch Ready Queue admitted exit Do scheduling
  28. 28. Timing Diagram Job #1 Job #2 Ready Queue: First In, First Out (FIFO)
  29. 29. Timing Diagram Job #1 Job #2 Ready Queue: Job #1
  30. 30. Timing Diagram Job #1 Job #2 Ready Queue: Job #1 admits one Job #2Job #2 Job #2
  31. 31. Timing Diagram Job #2 Job #2 Ready Queue:
  32. 32. Timing Diagram Job #2 Job #2 Ready Queue: Job #2
  33. 33. Timing Diagram Job #2 Ready Queue:
  34. 34. Timing Diagram Job #2 Ready Queue: An interrupt occurs during scheduling
  35. 35. Timing Diagram Job #2 Ready Queue: The interrupt admits one Job #3 Job #3 Job #3
  36. 36. Timing Diagram Job #2 Ready Queue: Job #3
  37. 37. Timing Diagram Job #2 Job #3 Ready Queue: Job #2
  38. 38. Timing Diagram Job #2 Ready Queue: Job #3 Another interrupt occurs during Job #2 executing
  39. 39. Timing Diagram Job #2 Ready Queue: Job #3 The interrupt admits one Job #1 Job #1 Job #1
  40. 40. Timing Diagram Job #2 Job #1 Ready Queue: Job #3
  41. 41. Timing Diagram Job #3 Ready Queue: Job #1
  42. 42. Timing Diagram Job #1 Ready Queue: Job #3 Job #3
  43. 43. Timing Diagram Ready Queue: Job #1
  44. 44. Timing Diagram Ready Queue: Job #1 Job #1
  45. 45. Timing Diagram Job #1 Ready Queue: Job #1 admits one Job #2 Job #2 Job #2
  46. 46. Timing Diagram Job #2 Ready Queue:
  47. 47. Timing Diagram Job #2 Ready Queue: Job #2
  48. 48. Timing Diagram Ready Queue:
  49. 49. Timing Diagram Ready Queue:
  50. 50. When does it admit job? ● Booting (Before scheduler start to run) ● A Job is running ● During interrupt
  51. 51. 哥schedule的不是Process, 是Function It is the functions, not the processes, that simple scheduler schedules.
  52. 52. 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
  53. 53. /* 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
  54. 54. 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 pointer for the callback function Original PCB Simple Scheduler PCB
  55. 55. 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.
  56. 56. SSBreak(BC) SSBreak(B) Scheduler’s State in Simple Scheduler Boot Run Break & Consume Left SSConsumeLeft(n) or SSBreak(BC) SSBreak(B) Break B: SS_BREAKSCHEDULING BC: SS_BREAKANDCONSUMELEFT SSMainLoop()
  57. 57. 有了 Simple Scheduler 那就用用看吧!
  58. 58. ADC & UART & Timer Labs + Scheduler 實作一個窮到只剩下 $$ Scheduler的OS kernel Simple OS for Simple Oscilloscope https://github.com/starnight/Simple-Oscilloscope
  59. 59. 樸實無華 X 極簡 As simple as possible ~
  60. 60. Architecture Scheduler Direvers Timer App #1 App #N Kernel User
  61. 61. Fill Modules into Architecture Simple Scheduler ADC USART SysTimer ADCSample Process Kernel User Timer USARTCommunicate MODBUS - like Platform Dependent Drivers
  62. 62. 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.
  63. 63. 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: ■ ...
  64. 64. Simple Oscilloscpe - MCU Side
  65. 65. Simple Oscilloscpe - Wave Gen. Side
  66. 66. MCU Side ATMega328P Wave Generator Side
  67. 67. Oscilloscope Console by python + pyserial + matplotlib
  68. 68. 畢業之後, 還可以重新仔細回味校園裡所學, 並將這些知識融合做出成果, 不也是一種 小確幸 ~
  69. 69. ● MVMC & ACAL Labs @ NCU ○ 老師們與歷屆學長、同學和學弟們 ● 作業系統Operating Systems on Share Course ● 我旁聽的作業系統課 @ NCU ● Textbook: Operating System Concepts ● Open Source Groups Thanks to
  70. 70. Thank you ~ and Q & A GitHub → starnight → simple-scheduler
  71. 71. Between Running and Ready State ready running interrupt scheduler dispatch Ready * NRunning Running Job’s state according to time: Time Interrupted by timeout For Multitasking
  72. 72. The Waiting State Waiting I/O event waitI/O event completion WaitingRunning Ready Job’s state according to time: Time For Multiprogramming
  73. 73. Diagram showing CPU switch from process to process Reference: Figure 3.4 of Operating System Concept, 8th
  74. 74. ● SS_READYQUEUEOK ○ The ready queue works fine. ● SS_READYQUEUEEMPTY ○ The ready queue is empty. ● SS_READYQUEUEFULL ○ The ready queue is full. Ready Queue Status Ready Queue PCB of Job #3 PCB of Job #2 PCB of Job #1 FI FO
  75. 75. Job’s State in Simple Scheduler new ready terminated running admitted exit scheduler dispatch Ready Queue SSAdmitJob SSMainLoop
  76. 76. 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
  77. 77. Recall thinking furthermore Apps Operating System Input Output drivers scheduler etc ... ETC ready queue data structure
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×