C / C ++ - search for real-time time events under Linux

I am looking for a way to do synchronization events in C / C ++ under Linux.
For instance. execute a piece of code every 100 milliseconds. (No more, no less, soft in real time)

The only options I've seen so far:
- The cycle is endless, and check every time if it is time to complete.
- A sleeping stream until the time comes.

Endless verification, if it is time to start, has the disadvantage that it is too intense. Sleeping, until this is the time, has the disadvantage that it is not accurate. Sleep time is just a guide; he can sleep much more.

Now I was wondering if anyone knew how to do real-time synchronization events in C / C ++ under Linux.

Thanks,
Dennis

+4
source share
5 answers

It is almost not important what you are going to use - a select() , epoll() , usleep() , settimer() . Some methods may take a higher resolution timeout; some may not. But Linux is not an operating system and should not even expect it to wake your process at this time. Now, even if you start the processor, the kernel can easily pause your process if it decides to allocate this processor for some other process or interrupt handler.

From the point of view of the API (as well as the latest trends), you should probably stick to timerfd() - you can tell what clock it goes well with other Linux event dispatch mechanisms (epoll, AIO, etc.).

Other than that, its out of your control as a programmer. It usually takes a lot of effort to get closer to real Linux behavior. It ranges from creating a customizable fixed kernel and proximity settings for the whole world, including interrupt protection, BIOS fix, and hardware tuning.

Be that as it may, assuming you are launching a new vanilla core on the x86_64 product server, which is not very busy, expect your wake-up time to be somewhere around 50 microseconds (you can easily compare it to get a rough idea for your system).

+4
source

Operation in an OS other than real-time is not reliable. You are right that the dream is inaccurate, and you can oversleep, but the same applies to the cycle: the OS cannot schedule your task on time, and you will miss your slot.

Depending on how much you need it, consider a real-time operating system (RTOS) such as QNX or RTLinux . The RTLinux wiki is a bit dated, so I'm not sure how active it is.

Another approach is to rely on time for some hardware devices (such as a GPS map). Your program will respond to an external event, and not try to time itself. For example, a hardware device may set a β€œfile”. Your program will open it and block it on read() . Every 100 ms, the hardware device sends you some data.

+3
source

You can try RTAI , the real-time application interface for Linux.

[...] which allows you to write applications with strict time limits for your favorite operating system. Like Linux itself, this software is a community.

From the beginner's guide:

RTAI offers the same Linux kernel services, adding the features of an industrial real operating system . It consists mainly of an interrupt manager: RTAI basically interrupts peripherals and redirects them to Linux if necessary. This is not an intrusive kernel modification; he uses the concept of HAL (layer of hardware abstraction) to get information from Linux and capture some fundamental functions. This HAL provides several dependencies on the Linux kernel. This leads to a simple adaptation in the Linux kernel, an easy RTAI port from version to version of Linux, and easier use of other operating systems instead of RTAI. RTAI sees Linux as a background task to perform when there is no real-time activity.

+1
source

check libevent: http://libevent.org/

The libevent API provides a mechanism for executing a callback function when a specific event occurs in a file descriptor or after a timeout is reached. In addition, libevent also supports callbacks due to signals or regular timeouts.

0
source

You can also consider Linux-specific timerfd_create (2) - and friends - (with poll (2) for multiplexing / waiting, or some kind of event library like libev or libevent , or Gtk or Qt event loops that will trigger poll inside) or Posix timer_create (2) . See Also clock_gettime (2) and friends

I highly recommend reading the time (7) man page. That explains a lot. And since Vlad answered , Linux is not a true real-time kernel (even if it may be close).

0
source

All Articles