How to implement two periodical processes in C++ under Linux?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to implement two periodical processes in C++ under Linux?
Hello,
I am doing real time programming in C++, under Linux.
I have two process, let me say A and B. A process is being started periodically, every 5ms. B process is being started every 10ms. The process A is doing some change of data. The process B is reading that data and displays it.
I am confused about how to run periodically processes.
Is the only way to create child process? Can I create independent processes on some other way?
I am doing real time programming in C++, under Linux.
I have two process, let me say A and B. A process is being started periodically, every 5ms. B process is being started every 10ms. The process A is doing some change of data. The process B is reading that data and displays it.
I am confused about how to run periodically processes.
Is the only way to create child process? Can I create independent processes on some other way?
Tnx!
Independent processes are child processes of the shell you invoke them from.
Thank you for your suggestions!
I really need independent processes...
Must you start and stop, and then restart your processes, every 10ms and 5ms, respectively? Or can they be run once and remain dormant (ie in a wait state) for the aforementioned periods while they are not processing data?
Starting and stopping processes is major CPU-hog; you would be better off placing the processes in a wait state, and perhaps even better off utilizing a multi-threaded solution.
Your requirements are too vague to provide further assistance. If you plan to stick with independent processes, I would suggest you look into using shared-memory for the data.
To get periodical tasks, I'd follow dwhitney67's suggestion and use alarm(2) at the beginning of each computation period and send the process to blocked state e.g. by calling wait().
But the instant the SIGALRM occurs, the scheduler could STILL decide to first run another process out of the ready queue, so this doesn't qualify as a true real time system. If you need true realtime, you'll need a different scheduler (for example an implementation of rate monotonic scheduling) in the kernel.
I can run processes once, and then put them in blocked (wait) state.
Actually, the problem is that the period of process A should be as much as it is possible accurate (5ms). For the process B it isn't so important. So I don't know what is the best way to deal with precision. Is it complicated to implement some other scheduler? It seems to me complicated...
I have created independent processes, each in one .cpp file, and I am starting them from bash file. Is that OK? Then I can try to use shared memory for inter-process communication?
You can just test the alarm()/wait() approach, it should be "somwhat accurate" as long as the system isn't under heavy load. Just be sure to set the alarm() as the FIRST thing you do on each computational period, so you don't accidently add your own execution time to the period.
As for implementing an own scheduler: This is probably a LOT of trouble and only worthwile if you REALLY need TRUE realtime -- and you'll still face some problems like "stolen" cycles in management mode. If it's an embedded system whose realtime requirements are crucial for ppls health, go for it
Thank you!
Beacuse this is my first time working with timers of the POSIX, for the beginning I did something very similar to your code. I just used SIGEV_SIGNAL instead of SIGEV_THREAD.
Compaling was successful. But running not. I have some errors in object file: Undefined reference to 'timer_create' and Undefined reference to 'timer_settime'.
I really don't know what is the problem...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.