Download presentation
Presentation is loading. Please wait.
Published byJodie Burke Modified over 9 years ago
1
MP2: RATE-MONOTONIC CPU SCHEDULING Based on slides by Gourav Khaneja, Raoul Rivas and Keun Yim University of Illinois at Urbana-Champaign Department of Computer Science CS423
2
G OAL Understand real-time scheduling concepts Design a real-time scheduler (i.e., RM) and its admission controller in OS Implement the RM scheduler and admission controller as a Linux kernel module Learn how to use the kernel-lv. API of the Linux scheduler, timer, and proc file system Test the kernel-level real-time scheduler by implementing a user-level periodic task CS423 MP2 2 Extending Linux kernel to support RM scheduler
3
R ATE M ONOTONIC S CHEDULER Periodic tasks to be completed before next period starts. Medical systems such as pace maker, airline navigation system CS423 MP2 3
4
/P ROC Uses the Proc file system as an interface instead of system calls Process Registers: provide pid, period, processing time Yield: Finished Processing. Sleep until the next period De-register CS423 MP2 4
5
C ONTEXT S WITCH Use Linux Scheduler for context switches through scheduling API and policies. CS423 MP2 5 Linux Scheduler User Space Proc FS Read Proc FS Write RMS Kernel Module Kernel Space Periodic App.
6
D ESIGN CS423 MP2 6 Process (or Task) Period Processing Time Deadline of Job 1 3. Job Arrival Job completion 4. Yield Kernel Space Proc FS Read Op. Proc FS Write Op. 2. List of RM Processes 1. Register Process Linked List for RM Tasks Dispatching Thread (High RT Priority) Timer RM Admission Controller Yield Func. Handler Linux Scheduler User- Level Periodic Process 4. Yield 5. Deregister 1. Register 2. Check wakeup() User Space … 5. Deregister
7
P ROCESS C ONTROL B LOCK PCB is defined by the task_struct structure PCBs are managed by a circular doubly linked list task_struct *current points to PCB of the currently running process. PCB contains the process state: Running or ready: TASK_RUNNING Waiting: TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE Other: TASK_TRACED, TASK_STOPPED CS423 MP2 7 task_struct Current
8
task_struct P ROCESS C ONTROL B LOCK Additionally PCB contains: priority: the static priority rt_priority: the real-time priority counter: the number of clock ticks left in the scheduling budget of the process policy: the scheduling policy ○ SCHED_NORMAL: Normal ○ SCHED_FIFO or SCHED_RR mm: the memory descriptor CS423 MP2 8 priority rt_priority counter policy state need_ resched
9
M P 2_ TASK _ STRUCT struct task_struct * task Int period Int processing_time Int task_state timer_list timer … CS423 MP2 9
10
S CHEDULING P OINTS schedule() is the entry point of the scheduler and invoked by: scheduler_tick() if a reschedule is marked. (task_struct→needs_resched) yield() is called by an application in user space schedule() in kernel or process context in kernel space Can schedule() be called inside interrupt handler? CS423 MP2 10 schedule() Timer 1/HZ add_timer(1 jiffy) jiffies++ scheduler_tick() tick_periodic Kernel Contextyield() Process Context
11
T WO H ALVES / D EFERRING W ORK Kernel timers are used to create timed events They use jiffies to measure the system time Timers are interrupts We can't sleep or hog CPU in them! Solution: Divide the work into two parts Uses the timer handler to signal a thread. (TOP HALF) Let the kernel thread do the real job. (BOTTOM HALF) CS423 MP2 11 Timer Timer Handler: wake_up(thread); Thread: While(1) { Do work(); Schedule(); } Interrupt context Kernel context TOP HALF BOTTOM HALF
12
S CHEDULER API schedule(): triggers rescheduling Runs after changing the state of any process wake_up_process(struct task_struct *): This can be called in the interrupt context. sched_setscheduler(): sets sched. parameters e.g., priority & scheduling policy set_current_state(): changes the state used to put the running context to sleep. set_task_state(): changes the state of another CS423 MP2 12
13
I MPLEMENTATION CS423 MP2 13 Select the “ready” process with shortest period. State = running Wake up process sleep State = ready Wake up scheduler Update timer State = sleep Wake up scheduler sleep Yield function handler Scheduler Timer interrupt
14
S CHEDULING H ACK Use Linux Scheduling policies CS423 MP2 14 struct sched_param sparam; wake_up_process(task): sparam.sched_priority=MAX_USER_RT_PRIO-1; sched_setscheduler(task, SCHED_FIFO, &sparam);
15
LOCKS Spinlocks inside timer interrupts spin_lock_irqsave(spinlock_t *, unsigned ) Disable interrupts: can safely implement spinlocks on single core machines spin_unlock_irqrestore(spinlock_t *, unsigned int) Mutex for linked list CS423 MP2 15
16
M EMORY CACHE Improves memory management by reducing fragmentation For frequent allocation/de-allocation of given objects Kmem_cache_create Kmem_cache_alloc Kmem_cache_free Kmem_cache_destroy CS423 MP2 16
17
A DMISSION C ONTROL CS423 MP2 17
18
T EST APPLICATION void main (void) { REGISTER(PID, Period, ProcessTime); //Proc filesystem list=READ STATUS(); //Proc filesystem: Verify the process was admitted if (!process in the list) exit 1; //setup everything needed for real-time loop: t0=gettimeofday() for test.c YIELD(PID); //Proc filesystem //this is the real-time loop while(exist jobs) { do_job(); //wakeup_time=t0-gettimeofday() and factorial computation YIELD(PID); //Proc filesystem } UNREGISTER(PID); //Proc filesystem } CS423 MP2 18
19
T ESTS Calculate the factorial of a fixed number Estimate approx. processing time Test with multiple applications Verify Pre-emption CS423 MP2 19
20
C ONCLUSION Linux uses Priority based Scheduling Priorities are adjusted based on the state of the process and its scheduling policy The PCB (task_struct) contains the information about the state of a process Rescheduling in Linux follows the Two-Halves approach. Reschedule can occur in process context or kernel context only. We can use the Scheduler API to build our own policy on top of the Linux scheduler. Our RMS scheduler will have a scheduler thread and will be invoked after a Job Release and after a Job Completion CS423 MP2 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.