I'm trying to assign two real time processes to two cpus(one each). I'm using SCHED_FIFO scheduling policy. I'm doing the following things:

  • adding "quiet splash isolcpus = 6,7" at GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub to isolate cpus. I rebooted the system and it seems to work except a few kernel threads running on 6 and 7 cpus(I did that using top command).
  • Then this:

    retval = sched_getattr(0, &attr, sizeof(struct sched_attr), 0);

    if(retval < 0){perror("Error in get attr:");return0;}

    attr.sched_policy = SCHED_FIFO;

    attr.sched_priority = 1;

    retval = sched_setattr(0,&attr,0);

    if(retval<0) { perror("Error setting sched params: "); return 0; }

    retval = sched_setaffinity(0,sizeof(cpu_set_t),&set);
        if(retval<0){perror("Affinity Error ");return 0;}

    I intend to run this process on cpu7. But I'm not able to do so. Why is it failing? Can you suggest other ways if it does not work this way?

Edit: I have not created any extra cpusets. There is only one cpuset directory in /sys/fs/cgroup


Try to figure out, which error is returned. See

man 2 sched_setaffinity 

Aside from that, you (the binary) need the capability "CAP_SYS_NICE" to change the affinity. So you must

  • run the program as root, or
  • set this capability on the binary (note: does not work on NFS mounted filesystems)

Setting the needed capabilty on the binary is done with

sudo setcap CAP_SYS_NICE+e <file>
| improve this answer | |
  • Thank you for responding. I've tried both the options you've mentioned. But no luck. The code is compiling without errors. On the other hand, I've written a program without any SCHED_FIFO/SCHED_DEADLINE and used CPU_SET and sched_setaffinity(). It was working. – Venu Aditya Feb 20 '19 at 10:15
  • FYI, I use sockets and shared memory. Is that the reason why sched_setaffinity() not working? – Venu Aditya Feb 20 '19 at 10:17
  • In the command "sudo setcap CAP_SYS_NICE+e <file>" , I think by file you meant the name of the executable. – Venu Aditya Feb 20 '19 at 10:22
  • Yes, the executable. Try to manually set the affinity on the running process from the commandline: $ > taskset -p <pid> # get $ > taskset -pc 4 <pid> # set to cpu 4 – gerhard d. Feb 20 '19 at 13:05
  • Does it work when run as root? – gerhard d. Feb 21 '19 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.