Technical skill

Linux Process: planning

How Linux shares processor time

Processor time is limited, so runnable threads take turns. Linux's scheduler decides which ready thread gets the next turn. With multiple logical CPUs, several threads can run at once, one on each available logical CPU. A process is a running program; it may contain one or more threads, which are separate paths of work inside that program. A thread waiting for a file, network response, or lock is not using CPU in the same way as one ready to run. With multiple logical CPUs, several ready threads can run at once.

If a long build should yield more readily while other work needs the processor, start it with a higher nice value:

nice -n 10 ./build-index

A nice value affects the relative scheduling weight of ordinary tasks when they compete for CPU. Linux uses a conventional range from -20 to +19: a lower value is less nice to other tasks and gets relatively more weight; a higher value gets less. Nice does not reserve a fixed share, promise a deadline, or make a task run faster when nothing else is competing. The command's -n setting adjusts from the inherited value, so inspect the resulting value rather than assuming a particular one.

Change a running task

Use ps to inspect a process and renice to adjust its nice value. For example:

ps -o pid,tid,stat,ni,psr,comm -p 1234
renice 10 -p 1234

The columns show process ID (PID), thread ID (TID), state, nice value (NI), the processor last used (PSR), and command name. 1234 is an example process ID; substitute the current one. renice 10 -p 1234 requests a nice value of 10. Permission depends on the change requested and the machine's scheduling limits. Check the output and resulting value: the request itself does not say how the scheduler will behave.

CPU affinity controls which logical CPUs a thread may run on. That is a placement restriction; nice changes relative scheduling weight. taskset can display or set an affinity mask. Containers and control groups can add limits or shares that also affect CPU use.

Distinguish ordinary and real-time scheduling

Most ordinary Linux tasks use the time-sharing policy called SCHED_OTHER in interfaces and SCHED_NORMAL in kernel code. Kernel scheduling details can change between versions, so a description of a particular algorithm is not a permanent interface.

Linux also has real-time policies such as SCHED_FIFO and SCHED_RR, and deadline-scheduling interfaces. They have separate rules and permissions. A real-time task can starve ordinary work if configured carelessly. Choosing a real-time policy does not, by itself, make an application meet its deadlines.

To investigate a slow task, inspect state and CPU use with ps, top, or pidstat; use chrt -p PID to check policy details. High CPU use means a thread used CPU during the sample, but does not prove its priority is wrong. The thread might be waiting for storage, limited by affinity or a control group, or blocked on another resource. Compare its behavior at the default setting and at a modest positive nice value while other work competes. Measure elapsed time and responsiveness; the result depends on hardware, kernel, and workload.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful