SlideShare a Scribd company logo
1 of 41
Download to read offline
Linux Scheduling (Kernel 2.6)


    Roy Lee, 21 Sep 2005
    NCTU Computer Operating System Lab



                                         1
Linux Scheduling

[include/linux/sched.h]

TASK_RUNNING
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
TASK_STOPPED
EXIT_ZOMBIE
EXIT_DEAD

set_task_state(task, state);
task_state = state;
set_current_state( state);




 Robert Love, “Linux Kernel Development,” 2nd Edition

                                                        2
Runnable & Running
struct runqueue {

   spinlock_t lock;

   unsigned long nr_running;
   unsigned long long nr_switches;
   unsigned long nr_uninterruptible;

   unsigned long expired_timestamp;
   unsigned long long timestamp_last_tick;

   task_t *curr, *idle;

   struct mm_struct *prev_mm;

   prio_array_t *active, *expired, arrays[2];
   int best_expired_prio;

   atomic_t nr_iowait;




                                                3
Runnable & Running(cont.)
#define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long))


                                                H    0
                                                                                   H
struct prio_array {                           p4           ...   ...
                                                    100
     unsigned int nr_active;
                                                H                                  H
     unsigned long bitmap[BITMAP_SIZE];                                  139        p1
                                              p2
                                                           bitmap
     struct list_head queue[MAX_PRIO];
                                                      0
};
                                                           ...   ...
                                                    100

idx = sched_find_first_bit(array->bitmap);                               139
queue = array->queue + idx;                                 queue
next = list_entry(queue->next, task_t, run_list);
                                                    struct prio_array



                                                                               4
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                     0
                                     H      H                                 H
   p4            ...   ...                            ...   ...
         100                                    100
     H                               H
                               139    p1                             139
   p2
                  queue                p3              queue

            0                                     0
                 ...   ...                            ...   ...
         100                                    100

                              139                                    139
                  bitmap                               bitmap

                *active                               *expired

P4 has the highest priority, and is selected for its execution

                                                                  process
                                                                  list_head       5
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                       0
                                     H        H                                 H
   p4            ...   ...                  p4          ...   ...
         100                                      100
     H                               H
                               139    p1                               139
   p2
                  queue                p3                queue

            0                                       0
                 ...   ...                              ...   ...
         100                                      100

                              139                                      139
                  bitmap                                 bitmap

                *active                                 *expired

Later on, P4 runs out of its timeslice, and get moved to the expired array

                                                                    process
                                                                    list_head       6
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                       0
                                     H        H                                 H
                 ...   ...                  p4          ...   ...
         100                                      100
     H                               H
                               139    p1                               139
   p2
                  queue                p3                queue

            0                                       0
                 ...   ...                              ...   ...
         100                                      100

                              139                                      139
                  bitmap                                 bitmap

                *active                                 *expired

Bitmaps are also updated

                                                                    process
                                                                    list_head       7
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                       0
                                     H        H                                 H
                 ...   ...                  p4          ...   ...
         100                                      100
     H                               H
                               139    p1                               139
   p2
                  queue                p3                queue

            0                                       0
                 ...   ...                              ...   ...
         100                                      100

                              139                                      139
                  bitmap                                 bitmap

                *active                                 *expired

Now, P2 has the highest priority, and is selected for its execution

                                                                    process
                                                                    list_head       8
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                       0
                                     H        H                                 H
                 ...   ...                  p4          ...   ...                p2
         100                                      100
     H                               H
                               139    p1                               139
   p2
                  queue                p3                queue

            0                                       0
                 ...   ...                              ...   ...
         100                                      100

                              139                                      139
                  bitmap                                 bitmap

                *active                                 *expired

Later on, P2 runs out of its timeslice, and get moved to the expired array

                                                                    process
                                                                    list_head         9
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                       0
                                     H        H                                 H
                 ...   ...                  p4          ...   ...                p2
         100                                      100
     H                               H
                               139    p1                               139
   p2
                  queue                p3                queue

            0                                       0
                 ...   ...                              ...   ...
         100                                      100

                              139                                      139
                  bitmap                                 bitmap

                *active                                 *expired

Bitmaps are also updated

                                                                    process
                                                                    list_head         10
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                       0
                                     H        H                                 H
                 ...   ...                  p4          ...   ...                p2
         100                                      100
     H                               H
                               139    p1                               139
                  queue                p3                queue

            0                                       0
                 ...   ...                              ...   ...
         100                                      100

                              139                                      139
                  bitmap                                 bitmap

                *active                                 *expired

Now, P1 has the highest priority, and is selected for its execution

                                                                    process
                                                                    list_head         11
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                            0
                                     H             H                                 H
                 ...   ...                       p4          ...   ...                p2
         100                                           100
     H                               H
                               139    p1                                    139
                  queue                p3                     queue

            0                                            0
                 ...   ...                  p5               ...   ...
         100                                           100

                              139                                           139
                  bitmap                                      bitmap

                *active                                      *expired

During its execution, it forks a child process P5

                                                                         process
                                                                         list_head         12
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                         0
                                     H          H                                 H
                 ...   ...                    p4          ...   ...                p2
         100                                        100
     H                               H
                               139    p5                                 139
                  queue                p1                  queue
                                         p3
            0                                         0
                 ...   ...                                ...   ...
         100                                        100

