LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2010, 11:48 AM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165
Blog Entries: 1

Rep: Reputation: 22
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.
 
Old 10-22-2010, 02:14 PM   #2
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Rep: Reputation: 1
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
 
Old 10-22-2010, 02:32 PM   #3
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165

Original Poster
Blog Entries: 1

Rep: Reputation: 22
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.
 
Old 10-22-2010, 11:18 PM   #4
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Rep: Reputation: 1
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.
 
Old 10-23-2010, 04:18 AM   #5
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by FatalKeystroke View Post
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.
 
Old 10-23-2010, 11:29 PM   #6
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Rep: Reputation: 1
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.

Last edited by FatalKeystroke; 10-23-2010 at 11:36 PM.
 
Old 10-24-2010, 07:09 AM   #7
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by yaplej View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use multiple audio devices simultaneously mittfh Linux - Software 2 10-31-2009 08:44 PM
multiple tcpdumps running simultaneously? genmaicha Linux - Networking 1 07-22-2009 02:16 AM
multiple pppoe sessions simultaneously zomane Linux - Networking 0 04-21-2008 05:09 PM
Multiple Blade servers booting from multiple disk drives simultaneously NGC_cheryl Linux - Enterprise 0 11-26-2007 08:38 AM
semaphore with multiple tasks Rama Anandakrishnan Linux - Kernel 3 08-01-2007 06:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration