LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to make a C app do multiple tasks simultaneously? (https://www.linuxquestions.org/questions/programming-9/how-to-make-a-c-app-do-multiple-tasks-simultaneously-839801/)

yaplej 10-22-2010 11:48 AM

How to make a C app do multiple tasks simultaneously?
 
I have been working on a project for a while now that involves a rather complex daemon that has to be simultaneously running different tasks.

For example one of these tasks is to receive IP packets from the Netfilter queue and place those packets into one of several internal queues. While other tasks involve taking IP packets from those internal queues and processing/manipulating them and finally returning them back to the Linux network stack.

As I as I have no previous C experience before starting this project I just spawned a new thread each time I needed the daemon to be doing something else while those other tasks continued to run. Is there any other way of doing this or is this pretty much the only way of doing this? Because C is procedural I could not figure out any other way of doing accomplishing what I wanted.

Should I have done it some other way or is this the correct and only way I could have gotten my C app to be running multiple tasks at the same time?

Thanks.

FatalKeystroke 10-22-2010 02:14 PM

Use "fork()"

http://www.csl.mtu.edu/cs4411/www/NO...rk/create.html
http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
http://uw714doc.sco.com/en/SDK_syspr...on_-_fork.html

yaplej 10-22-2010 02:32 PM

I had not really heard of fork() before so thanks.

Looking over that documentation though it looks like that fork() creates two totally isolated processes. So the forks cannot share memory with the parent. In a fork any variable modified in the child will not me modified in the parent as its a complete copy of the parent. Thus any variable modified in the partent is not modified in the child.

My application requires that the processes be able to access global varibles and move data between them so if fork() is the only other option then POSIX Threads seems like they were the correct choice.

FatalKeystroke 10-22-2010 11:18 PM

This isn't sharing the same memory space, but you could always use sockets to transfer the data between the parent and child processes.
It's what I use when I need to share data between parent and child, but I also have a C++ class that I made for that. So it may be more difficult to implement with C.

rupertwh 10-23-2010 04:18 AM

Quote:

Originally Posted by FatalKeystroke (Post 4136163)
Use "fork()"

I don't know why people are trying to promote forking as a general purpose alternative to multithreading.
As the OP has corretly noticed, those two are (from a developer's standpoint) two completely different things -- even though on Linux they are technically more similar than one would expect or hope for.

Some applications will lend themselves to be done with multithreading, some with multi processes, and some equally well to both.

Then again, some (many?) developers will forever be limited to forking, as they simply cannot wrap their mind around multithreading and its pitfalls.

But an unqualified "use fork()" is just -- wrong.

FatalKeystroke 10-23-2010 11:29 PM

I apologize if I provided an inadequate answer.
I have been programming on Linux for a couple years now, however I have no real "formal" education in programming and am mainly self taught. I took some programming classes in high school, but due to budget cuts, I was left with unqualified teachers who only had the experience of your average Windoze user and had read a VB textbook. Now I am in college and hope to get some better instruction.
I gave the best answer I knew of and I thought that fork() was what the OP was looking for. If I was wrong I apologize. My mis-recommendation was what I believed to be correct based on my knowledge and experience.

I will research and try out multithreading as I was unaware of an alternative to fork(). Thank you rupertwh, for revealing to me that there is an alternative.

JohnGraham 10-24-2010 07:09 AM

Quote:

Originally Posted by yaplej (Post 4136170)
My application requires that the processes be able to access global varibles and move data between them so if fork() is the only other option then POSIX Threads seems like they were the correct choice.

You're forgetting that you may be able to do all your work from inside a single thread - don't leave it out as an option. As has been noted, threads have their pitfalls and if you do everything in a single thread you won't have mutexes to deal with and none of the (sometimes very hard to trace) bugs that come with multi-threaded programs.

Although I have no experience with netfilter, there will most likely be a non-blocking method to check whether there's data to be read, and you can then process the incoming data if there is any, or go on to the other tasks if there isn't.


All times are GMT -5. The time now is 07:40 AM.