Page 1 of 1

New Processes and Threads

Posted: Fri Jun 21, 2013 11:03 am
by mg169706
Hi,

I'm writing some code for my Pi and I need to launch a new binary and pass it some info to use during execution. I'd like to check my understanding of threads and processes before getting started. This is a new aspect of coding for me.

So I have some code which is all tested and is working just fine. Into this I want to insert some new code to launch the Pi's camera software, both the stills and video capture binaries (not at the same time though ) and also a binary that takes a string input and sends out an SMS via an attached 3G dongle.

I have tried and tested the three seperate binaries with much joy. Now I want to tie them together. From my research into threads and processes I'm pretty sure I want to launch the two binaries (camera and 3G) as processes and NOT threads. This being due to threads operating in the same address space and launching a new binary in this manner could cause the new application to overwrite data from the calling program. If I'm wrong or there are methods of doing this, please correct me. Examples/psudeo code would be a great help.

Based on the above I'm left with launching in processes. Most people seem to suggest doing this with fork() and exec(). This creates a duplicate of the calling process and then launches the new binary. Being a duplicate I guess it doesn't matter if anything from the [duplicated] calling process gets over written. The process does it's thing and then exits. Both process run concurrently on the processor and a block in one does not affect the other.

Is that last sentence true?

I'll be creating the child processes within a loop (probably a new process every 60 seconds). If the old process hasn't finished, possibly because it has become blocked) then I'll terminate the process from code and launch a new one. I don't want a bunch of blocked processes hanging around. For example, if there isn't a 3G connection, the binary for that bit tries to resend the sms. Whilst I can tell it not too I want the ability to kill the process incase something goes very wrong.

So my question is... does all that sound reasonible? Is my understanding correct? Is there a better way of doing it?

Ta muchly,

Ben

Re: New Processes and Threads

Posted: Fri Jun 21, 2013 12:39 pm
by DaveDriesen
I think your understanding of threads and processes is solid.

Basically you first create a duplicate of yourself, and then replace that duplicate with a new process (whatever that may be).
This means that in the split seconds after the fork, you need to check whether you are the parent or the child and branch off on that. Bob's your uncle.

So:

Code: Select all

// First fork, and keep the PID for killing later, or communicating etc.
child_pid = fork(); 

if (I am child process) {
   // This replaces the current child process with the specified program
   exec("whatever program", "arguments"); 
   // do error handling etc.
}

... For the parent, main program flow continues here...
What is the purpose of combining the components into a binary though? I mean, is it necessary like for interprocess communication?

Because tbh you may be better off scripting this solution (shell, perl, ...), as it will be more reliable and safe than building a binary that forks off additional software.

Personally if you're going to be forking and threading and whatnot, which is very cool, I think you might as well go the whole 9 yards and tap into whatever API your children leverage directly. It'll be more difficult, but also more cool.

Dave Driesen
Linux dev and oldskool elite

Re: New Processes and Threads

Posted: Fri Jun 21, 2013 7:42 pm
by mg169706
I've run into a new issue that I have no idea about how to solve. It works, but it doesn't. I'm using the following code to execute the Pi's camera to take a still photo:

execlp("/opt/vc/bin", "/opt/vc/bin/raspistill", "-o", "image.jpg", (char*) 0);

Beside the usual main() lines, that is all the program does. I know it seems stupidly simple, but it's just for proof of concept. The code runs. That is to say the program executes and launches a terminal window. The window reports that program terminated with result 0 (zero), so every went according to plan, but a photo was never recorded. The same command entered through the terminal window works just fine. So for some reason, executing it through another program is causing issues. Any ideas anyone?

Ben

Re: New Processes and Threads

Posted: Fri Jun 21, 2013 9:32 pm
by gordon@drogon.net
mg169706 wrote:I've run into a new issue that I have no idea about how to solve. It works, but it doesn't. I'm using the following code to execute the Pi's camera to take a still photo:

execlp("/opt/vc/bin", "/opt/vc/bin/raspistill", "-o", "image.jpg", (char*) 0);

Beside the usual main() lines, that is all the program does. I know it seems stupidly simple, but it's just for proof of concept. The code runs. That is to say the program executes and launches a terminal window. The window reports that program terminated with result 0 (zero), so every went according to plan, but a photo was never recorded. The same command entered through the terminal window works just fine. So for some reason, executing it through another program is causing issues. Any ideas anyone?

Ben
I think the first arg. ought to be: /opt/vc/bin/raspistill - as well as the 2nd arg.

the first arg is the pathname to the program and the subsequent ones are the arguments - noting that arguent 0 is traditionally the path to the file being executed, so:

Code: Select all

  execl ("/opt/vc/bin/raspistill", "/opt/vc/bin/raspistill", "-o", "image.jpg", (char*) 0);
Also, execl() is marginally faster here, execlp () will search your $PATH for the program - not needed as you're specifying the full path.

-Gordon