                              139                                        139
                  bitmap                                   bitmap

                *active                                   *expired

To avoid COW overhead, P1 yields the CPU to the P5

                                                                      process
                                                                      list_head         13
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                             0
                                     H          H                                     H
                 ...   ...                    p4              ...   ...                p2
         100                                            100
     H                               H
                               139    p5            H                        139
                  queue                p1      p5              queue
                                         p3
            0                                             0
                 ...   ...                                    ...   ...
         100                                            100

                              139                                            139
                  bitmap                                       bitmap

                *active                                       *expired

Later on, P5 runs out of its timeslice, and get moved to the expired array

                                                                          process
                                                                          list_head         14
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                             0
                                     H          H                                     H
                 ...   ...                    p4              ...   ...                p2
         100                                            100
     H                               H
                               139    p5            H                        139
                  queue                p1      p5              queue
                                         p3
            0                                             0
                 ...   ...                                    ...   ...
         100                                            100

                              139                                            139
                  bitmap                                       bitmap

                *active                                       *expired

Bitmaps are also updated

You may notice that it’s priority is changed here                         process
We will explain this later                                                list_head         15
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                           0
                                     H        H                                     H
                 ...   ...                  p4              ...   ...                p2
         100                                          100
     H                               H
                               139    p1                                   139      H
                                                  H
                  queue                p3    p5              queue

            0                                           0
                 ...   ...                                  ...   ...
         100                                          100

                              139                                          139
                  bitmap                                     bitmap

                *active                                     *expired

P1 resumes its execution, finishes its job and then exits

This is a typical fork() and the exec() scenario                        process
                                                                        list_head         16
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                          0
                                     H       H                                     H
                 ...   ...                 p4              ...   ...                p2
         100                                         100
     H                               H
                               139    p3                                  139      H
                                                 H
                  queue                     p5              queue

            0                                          0
                 ...   ...                                 ...   ...
         100                                         100

                              139                                         139
                  bitmap                                    bitmap

                *active                                    *expired

Now, P3 has the highest priority, and is selected for its execution

                                                                       process
                                                                       list_head         17
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                          0
                                     H       H                                     H
                 ...   ...                 p4              ...   ...                p2
         100                                         100
     H                               H
                               139    p3                                  139      H
                                                 H
                  queue                     p5              queue                   p3

            0                                          0
                 ...   ...                                 ...   ...
         100                                         100

                              139                                         139
                  bitmap                                    bitmap

                *active                                    *expired

Later on, P3 runs out of its timeslice, and get moved to the expired array

                                                                       process
                                                                       list_head         18
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                          0
                                     H       H                                     H
                 ...   ...                 p4              ...   ...                p2
         100                                         100
     H                               H
                               139    p3                                  139      H
                                                 H
                  queue                     p5              queue                   p3

            0                                          0
                 ...   ...                                 ...   ...
         100                                         100

                              139                                         139
                  bitmap                                    bitmap

                *active                                    *expired

Bitmaps are also updated

                                                                       process
                                                                       list_head         19
Runnable & Running(cont.)
   struct   prio_array       array[2]



     H      0                                            0
                                     H         H                                     H
                 ...   ...                   p4              ...   ...                p2
         100                                           100
     H                               H
                               139                                          139      H
                                                   H
                  queue                       p5              queue                   p3

            0                                            0
                 ...   ...                                   ...   ...
         100                                           100

                              139                                           139
                  bitmap                                      bitmap
                                        Exchange!
                *active                                      *expired

Now the active array is empty, scheduler exchanges it with the expired one

                                                                         process
                                                                         list_head         20
Runnable & Running(cont.)
   struct   prio_array      array[2]



     H      0                                       0
                                    H     H                                     H
                ...   ...               p4              ...   ...                p2
         100                                      100
     H                              H
                              139                                      139      H
                                              H
                 queue                   p5              queue                   p3

            0                                       0
                ...   ...                               ...   ...
         100                                      100

                             139                                       139
                 bitmap                                  bitmap

                *expired                                *active

Another round begins!

                                                                    process
                                                                    list_head         21
What Polices Do We Have?
                                  0             MAX_RT_PRIO   MAX_PRIO

   SCHED_NORMAL
     Ranges from MAX_RT_PRIO to MAX_PRIO - 1 (100 ~ 139)




   SCHED_FIFO & SCHED_RR
     Ranges from 0 to MAX_RT_PRIO -1 (0 ~ 99)

     Both are soft real-time scheduling.

     A SCHED_FIFO process doesn’t have timeslice.

     A SCHED_RR process only round-robbin with those which have

      equal priority.
     The real-time processes:

         never expire.
         work with static priority


                                                                   22
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                        0
                                     H      H                                   H
                 ...   ...                p4              ...   ...              p1
         100                             p2        100                            p5
     H                               H      H
                               139                                       139    H
                                          p6                                     p3
                  queue                                    queue
                                         p7

            0                                        0
                 ...   ...                               ...    ...
         100                                       100

                              139                                        139
                  bitmap                                   bitmap

                *expired                                  *active

P4 has the highest priority, and is selected for its execution

                                                RR process            Normal process
                                                FIFO process          list_head     23
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                        0
                                     H      H                                   H
                 ...   ...                p2              ...   ...              p1
         100                             p4        100                            p5
     H                               H      H
                               139                                       139    H
                                          p6                                     p3
                  queue                                    queue
                                         p7

