I am learning about Linux scheduler. Firstly, I want to test the FIFO scheduler. Here is the code I use to test:
#include <sched.h> #include <stdio.h> int main(int argc, char **argv) { printf("Setting SCHED_FIFO and priority to %dn",atoi(argv[1])); struct sched_param param; param.sched_priority = atoi(argv[1]); sched_setscheduler(0, SCHED_FIFO, ¶m); int n = 0; while(1) { n++; if (!(n % 10000000)) { printf("%s FIFO Prio %d running (n=%d)n",argv[2], atoi(argv[1]),n); } } }
I run this program with the priority 1 on 2 terminals: ./main 1
. Because my CPU has only 1 core, I expect only the first terminal can run because of the FIFO attribute. However, in the real-test situation: both terminal can run the code.
Here is my CPU information when I run the command lscpu
:
Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 1 On-line CPU(s) list: 0 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 85 Model name: Intel(R) Xeon(R) Gold 6140 CPU @ 2.30GHz Stepping: 4 CPU MHz: 2294.608 BogoMIPS: 4589.21 Virtualization: VT-x Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K
Please explain for me why.
Advertisement
Answer
You should always check the return value of system calls and the corresponding value of errno
when that return value is negative. In your case, you are likely not running the program with the right privileges. sched_setscheduler(2)
requires CAP_SYS_NICE
capability or running as root when you request scheduling policy SCHED_FIFO
with priority higher than the soft limit of RLIMIT_RTPRIO
, obtainable and settable using ulimit -r
. The typical value of this resource limit is 0, which forbids unprivileged processes from requesting the FIFO scheduling policy.
See here for a lot more details and here for the possible error values.