1 / 20

제 39 강 : Scheduling

제 39 강 : Scheduling. Ch 4 Scheduling Linux supports realtime jobs Soft-Realtime For faster response, preempt task in kernel mode. Priority. Priority changes dynamically priority ++  CPU preempted for I/O? (I/O bound) priority --  CPU preempted while computing? (CPU-bound)

Download Presentation

제 39 강 : Scheduling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제39강 : Scheduling Ch 4 SchedulingLinux supports realtime jobsSoft-RealtimeFor faster response, preempt task in kernel mode

  2. Priority • Priority changes dynamically • priority++ CPU preempted for I/O? (I/O bound) • priority -- CPU preempted while computing? (CPU-bound) • Base priority (initial value) • Standard priority • Real-time priority • Who runs next?  Runnable (ready) process with remainingtimeslice highestpriority AND

  3. Lower priority or Higher priority or Less interactive More interactive Default 100ms Maximum 200ms Minimum 10ms Timeslice • Preempted?  preempted task can use remaining timeslice later Task is runnable(ready) iff timeslice remain • A task used all the timeslice? this task cannot run on CPU wait until all other tasks exhausted their timeslices recalculate timeslice for this task based on priority • eg Task is given 100 ms. timeslice. • After 20 m., CPU preempted. • Use 80 ms. later

  4. Ready Queue per each CPU list of all runnable tasks Ready Queue Hi-Priority Queue High priority tasks Low-Priority Queue Low priority tasks Priority 1 tasks Priority 1 Queue Priority 2 Queue Priority 3 tasks Priority 3 Queue Priority 4 Queue Priority 5 tasks Priority 5 Queue

  5. 0 list of priority 2 tasks 0 2 2 1 . . . . . . list of priority 132 tasks 132 132 1 bitmap[BITMAP_SIZE] queue[MAX_PRIO] Ready Queue per each CPU head pointer binary struct prio_array { int nr_active; /* number of tasks */ unsigned long bitmap[BITMAP_SIZE]; /* priority bitmap */ struct list_head queue[MAX_PRIO]; /* priority queues */ };

  6. prio_array bit 2 priority 2 list of runnable tasks 2 (1) scan priority traverse task list . . . (2) list of runnable tasks 132 bit 132 priority 132 bitmap[BITMAP_SIZE] queue[MAX_PRIO] Priority Arrays Diagram(per each CPU)

  7. list of runnable tasks 2 . . . list of runnable tasks 132 Expired array (timeslice expired) Each CPU bit 2 priority 2 bit 2 priority 2 Active array (timeslice remain) timeslice expired? 2 list of runnable tasks . . . bit 132 priority 132 bit 132 priority 132 132 list of runnable tasks bitmap[BITMAP_SIZE] queue[MAX_PRIO] Two Priority Arrays per CPU (3)

  8. list of runnable tasks 2 . . . list of runnable tasks 132 bit 2 priority 2 bit 132 priority 132 Two Priority Arrays per CPU Expired array (timeslice expired) Each CPU (3) timeslice expired? Active array (timeslice remain) (4) Active array becomes empty? i.e. all tasks used timeslice all tasks moved to Expired array then inter-change 2 priority arrays (new timeslices are given to all tasks) . . . bitmap[BITMAP_SIZE] queue[MAX_PRIO]

  9. Recalculating timeslice • task’s timeslice reaches 0 • move it to the expired array. • recalculated its timeslice • All tasks have moved to expired array?  Switch active & expired arrays. struct prio_arry array = rq->active; /* array is temporary; get active prio_array */ if (! array->nr_active) { /* there is no active tasks here */ rq->active = rq->expired; /* switch between active and expired */ rq->expired=array; }

  10. Two Priority Arrays per CPU Expired array (timeslice expired) Ready Queue Each CPU Active array (timeslice remain) Ready Queue

  11. . . . . . . Expired active priority arrays expired priority arrays Active runqueue1 CPU1 active priority arrays expired priority arrays runqueue2 CPU2 active priority arrays expired priority arrays runqueue3 CPU3 Data Structure for Each CPU Scheduling structrunqueue { spinlock_t lock; /* obtain locks in ascending address – deadlock */ unsigned long nr_running, nr_switches, expired_timestamp, nr_uninterruptible; task_struct *curr; /* currently running task */ struct mm_struct *prev_mm; prio_array*active, *expired; /* priority arrays */ int prev_cpu_load[NR_CPUS]; task_t *migration_thread; struct list_head migration_queue; atomic_t nr_iowait; }; Each CPU bitmap queue

  12. Scheduling Algorithm • O(1) scheduler schedule() constant-time scheduling regardless of # of processes task_t *prev, *next; prio_array_t *array; struct list_head *queue; int idx; array = rq->active; /* given CPU, get active prio_array */ idx = sched_find_first_bit(array->bitmap); /* find highest priority level */ queue = array->queue + idx; /* find highest priority queue */ next = list_entry (queue->next, task_t, run_list); /* get the next runnable task */ active priority arrays expired priority arrays runqueue1 CPU1 active priority arrays expired priority arrays runqueue2 CPU2

  13. Others • The load balancer • If runqueues are unbalanced in a multiprocessor • move processes from busier runqueue to current runqueue • Real-time • Soft real-time behavior -- no deadline guarantee • One can set/get scheduling policy • SCHED_FIFO no timeslice (non-preemptible) • SCHED_RR realtime round-robin • (SCHED_OTHER is non-realtime normal scheduling)

  14. Kernel mode task preemption& Critical Section problem

  15. Mutual Exclusion Process B Process A Read x into register (11) Operation with ALU register (12) Write back to storage box (12) Shared Memory x Read x into register (12) Operation with ALU register (13) Write back to storage box (13) Allow only one process to enter critical section at a time (exclude other process) correct result

  16. ---Functions in kernel ---Stack frame for function return address parameter local variables sh mail user user Kernel Stack proc proc read( ) send( ) Global Variables Lions kernel treated entire kernel as one critical section (excessive) Linux kernel can preempt if not accessing shared variable shared variable access = critical section (lock/unlock)

  17. Critical Section Lock Unlock preempt_count=0 AND need_resched means ”this thread can be preempted” AND ”other thread is waiting” When can you preempt CPU from a task running in kernel mode? Two Variables • preempt_count (in thread_info) per thread • number of locks held by this thread = ZERO?  “thread is not inside critical section”  one can preempt this kernel mode thread • need_resched flag • If set  “higher priority process is waiting to run”

  18. When does kernel preemption occurs? • When kernel becomes preemptible again • That is, after unlock(), test • (preempt_count=0) AND (need_resched is set) • If TRUE, call schedule() • When interrupt handler returns to kernel, test • (preempt_count=0) AND (need_resched is set) • If TRUE, call schedule() • Kernel explicitly calls schedule() • when this thread is returning to user mode • when this thread is going to sleep

  19. preemption points call schedule() I.H. Interrupt Handler C.S. Critical Section sleep I. H. C. S. I. H. (1) return from interrupt (3) exit Critical Section (unlock) (4) sys call is done (2) going to sleep (1) return from interrupt user mode kernel mode

  20. schedule() • schedule() selects new process to run(like swtch()) it calls context_swtch()(like savu()) • context_switch() calls • virtual memory mapping: switch_mm() • processor state switching: switch_to() (save and restore stack information & CPU registers) • Similar to UNIX’sswtch() savu() retu(), …

More Related