Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel processing in linux

I'm not sure how to go about handling asynchronous tasks in a program I am writing and I'm hoping someone more experienced can at least point me in the right direction.

I'm running Angstrom Linux on an embedded ARM processor. My program controls several servos through exposed hardware PWM and a camera over PTP. Additionally it is socket daemon which takes commands from an arbitrary client (Android in this instance). The camera PTP is slow, and I don't want to wait around for it to finish its task because the rest of the program needs to be responsive.

I've tried threads, but any problems in the camera thread seems to kill the whole process. Ideally I want to send the camera off on its own to do its thing and when it is finished let the main function know. Is this an appropriate forking technique or have I implemented threading improperly?

Additionally, I would like to stay away from large secondary libraries to avoid any more cross compiling issues then I already have. Thanks in advance for any suggestions.

like image 682
Count Zero Avatar asked Jun 12 '12 00:06

Count Zero


People also ask

What is parallel command Linux?

GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables.

What is a parallel processing explain?

Parallel processing is a method in computing of running two or more processors (CPUs) to handle separate parts of an overall task. Breaking up different parts of a task among multiple processors will help reduce the amount of time to run a program.

What is parallel processing example?

In parallel processing, we take in multiple different forms of information at the same time. This is especially important in vision. For example, when you see a bus coming towards you, you see its color, shape, depth, and motion all at once. If you had to assess those things one at a time, it would take far too long.


2 Answers

Your problem sounds like a classic case for multiple processes, communicating with inter-process communications (IPC) of some sort.

The camera should have its own process, and if that process dies, the main process should not have a problem. You could even have the init(8) process manage the camera process; that can automatically restart the process if it dies for any reason.

You could set up a named pipe permanently, and then the camera process could re-open it any time it restarts after failure.

Here is some documentation about named pipes:

http://www.tldp.org/LDP/lpg/node15.html

I found this from the Wikipedia page:

http://en.wikipedia.org/wiki/Named_pipe

I searched StackOverflow and found a discussion of named pipes vs. sockets:

IPC performance: Named Pipe vs Socket

like image 142
steveha Avatar answered Oct 05 '22 12:10

steveha


Take the basic method of steveha's answer but skip the init(8) and named pipes.

fork() a child containing your camera code and communicate through regular pipes or domain sockets. Code a signal handler for SIGCHLD in the parent.If the child dies interrogate the reasons why with the return code from wait(). If it died on its own then cleanup and restart it; if it ended normally do what is appropriate in that case. Communicate with the child through whichever IPC you end up choosing. This give you more control over the child than init and domain sockets or pipes, in particular, will make it easier to set up and communicate between parent and child than messing with the funky semantics of FIFOs.

Of course, if there is really problems with the camera code all you have really done is make the failures somewhat more manageable by not taking down the whole program. Ideally you should get the camera code to work flawlessly if that is within your power.

like image 24
Duck Avatar answered Oct 05 '22 12:10

Duck