            0                                        0
                 ...   ...                               ...    ...
         100                                       100

                              139                                        139
                  bitmap                                   bitmap

                *expired                                  *active
P4 runs out its timeslice, but since its a RR process, it does not expire
Scheduler reinserts it to the tail of its priority list
                                                RR process            Normal process
                                                FIFO process          list_head     24
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                        0
                                     H      H                                   H
                 ...   ...                p2              ...   ...              p1
         100                             p4        100                            p5
     H                               H      H
                               139                                       139    H
                                          p6                                     p3
                  queue                                    queue
                                         p7

            0                                        0
                 ...   ...                               ...    ...
         100                                       100

                              139                                        139
                  bitmap                                   bitmap

                *expired                                  *active

Now P2 has the highest priority, and is selected for its execution

                                                RR process            Normal process
                                                FIFO process          list_head     25
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                       0
                                     H     H                                   H
                 ...   ...               p4              ...   ...              p1
         100                                      100                            p5
     H                               H      H
                               139                                      139    H
                                          p6                                    p3
                  queue                                   queue
                                         p7

            0                                       0
                 ...   ...                              ...    ...
         100                                      100

                              139                                       139
                  bitmap                                  bitmap

                *expired                                 *active
P2 finishes its job and exits
Now P4 has the highest priority, and is selected for its execution
                                               RR process            Normal process
                                               FIFO process          list_head     26
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                       0
                                     H     H                                   H
                 ...   ...               p4              ...   ...              p1
         100                                      100                            p5
     H                               H      H
                               139                                      139    H
                                          p6                                    p3
                  queue                                   queue
                                         p7

            0                                       0
                 ...   ...                              ...    ...
         100                                      100

                              139                                       139
                  bitmap                                  bitmap

                *expired                                 *active
In this case, unless P4 exits or voluntarily relinquishes its execution,
or higher priority processes are created/waked up, it monopolize the CPU
                                               RR process            Normal process
                                               FIFO process          list_head     27
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                        0
                                     H      H                                   H
                 ...   ...                                ...   ...              p1
         100                                       100                            p5
     H                               H       H
                               139                                       139    H
                                           p6                                    p3
                  queue                                    queue
                                          p7

            0                                        0
                 ...   ...                               ...    ...
         100                                       100

                              139                                        139
                  bitmap                                   bitmap

                *expired                                  *active
Later on, P4 finishes its job and exits
P1 is selected for its execution

                                                RR process            Normal process
                                                FIFO process          list_head     28
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                       0
                                     H     H                                   H
                 ...   ...                               ...   ...              p5
         100                                      100                            p1
     H                               H      H
                               139                                      139    H
                                          p6                                    p3
                  queue                                   queue
                                         p7

            0                                       0
                 ...   ...                              ...    ...
         100                                      100

                              139                                       139
                  bitmap                                  bitmap

                *expired                                 *active

P1 runs out its timeslice and is reinserted to the tail of its list

                                               RR process            Normal process
                                               FIFO process          list_head     29
Realtime Scheduling
   struct   prio_array       array[2]



     H      0                                       0
                                     H     H                                   H
                 ...   ...                               ...   ...              p5
         100                                      100                            p1
     H                               H      H
                               139                                      139    H
                                          p6                                    p3
                  queue                                   queue
                                         p7

            0                                       0
                 ...   ...                              ...    ...
         100                                      100

                              139                                       139
                  bitmap                                  bitmap

                *expired                                 *active
P5 is FIFO realtime, it does not have timeslice.
Unless higher priority processes are created/waked up, it monopolizes the CPU

                                               RR process            Normal process
                                               FIFO process          list_head     30
The Priority of Processes
                        0                          MAX_RT_PRIO    MAX_PRIO


    Static priority
                                                  mapping
        Ranges from -20 to 19
        Specified by the user (nice value).
    Dynamic priority
        A bonus or penalty from the range -5 to +5 based on the
         interactivity of the task.

    #define MAX_USER_RT_PRIO     100
    #define MAX_RT_PRIO      MAX_USER_RT_PRIO
    #define MAX_PRIO         (MAX_RT_PRIO + 40)


    #define NICE_TO_PRIO(nice)   (MAX_RT_PRIO + (nice) + 20)
    #define PRIO_TO_NICE(prio)   ((prio) - MAX_RT_PRIO - 20)
    #define TASK_NICE(p)         PRIO_TO_NICE((p)->static_prio)


