Week 3: Scheduling and nice
nice
Note
In fact, this struct ID is just the PID if it’s a process, and the TGID if it’s a thred (or kernel thread), but he doesn’t care. It’s the same for him
Threads are just tasks sharing resources.
We need a scheduler because we want to run things concurrently, and run more tasks than cores.
How do we decide which tasks get CPU time (go into the bar)?
With priority
, a number tied to each task
nice
commandWhen a program is nice, it leaves more time for the other programs, leaving more room to other ones. By using nice
, this is exactly what we do, make it “nicer” and alter it’s priority. In fact, the nice program code is really simple:
//...
ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0); // Syscall to setpriority
//...
execvp (argv[i], &argv[i]);
//...
Note
Internally, the priority is actually interpreted different, hence the system uses helper functions to translate between these two types of priorities.
static inline long nice_to_rlimit(long nice)
{
return (MAX_NICE - nice + 1);
}
/*
* Convert rlimit style value [1,40] to nice value [-20, 19].
*/
static inline long rlimit_to_nice(long prio)
{
return (MAX_NICE - prio + 1);
}
See more on the linux kernel sched code
Linux Scheduling Performance: https://www.youtube.com/watch?v=8g9fG7cApbc
Create your own linux scheduler with bpf: https://fosdem.org/2025/schedule/event/fosdem-2025-4620-rust-ifying-the-linux-kernel-scheduler-in-user-space-/
Linux niceness explained: https://www.youtube.com/watch?v=vd_oGmeoFKs
Understanding why we need an scheduler (the PCB named on this video is called task_struct
on linux): https://www.youtube.com/watch?v=O2tV9q6784k
If you really want to understand a little bit more what happens under the hood, you can do the following exercices. Be aware that you should read the “References” first, and then try to do this exercises.
Ready to have some fun? Check out the second part of the second AMSA activity here!
AMSA 2025-2026 - 🏠 Home