4

I've just built and set up a vanilla Linux kernel with the RT patch applied. Everything went fine and I can now correctly boot into the new kernel.

What leaves me wondering is this: I have a simulator program that I've made in C, and I want it to execute in hard real time mode, as should be allowed by the new kernel. Probably the whole simulator doesn't need to be run with real-time priority, but some of the tasks inside do.

How can I accomplish this? I take it that simply running the program won't do.

jxh
  • 69,070
  • 8
  • 110
  • 193

1 Answers1

10

If you are asking how to run some of the threads in real-time context, and others as conventional time-sharing threads, then all you need is to set their schedulers properly using sched_setscheduler.

Time-sharing threads want to be SCHED_OTHER; real-time simulator threads want to be SCHED_FIFO or SCHED_RR.

On Linux, in order to run at real-time priorities, your user must have resource limits (man 2 rlimit) that allows this. In particular, your rtprio rlimit must be set to the highest priority you will need. Alternatively, you can run the application as root. In a linux system with PAM, this is typically accomplished by adding the appropriate line to /etc/security/limits.conf

    @realtime   -  rtprio     99

This will grant rtprio limits up to real-time priority 99 to the realtime group. Then you add a real-time group to /etc/groups and make sure your user is in the group.

(And since this appears to be your first time doing this, you may also want to have a "dead man's switch" high-priority real-time thread around to make sure that your simulator doesn't get out of hand and render the system unusable... if you are simulating high CPU load, you may get ACTUAL high CPU load and be unable to stop things without a reboot.)

SH'
  • 186
  • 2
  • 10
andersoj
  • 22,406
  • 7
  • 62
  • 73
  • Should not Alt+SysRq+N reset all real-time processes into normal mode? – Victor Vasiliev May 08 '12 at 22:38
  • @Victor: I haven't used that feature before and don't know how much to rely on it. Looks like it's used to renice everything and drop all rt priorities, but no idea how you'd get back to anything like a normal priority state after doing so. – andersoj May 08 '12 at 22:58
  • so let's say I've called in a newly forked process this command: sched_setscheduler(getpid(), SCHED_FIFO, &sp); why this doesn't get correctly excecuted? sched_setscheduler returns 1 instead of 0 and i'm pretty sure sp is configured correctly (sp.sched_priority set to 98) – Lawrence Ironjuly May 17 '12 at 12:11
  • You may not have permissions to call set_scheduler. Are you running as root? If that fixes it, then the Right Thing is to set up a group allowed to do realtime stuff, and put that in your limits.conf file. Quick example here: http://jackaudio.org/linux_rt_config – andersoj May 17 '12 at 12:44
  • 1
    Path to `limits.conf` on Ubuntu 16.04 is `/etc/security/limits.conf`(note the added `security` directory) http://manpages.ubuntu.com/manpages/trusty/man5/limits.conf.5.html – Toby Sep 13 '17 at 09:35