                                                                             31
The Priority of Processes(cont.)
Struct task_struct        Dynamic priority.
{
    int state;              Specified by the user.(nice)
    ...

   int prio, static_prio;       Ranges from 0 to MAX_SLEEP_AVG

   ...
   prio_array_t *array;                       timer_interrupt()

   unsigned long sleep_avg;
   unsigned long long timestamp, last_ran;       update_process_times()
   unsigned long long sched_time;
   int activated;
                                                     scheduler_tick()

   unsigned long policy;                                          if (!--p->time_slice)

   cpumask_t cpus_allowed;
   unsigned int time_slice, first_time_slice;              recalc_task_prio()
   ...
                                                              effective_prio()




                                                                                          32
When A Process is Interactive Enough…
   struct   prio_array        array[2]



     H      0                                      0
                                      H      H                                 H
                  ...   ...                p4          ...   ...                p2
         100                              p1     100
     H                                H
                                139                                   139      H
                                             H                                  p3
                   queue                                queue

            0                                      0
                  ...   ...                            ...   ...
         100                                     100

                               139                                    139
                   bitmap                               bitmap

                *expired                               *active
If P4 has enough interactivity, after it runs out its timeslice,
the scheduler would reinsert it to the end of its list

Case 1                                                             process
                                                                   list_head         33
When A Process is Interactive Enough…
   struct   prio_array        array[2]



     H      0                                      0
                                      H      H                                 H
                  ...   ...                p1          ...   ...                p2
         100                              p4     100
     H                                H
                                139                                   139      H
                                             H                                  p3
                   queue                                 queue

            0                                      0
                  ...   ...                            ...   ...
         100                                     100

                               139                                    139
                   bitmap                                bitmap

                *expired                               *active
The scheduler would reinsert it to the end of its list
instead of moving it to the expired array

Case 1                                                             process
                                                                   list_head         34
When A Process is Interactive Enough…
   struct   prio_array        array[2]



     H      0                                      0
                                      H      H                                 H
   p4             ...   ...                p4          ...   ...                p2
         100                              p1     100
     H                                H
                                139                                   139      H
                                             H                                  p3
                   queue                                queue

            0                                      0
                  ...   ...                            ...   ...
         100                                     100

                               139                                    139
                   bitmap                               bitmap

                *expired                               *active
However, if there are any processes that have been starved,
it still has to be expired to prevent further starvation

Case 2                                                             process
                                                                   list_head         35
Interactivity of Process
  if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) {
     enqueue_task(p, rq->expired);
         if (p->static_prio < rq->best_expired_prio)
             rq->best_expired_prio = p->static_prio;
 } else
     enqueue_task(p, rq->active);



 #define TASK_INTERACTIVE(p) 
     ((p)->prio <= (p)->static_prio - DELTA(p))

 #define DELTA(p) 
     (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA)

 #define EXPIRED_STARVING(rq) 
     ((STARVATION_LIMIT && ((rq)->expired_timestamp && 
         (jiffies - (rq)->expired_timestamp >= 
             STARVATION_LIMIT * ((rq)->nr_running) + 1))) || 
             ((rq)->curr->static_prio > (rq)->best_expired_prio))



                                                                    36
Timeslice

   The calculation is a simple scaling of the static priority into a range
    of timeslices (5 ~ 800 ms).

   By default (with nice value of zero) is 100 ms.

 #define MIN_TIMESLICE       max(5 * HZ / 1000, 1)
 #define DEF_TIMESLICE       (100 * HZ / 1000)

 #define SCALE_PRIO(x, prio) 
     max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE)

 static inline unsigned int task_timeslice(task_t *p)
 {
     if (p->static_prio < NICE_TO_PRIO(0))
         return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio);
     else
         return SCALE_PRIO(DEF_TIMESLICE, p->static_prio);
 }



                                                                          37
Scheduling with Process Creation
                              p->state = TASK_RUNNING;
                                  INIT_LIST_HEAD(&p->run_list);
do_fork()                         p->array = NULL;
                                  spin_lock_init(&p->switch_lock);
  copy_process()                 ...

      sched_fork()            local_irq_disable();
                              p->time_slice = (current->time_slice + 1) >> 1;
                              p->first_time_slice = 1;
      wake_up_new()           current->time_slice >>= 1;
                              p->timestamp = sched_clock();

 p->prio = current->prio;     if (unlikely(!current->time_slice)) {
 list_add_tail(...);              current->time_slice = 1;
 p->array = current->array;       preempt_disable();
 p->array->nr_active++;           scheduler_tick();
 rq->nr_running++;                local_irq_enable();
                                  preempt_enable();
 To avoid the COW overhead,   } else
  we let the child go first       local_irq_enable();


                                                                        38
Scheduling with Process Termination

