Monday 15 June 2015

Introduction to process scheduling in linux

Process scheduler divides the processor time between runnable processes in the system.Scheduler decides which process runs next.(Source code: kernel/sched)

Linux kernel implements priority based scheduling using two schemes:

1. Based on nice values

nice value is a number from -20 to 19 with a default of 0.When nice value is more the process is nicer (i.e) gives up for other processes. In other words, lesser nice value implies higher priority.

For example RFCOMM (used to setup and maintain bluetooth configuration of in the Linux kernel) sets the nice value of -10 using the function call:


                               set_user_nice(current, -10);  [1]

root@ramapriya:/home/devel/linux-4.0.5# ps -el | grep rfcomm
F S   UID   PID  PPID  C PRI  NI   ADDR SZ WCHAN  TTY          TIME   CMD
5 S     0      723     2     0  70   -10    -         0    rfcomm    ?        00:00:00 krfcommd


As seen above nice value is set to -10 for rfcomm.

Processes with small nice value(high priority) receive a larger proportion of processor timeslice (a slice of processor's time).


Nice value can also be set from the command line while execution. Refer to this link:
http://www.thegeekstuff.com/2013/08/nice-renice-command-examples/

2. Based on real-time priority

Default range is 0 to 99. Higher real time priority value indicate greater priority(Unlike nice values, where small nice value indicate high priority).


Great!! Now how how does the scheduler allocate time slice on the basis of nice/real time priority values?


Scheduler thread removes the highest priority context on the runqueue and schedules it.

        new = grab_runnable_context(ctx->prio + 1, spu->node);
        if (new)
                spu_schedule(spu, new);


spu_schedule() calls spu_set_timeslice to set the time slice using the formula: 
max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE) [2]

user-nice values [ -20 ... 0 ... 19 ] are scaled to time slice values [800ms ... 100ms ... 5ms]


[Note: In kernel 4.0.5, time slice values are: Minimum timeslice is 5 msecs, default timeslice is 100 msecs, maximum timeslice is 800 msecs. (Note: values are taken from sched.c)]


(kernel version: 4.0.5)

[1] net/bluetooth/rfcomm/core.c
[2] include/linux/sched/prio.h

No comments:

Post a Comment