views:

353

answers:

3

I need my parent and child process to both be able to read and write the same variable (of type int) so it is "global" between the two processes.

I'm assuming this would use some sort of cross-process communication and have one variable on one process being updated.

I did a quick google and IPC and various techniques come up but I don't know which is the most suitable for my situation.

So what technique is best and could you provide a link to a noobs tutorial for it.

Thanks.

+2  A: 

If you need to share memory, perhaps using threads instead of processes would be a better solution?

Bus
Its an assignment for university. You have to use fork() specifically
Ben
+7  A: 

Since you are mentioning using fork(), I assume that you are living on a *nix-System

The primary way to share data between processes using UNIX IPCs are:

(1) Shared memory;

(2) Sockets:

There are other UNIX IPCs including

(3) Message Queues.

(4) Semaphores;

(5) Signals.

Your best best (for IPCs) is to use shared memory segments, based on your post. You might need to use semaphores to insure that the shared memory operations are atomic.

A tutorial is on forking and shared memory is on dev shed:

http://forums.devshed.com/c-programming-42/posix-semaphore-example-using-fork-and-shared-memory-330419.html

another more in-depth description of using multithreading (if appilcable for your application) can be found here:

https://computing.llnl.gov/tutorials/pthreads/

sum1stolemyname
Went with Shared memory in the end.
Ben
A: 

i would suggest you to use socket so that you can get a feeling of interprocess communication very well.

Renjith G
This does not solve the poster's problem. And sockets for IPC are a stream-based communication and require active actions like reading and writing.
DarkDust