The Simple Scheduler in Embedded System @ OSDC.TW 2014
1. The Simple Scheduler in
Embedded System
A simple scheduler module implemented in C
StarNight @ OSDC.TW 2014
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. Outline
● History
● OS concepts learned from textbooks
● Process & Scheduler
● What if …
● Simple Scheduler
● Simple Oscilloscope
5. There is a robot in every boy’s mind ~
每個男孩心中,
都有一個機器人~
6. There is an operating system in every
computer scientist’s mind ~
上了大學之後,
同學告訴我每位資工人的
心中都有個 OS OS ~
7. However, I am graduated from
department of mechanical engineering ~
但我是機械系畢業的~
8. For the expectation in my childhood, not only
I attended the required classes, but also I sat
in the operating system class ~
為了男孩心中的機器人
所以除了本科外,我旁聽 ~
11. My First Impression of OS
Operating
System
Input Output
keyboard
mouse
microphone
touchpad
comunication (in)
...
screen
headset
comunication (out)
...
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)
...
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. 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. 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. 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. Scheduling Algorithms
● First-Come, First-Served (or FIFO)
● Round Robin
● Shortest Job First
● Shortest Remaining Time First
● Priority Scheduling
● Multilevel Queues
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. 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. 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. 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. 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. 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. You can have it from
GitHub → StarNight → simple scheduler
https://github.com/starnight/simple-scheduler
PS. Wiki included
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. Job’s State in Simple Scheduler
new
ready
terminated
running
scheduler dispatch
Ready Queue
admitted exit
Do scheduling
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. /* 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. 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. 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. 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()
61. Fill Modules into Architecture
Simple Scheduler
ADC
USART
SysTimer
ADCSample
Process
Kernel
User
Timer
USARTCommunicate
MODBUS - like
Platform
Dependent
Drivers
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. 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:
■ ...
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
73. Diagram showing CPU switch from
process to process
Reference: Figure 3.4 of Operating System Concept, 8th
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. Job’s State in Simple Scheduler
new
ready
terminated
running
admitted exit
scheduler dispatch
Ready Queue
SSAdmitJob
SSMainLoop
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