sys_exit()

                              sched_exit()
  do_exit()


    exit_notify()      rq = task_rq_lock(p->parent, &flags);

                       if (p->first_time_slice) {
      release_task()      p->parent->time_slice += p->time_slice;
                          if (unlikely(p->parent->time_slice >
                                                      task_timeslice(p)))
        sched_exit()
                              p->parent->time_slice = task_timeslice(p);
                       }
    schedule()         if (p->sleep_avg < p->parent->sleep_avg)
                          p->parent->sleep_avg = p->parent->sleep_avg /
                          (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg /
                          (EXIT_WEIGHT + 1);
    BUG();
                       task_rq_unlock(rq, &flags);


                                                                       39
Control Flow of scheduler_tick()
scheduler_tick()

                          yes                       no(FIFO)
   Realtime task?                 Round robbin?
          no                             yes
                          yes                             yes
   Timeslice remained?             Timeslice remained?
          no
                                          no
   Remove from active
                                  Set need_reschedule flag

   Recalculate priority
   and timeslice                                                Continue exection


                            yes                          no
   Interactive enough?            Is there any task in          Reinsert to active
          no                      expired starving?
                                           yes
   Reinsert to expired


   Set need_reschedule flag


                                                                               40
Charge Ticks to the Current Process

timer_interrupt()                #define user_mode(regs) (!!((regs)->cs & 3))


  update_process_times(user_mode(regs))



             User mode?                                   jiffies_to_cputime(1)

   No               Yes

         account_user_time()         p->utime = cputime_add(p->utime, cputime);



         account_system_time()       p->stime = cputime_add(p->stime, cputime);




                                                                                41

More Related Content

What's hot

ĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdf
ĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdfĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdf
ĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdfMan_Ebook
 
Bai giang-vhdl
Bai giang-vhdlBai giang-vhdl
Bai giang-vhdlhoangclick
 
Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...
Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...
Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...vanhaimta
 
Báo cáo t hiết kế mạng doanh nghiệp
Báo cáo t hiết kế mạng doanh nghiệpBáo cáo t hiết kế mạng doanh nghiệp
Báo cáo t hiết kế mạng doanh nghiệpLe Trung Hieu
 
LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...
LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...
LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...KhoTi1
 
Đề cương môn xử lý ảnh
Đề cương môn xử lý ảnhĐề cương môn xử lý ảnh
Đề cương môn xử lý ảnhJean Valjean
 
Chapter5 lan mac
Chapter5 lan macChapter5 lan mac
Chapter5 lan macNghia Simon
 
Xử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũ
Xử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũXử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũ
Xử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũPhan Khanh Toan
 

What's hot (20)

Luận văn: Nghiên cứu công nghệ ảo hóa mạng với open flow
Luận văn: Nghiên cứu công nghệ ảo hóa mạng với open flowLuận văn: Nghiên cứu công nghệ ảo hóa mạng với open flow
Luận văn: Nghiên cứu công nghệ ảo hóa mạng với open flow
 
ĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdf
ĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdfĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdf
ĐIỀU KHIỂN HỆ ĐA TÁC TỬ.pdf
 
Bai giang-vhdl
Bai giang-vhdlBai giang-vhdl
Bai giang-vhdl
 
Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...
Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...
Co so-tu-dong-nang-cao huynh-thai-hoang-chuong-3_ltdknc_dieu-khien-toi-uu- - ...
 
Báo cáo t hiết kế mạng doanh nghiệp
Báo cáo t hiết kế mạng doanh nghiệpBáo cáo t hiết kế mạng doanh nghiệp
Báo cáo t hiết kế mạng doanh nghiệp
 
LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...
LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...
LUẬN VĂN THIẾT KẾ HỆ THỐNG NHÚNG VỚI VI ĐIỀU KHIỂN LÕI MỀM VÀ HỆ ĐIỀU HÀNH TR...
 
Đề tài: Nguyên lí hoạt động của một số máy đùn ép nhựa, 9đ
Đề tài: Nguyên lí hoạt động của một số máy đùn ép nhựa, 9đĐề tài: Nguyên lí hoạt động của một số máy đùn ép nhựa, 9đ
Đề tài: Nguyên lí hoạt động của một số máy đùn ép nhựa, 9đ
 
Đề cương môn xử lý ảnh
Đề cương môn xử lý ảnhĐề cương môn xử lý ảnh
Đề cương môn xử lý ảnh
 
Nghiên cứu tổng hợp vật liệu SnO2 có cấu trúc nano đa cấp, HAY
Nghiên cứu tổng hợp vật liệu SnO2 có cấu trúc nano đa cấp, HAYNghiên cứu tổng hợp vật liệu SnO2 có cấu trúc nano đa cấp, HAY
Nghiên cứu tổng hợp vật liệu SnO2 có cấu trúc nano đa cấp, HAY
 
Đề tài: Mô phỏng kênh truyền vô tuyến số bằng matlab, 9đ
Đề tài: Mô phỏng kênh truyền vô tuyến số bằng matlab, 9đ Đề tài: Mô phỏng kênh truyền vô tuyến số bằng matlab, 9đ
Đề tài: Mô phỏng kênh truyền vô tuyến số bằng matlab, 9đ
 
Đồ Án Tốt Nghiệp Lưu Đồ Giải Thuật Và Chương Trình Điều Khiển.docx
Đồ Án Tốt Nghiệp Lưu Đồ Giải Thuật Và Chương Trình Điều Khiển.docxĐồ Án Tốt Nghiệp Lưu Đồ Giải Thuật Và Chương Trình Điều Khiển.docx
Đồ Án Tốt Nghiệp Lưu Đồ Giải Thuật Và Chương Trình Điều Khiển.docx
 
Luận văn: Án treo và thực tiễn áp dụng tại tỉnh Hải Dương, HAY
Luận văn: Án treo và thực tiễn áp dụng tại tỉnh Hải Dương, HAYLuận văn: Án treo và thực tiễn áp dụng tại tỉnh Hải Dương, HAY
Luận văn: Án treo và thực tiễn áp dụng tại tỉnh Hải Dương, HAY
 
Đề tài: Quyền con người trong giai đoạn khởi tố, điều tra vụ án
Đề tài: Quyền con người trong giai đoạn khởi tố, điều tra vụ ánĐề tài: Quyền con người trong giai đoạn khởi tố, điều tra vụ án
Đề tài: Quyền con người trong giai đoạn khởi tố, điều tra vụ án
 
Luận văn: Hình phạt cải tạo không giam giữ theo pháp luật hình sự
Luận văn: Hình phạt cải tạo không giam giữ theo pháp luật hình sựLuận văn: Hình phạt cải tạo không giam giữ theo pháp luật hình sự
Luận văn: Hình phạt cải tạo không giam giữ theo pháp luật hình sự
 
Luận văn: Thiết kế giao diện người – máy, HAY, 9đ
Luận văn: Thiết kế giao diện người – máy, HAY, 9đLuận văn: Thiết kế giao diện người – máy, HAY, 9đ
Luận văn: Thiết kế giao diện người – máy, HAY, 9đ
 
Luận văn: Kiểm sát điều tra vụ án gây rối trật tự công cộng, HOT
Luận văn: Kiểm sát điều tra vụ án gây rối trật tự công cộng, HOTLuận văn: Kiểm sát điều tra vụ án gây rối trật tự công cộng, HOT
Luận văn: Kiểm sát điều tra vụ án gây rối trật tự công cộng, HOT
 
Chapter5 lan mac
Chapter5 lan macChapter5 lan mac
Chapter5 lan mac
 
Luận án: Thuật toán và xây dựng chương trình xử lý số liệu GNSS
Luận án: Thuật toán và xây dựng chương trình xử lý số liệu GNSSLuận án: Thuật toán và xây dựng chương trình xử lý số liệu GNSS
Luận án: Thuật toán và xây dựng chương trình xử lý số liệu GNSS
 
Xử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũ
Xử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũXử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũ
Xử lý ảnh theo phương pháp âm bản, nhị phân, hàm số mũ
 
Iptv
IptvIptv
Iptv
 

Viewers also liked

The Linux Scheduler: a Decade of Wasted Cores
The Linux Scheduler: a Decade of Wasted CoresThe Linux Scheduler: a Decade of Wasted Cores
The Linux Scheduler: a Decade of Wasted Coresyeokm1
 
Real Time Operating System
Real Time Operating SystemReal Time Operating System
Real Time Operating SystemSharad Pandey
 
Operating system 11.10.2016 adarsh bang
Operating system 11.10.2016 adarsh bangOperating system 11.10.2016 adarsh bang
Operating system 11.10.2016 adarsh bangadarshbang1998
 
Docker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутомDocker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутомAnton Turetsky
 
Intro to cluster scheduler for Linux containers
Intro to cluster scheduler for Linux containersIntro to cluster scheduler for Linux containers
Intro to cluster scheduler for Linux containersKumar Gaurav
 
LCU13: Power-efficient scheduling, and the latest news from the kernel summit
LCU13: Power-efficient scheduling, and the latest news from the kernel summitLCU13: Power-efficient scheduling, and the latest news from the kernel summit
LCU13: Power-efficient scheduling, and the latest news from the kernel summitLinaro
 
Javaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだかJavaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだかHiroki Uchida
 
Scheduling in Android
Scheduling in AndroidScheduling in Android
Scheduling in AndroidOpersys inc.
 
3. CPU virtualization and scheduling
3. CPU virtualization and scheduling3. CPU virtualization and scheduling
3. CPU virtualization and schedulingHwanju Kim
 
Insider operating system
Insider   operating systemInsider   operating system
Insider operating systemAditi Saxena
 
Windows process-scheduling
Windows process-schedulingWindows process-scheduling
Windows process-schedulingTalha Shaikh
 

Viewers also liked (17)

The Linux Scheduler: a Decade of Wasted Cores
The Linux Scheduler: a Decade of Wasted CoresThe Linux Scheduler: a Decade of Wasted Cores
The Linux Scheduler: a Decade of Wasted Cores
 
Scheduling In Linux
Scheduling In LinuxScheduling In Linux
Scheduling In Linux
 
Process scheduling linux
Process scheduling linuxProcess scheduling linux
Process scheduling linux
 
Real Time Operating System
Real Time Operating SystemReal Time Operating System
Real Time Operating System
 
Jireh ict
Jireh ictJireh ict
Jireh ict
 
Operating system 11.10.2016 adarsh bang
Operating system 11.10.2016 adarsh bangOperating system 11.10.2016 adarsh bang
Operating system 11.10.2016 adarsh bang
 
Docker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутомDocker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутом
 
Intro to cluster scheduler for Linux containers
Intro to cluster scheduler for Linux containersIntro to cluster scheduler for Linux containers
Intro to cluster scheduler for Linux containers
 
LCU13: Power-efficient scheduling, and the latest news from the kernel summit
LCU13: Power-efficient scheduling, and the latest news from the kernel summitLCU13: Power-efficient scheduling, and the latest news from the kernel summit
LCU13: Power-efficient scheduling, and the latest news from the kernel summit
 
Javaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだかJavaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだか
 
React native
React nativeReact native
React native
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Scheduling in Android
Scheduling in AndroidScheduling in Android
Scheduling in Android
 
3. CPU virtualization and scheduling
3. CPU virtualization and scheduling3. CPU virtualization and scheduling
3. CPU virtualization and scheduling
 
Insider operating system
Insider   operating systemInsider   operating system
Insider operating system
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Windows process-scheduling
Windows process-schedulingWindows process-scheduling
Windows process-scheduling
 

Linux O(1) Scheduling

  • 1. Linux Scheduling (Kernel 2.6) Roy Lee, 21 Sep 2005 NCTU Computer Operating System Lab 1
  • 3. Runnable & Running struct runqueue { spinlock_t lock; unsigned long nr_running; unsigned long long nr_switches; unsigned long nr_uninterruptible; unsigned long expired_timestamp; unsigned long long timestamp_last_tick; task_t *curr, *idle; struct mm_struct *prev_mm; prio_array_t *active, *expired, arrays[2]; int best_expired_prio; atomic_t nr_iowait; 3
  • 4. Runnable & Running(cont.) #define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long)) H 0 H struct prio_array { p4 ... ... 100 unsigned int nr_active; H H unsigned long bitmap[BITMAP_SIZE]; 139 p1 p2 bitmap struct list_head queue[MAX_PRIO]; 0 }; ... ... 100 idx = sched_find_first_bit(array->bitmap); 139 queue = array->queue + idx; queue next = list_entry(queue->next, task_t, run_list); struct prio_array 4
  • 5. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H p4 ... ... ... ... 100 100 H H 139 p1 139 p2 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired P4 has the highest priority, and is selected for its execution process list_head 5
  • 6. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H p4 ... ... p4 ... ... 100 100 H H 139 p1 139 p2 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Later on, P4 runs out of its timeslice, and get moved to the expired array process list_head 6
  • 7. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... 100 100 H H 139 p1 139 p2 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Bitmaps are also updated process list_head 7
  • 8. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... 100 100 H H 139 p1 139 p2 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Now, P2 has the highest priority, and is selected for its execution process list_head 8
  • 9. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p1 139 p2 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Later on, P2 runs out of its timeslice, and get moved to the expired array process list_head 9
  • 10. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p1 139 p2 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Bitmaps are also updated process list_head 10
  • 11. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p1 139 queue p3 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Now, P1 has the highest priority, and is selected for its execution process list_head 11
  • 12. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p1 139 queue p3 queue 0 0 ... ... p5 ... ... 100 100 139 139 bitmap bitmap *active *expired During its execution, it forks a child process P5 process list_head 12
  • 13. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p5 139 queue p1 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired To avoid COW overhead, P1 yields the CPU to the P5 process list_head 13
  • 14. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p5 H 139 queue p1 p5 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Later on, P5 runs out of its timeslice, and get moved to the expired array process list_head 14
  • 15. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p5 H 139 queue p1 p5 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Bitmaps are also updated You may notice that it’s priority is changed here process We will explain this later list_head 15
  • 16. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p1 139 H H queue p3 p5 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired P1 resumes its execution, finishes its job and then exits This is a typical fork() and the exec() scenario process list_head 16
  • 17. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p3 139 H H queue p5 queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Now, P3 has the highest priority, and is selected for its execution process list_head 17
  • 18. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p3 139 H H queue p5 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Later on, P3 runs out of its timeslice, and get moved to the expired array process list_head 18
  • 19. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 p3 139 H H queue p5 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *active *expired Bitmaps are also updated process list_head 19
  • 20. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 139 H H queue p5 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap Exchange! *active *expired Now the active array is empty, scheduler exchanges it with the expired one process list_head 20
  • 21. Runnable & Running(cont.) struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 100 H H 139 139 H H queue p5 queue p3 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active Another round begins! process list_head 21
  • 22. What Polices Do We Have? 0 MAX_RT_PRIO MAX_PRIO  SCHED_NORMAL  Ranges from MAX_RT_PRIO to MAX_PRIO - 1 (100 ~ 139)  SCHED_FIFO & SCHED_RR  Ranges from 0 to MAX_RT_PRIO -1 (0 ~ 99)  Both are soft real-time scheduling.  A SCHED_FIFO process doesn’t have timeslice.  A SCHED_RR process only round-robbin with those which have equal priority.  The real-time processes:  never expire.  work with static priority 22
  • 23. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p1 100 p2 100 p5 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active P4 has the highest priority, and is selected for its execution RR process Normal process FIFO process list_head 23
  • 24. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... p2 ... ... p1 100 p4 100 p5 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active P4 runs out its timeslice, but since its a RR process, it does not expire Scheduler reinserts it to the tail of its priority list RR process Normal process FIFO process list_head 24
  • 25. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... p2 ... ... p1 100 p4 100 p5 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active Now P2 has the highest priority, and is selected for its execution RR process Normal process FIFO process list_head 25
  • 26. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p1 100 100 p5 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active P2 finishes its job and exits Now P4 has the highest priority, and is selected for its execution RR process Normal process FIFO process list_head 26
  • 27. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p1 100 100 p5 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active In this case, unless P4 exits or voluntarily relinquishes its execution, or higher priority processes are created/waked up, it monopolize the CPU RR process Normal process FIFO process list_head 27
  • 28. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... ... ... p1 100 100 p5 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active Later on, P4 finishes its job and exits P1 is selected for its execution RR process Normal process FIFO process list_head 28
  • 29. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... ... ... p5 100 100 p1 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active P1 runs out its timeslice and is reinserted to the tail of its list RR process Normal process FIFO process list_head 29
  • 30. Realtime Scheduling struct prio_array array[2] H 0 0 H H H ... ... ... ... p5 100 100 p1 H H H 139 139 H p6 p3 queue queue p7 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active P5 is FIFO realtime, it does not have timeslice. Unless higher priority processes are created/waked up, it monopolizes the CPU RR process Normal process FIFO process list_head 30
  • 31. The Priority of Processes 0 MAX_RT_PRIO MAX_PRIO  Static priority mapping  Ranges from -20 to 19  Specified by the user (nice value).  Dynamic priority  A bonus or penalty from the range -5 to +5 based on the interactivity of the task. #define MAX_USER_RT_PRIO 100 #define MAX_RT_PRIO MAX_USER_RT_PRIO #define MAX_PRIO (MAX_RT_PRIO + 40) #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) 31
  • 32. The Priority of Processes(cont.) Struct task_struct Dynamic priority. { int state; Specified by the user.(nice) ... int prio, static_prio; Ranges from 0 to MAX_SLEEP_AVG ... prio_array_t *array; timer_interrupt() unsigned long sleep_avg; unsigned long long timestamp, last_ran; update_process_times() unsigned long long sched_time; int activated; scheduler_tick() unsigned long policy; if (!--p->time_slice) cpumask_t cpus_allowed; unsigned int time_slice, first_time_slice; recalc_task_prio() ... effective_prio() 32
  • 33. When A Process is Interactive Enough… struct prio_array array[2] H 0 0 H H H ... ... p4 ... ... p2 100 p1 100 H H 139 139 H H p3 queue queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active If P4 has enough interactivity, after it runs out its timeslice, the scheduler would reinsert it to the end of its list Case 1 process list_head 33
  • 34. When A Process is Interactive Enough… struct prio_array array[2] H 0 0 H H H ... ... p1 ... ... p2 100 p4 100 H H 139 139 H H p3 queue queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active The scheduler would reinsert it to the end of its list instead of moving it to the expired array Case 1 process list_head 34
  • 35. When A Process is Interactive Enough… struct prio_array array[2] H 0 0 H H H p4 ... ... p4 ... ... p2 100 p1 100 H H 139 139 H H p3 queue queue 0 0 ... ... ... ... 100 100 139 139 bitmap bitmap *expired *active However, if there are any processes that have been starved, it still has to be expired to prevent further starvation Case 2 process list_head 35
  • 36. Interactivity of Process if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) { enqueue_task(p, rq->expired); if (p->static_prio < rq->best_expired_prio) rq->best_expired_prio = p->static_prio; } else enqueue_task(p, rq->active); #define TASK_INTERACTIVE(p) ((p)->prio <= (p)->static_prio - DELTA(p)) #define DELTA(p) (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA) #define EXPIRED_STARVING(rq) ((STARVATION_LIMIT && ((rq)->expired_timestamp && (jiffies - (rq)->expired_timestamp >= STARVATION_LIMIT * ((rq)->nr_running) + 1))) || ((rq)->curr->static_prio > (rq)->best_expired_prio)) 36
  • 37. Timeslice  The calculation is a simple scaling of the static priority into a range of timeslices (5 ~ 800 ms).  By default (with nice value of zero) is 100 ms. #define MIN_TIMESLICE max(5 * HZ / 1000, 1) #define DEF_TIMESLICE (100 * HZ / 1000) #define SCALE_PRIO(x, prio) max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE) static inline unsigned int task_timeslice(task_t *p) { if (p->static_prio < NICE_TO_PRIO(0)) return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio); else return SCALE_PRIO(DEF_TIMESLICE, p->static_prio); } 37
  • 38. Scheduling with Process Creation p->state = TASK_RUNNING; INIT_LIST_HEAD(&p->run_list); do_fork() p->array = NULL; spin_lock_init(&p->switch_lock); copy_process() ... sched_fork() local_irq_disable(); p->time_slice = (current->time_slice + 1) >> 1; p->first_time_slice = 1; wake_up_new() current->time_slice >>= 1; p->timestamp = sched_clock(); p->prio = current->prio; if (unlikely(!current->time_slice)) { list_add_tail(...); current->time_slice = 1; p->array = current->array; preempt_disable(); p->array->nr_active++; scheduler_tick(); rq->nr_running++; local_irq_enable(); preempt_enable(); To avoid the COW overhead, } else we let the child go first local_irq_enable(); 38
  • 39. Scheduling with Process Termination sys_exit() sched_exit() do_exit() exit_notify() rq = task_rq_lock(p->parent, &flags); if (p->first_time_slice) { release_task() p->parent->time_slice += p->time_slice; if (unlikely(p->parent->time_slice > task_timeslice(p))) sched_exit() p->parent->time_slice = task_timeslice(p); } schedule() if (p->sleep_avg < p->parent->sleep_avg) p->parent->sleep_avg = p->parent->sleep_avg / (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg / (EXIT_WEIGHT + 1); BUG(); task_rq_unlock(rq, &flags); 39
  • 40. Control Flow of scheduler_tick() scheduler_tick() yes no(FIFO) Realtime task? Round robbin? no yes yes yes Timeslice remained? Timeslice remained? no no Remove from active Set need_reschedule flag Recalculate priority and timeslice Continue exection yes no Interactive enough? Is there any task in Reinsert to active no expired starving? yes Reinsert to expired Set need_reschedule flag 40
  • 41. Charge Ticks to the Current Process timer_interrupt() #define user_mode(regs) (!!((regs)->cs & 3)) update_process_times(user_mode(regs)) User mode? jiffies_to_cputime(1) No Yes account_user_time() p->utime = cputime_add(p->utime, cputime); account_system_time() p->stime = cputime_add(p->stime, cputime); 41