LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-11-2008, 02:42 PM   #1
Zagi
LQ Newbie
 
Registered: Jul 2003
Posts: 7

Rep: Reputation: 0
What is fastest way of exchanging data between child and parent process


Hi,

Does anyone knows what is the fastest way (less CPU usage) of sending data between child and parent process in c/linux.

/thanks,
 
Old 07-11-2008, 04:52 PM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Most PCs have one memory bank accessed by all processors, so if all you want is to have both processes see the same data then shared memory is your best bet.

I believe that the preferred way to use shared memory is to mount a ram disk on the hard drive (shmfs or tmpfs), and to have both processes read and write files on that directory (which physically exist in system RAM). That results in understandable and maintainable code, and can be easily ported to other systems (including mainframes, which may have much more complex memory arrangements).

But if minimum CPU is the goal then you may want to skip the virtual filesystem and access shared memory segments directly; there's quite a good tutorial on this here: http://www.cs.cf.ac.uk/Dave/C/node27.html. This is much more code to write than the filesystem approach, but has the advantage that there is no reading or writing to do; the data is simply there for both programs whenever it exists.

I should also note that the most "correct" way to share information is to use a pipe (named or anonymous). This has the advantage that you can't run out of memory: if the process sending information sends too much for the OS to buffer, then it'll simply slow down the sending process until the receiver has processed more. This is even simpler than the RAM disk approach, as it needs no setup other than that which the program can do.

The Documentation Project has further reading on IPC: http://tldp.org/LDP/lpg/node7.html

Hope that helps,

—Robert J Lee
 
Old 07-11-2008, 04:55 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Processes can open shared memory segments. See:

man shmat shmget

It is not necessary to create a file system entry.
 
Old 07-12-2008, 12:47 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Sharing memory is intrinsically fastest, but you have to be careful to set up a synchronization mechanism between parent and child, to make sure that data is only read/written when it should be, and to make sure your processes don't step on each other.

This will add overhead. You need to study semaphores and spinlocks. Pay particular attention to race conditions and potential non-atomic conditions (conditions that could be interrupted in the middle before completion).
 
  


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
how parent process can control it's child? sluge Linux - General 2 08-02-2007 04:31 AM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM
child and parent process error jdevanand Programming 1 04-29-2002 08:13 AM

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

All times are GMT -5. The time now is 07:36 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