dukenukem
Posts: 3
Joined: Tue Jul 22, 2014 9:03 am

Auto run 2 different programs at boot

Tue Jul 22, 2014 9:23 am

Hi everybody,
I would like to use a Raspberry Pi in an embedded way in a project: the Raspberry has to auto-run two C programs at boot and periodically (every 50/100ms) run those programs, for its entire life.

What is the best way to do so?

I am able to auto run programs at boot modifying the rc.local file , but this will execute those program only once. How to periodically execute them?

In addition, one of those C program should be activate only when an interrupt is detected on a GPIO pin. Is it possible to obtain this behaviour?

Thank you!
Tommaso

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Auto run 2 different programs at boot

Tue Jul 22, 2014 10:07 am

every 50 / 100 ms is pretty damn fast ...

but if you have a script that calls your stuff like
#!/bin/sh
while [ 1 ] ; do
/home/pi/proggy
done

then that will run for ever

you can use interrupts to select your code etc....
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Auto run 2 different programs at boot

Tue Jul 22, 2014 10:12 am

How much jitter is acceptable to you ? Can you deviate from the 50/100 ms interval ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

dukenukem
Posts: 3
Joined: Tue Jul 22, 2014 9:03 am

Re: Auto run 2 different programs at boot

Tue Jul 22, 2014 10:47 am

Well every 50ms a pin is raised from a micro controller to a GPIO pin of the Raspberry, this pin should generate an interrupt in the Raspberry (that in the meantime is running the other C program) and they should communicate in SPI (this communication lasts for about 2 ms), subsequently this data has to be elaborated but in very simple way, so i think it won't take much time.

The other program should be executed every 100ms but this is not a strict obligation, a 200/300ms period is ok.

For the 50ms interrupt i am now reading some infos about the wiringPi library, i think it should work, doesn't it?

Another way could be a 'concurrent' execution of these 2 programs and the first one reads that pin in a polling way, but i am afraid this could decrease too much the entire system speed.

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Auto run 2 different programs at boot

Tue Jul 22, 2014 12:22 pm

Why cant the program just run continuously and do its stuff when it needs to, you can control timing much better from inside a running program.

- When should I do stuff: T2
- What the time now: T1
- Lets wait: T2 - T1
- do stuff.

And the other process handling the GPIO interrupt could in theory run in a seprate thread in the same program.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Auto run 2 different programs at boot

Tue Jul 22, 2014 3:26 pm

Two ways: in the script, use sleep to pause until you do it again. The other way is to use crontab, but that has a minimum of 1 second. I recommend the first.
There are 10 types of people: those who understand binary and those who don't.

dukenukem
Posts: 3
Joined: Tue Jul 22, 2014 9:03 am

Re: Auto run 2 different programs at boot

Wed Jul 23, 2014 7:33 am

I have no experience in this field but it seems to me that two feasible ways to run two application in a concurrent way are:

1) a shell script that periodically calls those programs. Here i don't get how to handle the interrupt wake up of one program and the periodic execution of the other (is it possible to use a timer in a shall script?????).

2) the other way is writing an entire program with multiple threads. I suppose it is the easiest and faster way but, what i don't like of this solution is the fact that those 2 programs have nothing in common, they don't share any variable, so i would like to keep them separate.

Does it make sense what i have said? Are there other solutions?

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Auto run 2 different programs at boot

Wed Jul 23, 2014 7:50 am

personally,
for interrupts - I'd have the program running all the time and act on those interrupts to do what it needs to do
for the other that just runs periodically then depending on what it neds to do and how often and to what degree of accuracy
cron has a 1 minute min period
anything shorter then you need to either loop a script - with suitable delays
or write a program that does it .
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Auto run 2 different programs at boot

Wed Jul 23, 2014 8:14 am

Don't use a script , but a third program to spawn the others - a C program should give you much better control than a Bash script.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Return to “General discussion”