So basically, if I program something and somehow set it to be "realtime" then I can be sure that it won't be suspended before it finishes whatever it is doing?
Such a condition cannot be kept indefinitely - soon or later there will be activities that will need the OS' attention (therefore my program will have to be suspended), no? (If yes then I have problems understanding the threshold...).
Not necessarily. The OS will provide guarantees about how when the program will be suspended and for how long. The strength of those guarantees will vary between systems, and it is a fuzzy line to decide at what point you have a realtime system.
One example of this is cheap smartphones. Most phones contain a separate baseband processor to control the phone's radio. Because of how critical timing is to radios, these processors will typically run some sort of realtime system. However, modern processors are powerful enough to run both the baseband code, and Android on the same processor.
The difficulty in doing so is that Android is very much not a realtime system, so trying to run the radio software as, say, a Linux kernel driver would be impossible.
The solution is to run both Android and the baseband under a realtime hypervisor. The hypervisor would then guarantee, for example, that every millisecond it will switch to the baseband code. The baseband still gets suspended while Android is running, but because it knows exactly how long it will be suspended and how long it has to do its work while it is not suspended, it is possible for it to do its timing critical job.
You could view this as the baseband having a lot of short-lived "jobs" that it finishes each time before loosing control. Another way to think about it (that is typically more realistic) is that the baseband has 1 longrunning job that involves a fair bit of waiting; and it can be suspended while it is waiting.
Depending on your architecture, you might have the baseband coorporativly suspend itself, or have the hypervisor preemptively suspend it (with well defined timings that the baseband was designed to work with)
Not entirely true. Although Linux is not typically real-time you can achieve this by using SCHED_FIFO or SCHED_DEADLINE for a given process/thread.
Android in fact does this for things like audio or display composition.
""" SCHED_FIFO and SCHED_RR threads will run until one of the following events occurs:
- The thread goes to sleep or begins waiting for an event
- A higher-priority realtime thread becomes ready to run
If one of these events does not occur, the threads will run indefinitely on that processor, and lower-priority threads will not be given a chance to run. This can result in system service threads failing to run, and operations such as memory swapping and filesystem data flushing not occurring as expected. """
SCHED_DEADLINE, which is newer and not mentioned in the guide, can provide a bit stronger guarantees because it can fail if it thinks the requirements you set aren't actually obtainable based on other system load (including high priority kernel tasks that might need to run, etc).
[1]: https://access.redhat.com/documentation/en-US/Red_Hat_Enterp...
It's tricky though because even things like dynamic memory allocation can have non-realtime behavior.
That's not why they avoid syscalls. If you are running on a real time schedule there won't be a context switch in response to a syscall. They may, however, get blocked on kernel mutexes. So syscalls are avoided for the same reason user-space mutexes are avoided - being real-time doesn't help if you're blocked on a mutex.
I guess that context switches would have to happen if e.g. 2 realtime programs, with the same priority etc..., would run concurrently on the same CPU - or would "the running program" (the one that managed to start first) be allowed by the OS(/HW?) to take total ownership of the CPU until it finishes running?
The point of a realtime OS is not to make your programs run in real-time, but to make it possible for your programs to run in real-time. This is simply not possible on a general OS like Linux, because the kernel can always to do something else while your program is running.
Consider the software that drives the printhead in your printer. The timing here is critical. If a command gets sent too early or too late, the print will not come out right.
You probably don't want the OS to be what tells you to send a command every hundred millisecond. Getting this timing right is a core feature of your application, all you need from the OS is to not get in the way of your getting the timing right.
If you are in a state where you need to send a partial result then the program computing the result is likely not really realtime.