Click to See Complete Forum and Search --> : two c++ programs sharing same variables/memory
tecknophreak
10-13-2001, 06:44 PM
let's say i have two c++ programs, one which places chars into an array, and another who grabs those chars and places them on the screen or something. there also needs to be a boolean/char/int which will tell each program which one's turn it is. how do i set up the memory for this? i don't need the code, though i wouldn't mine, just send me in a particular direction
just so you know, this is a simplified version of the program i'm writing, since i know you can just do this in a single program.
would this be semaphores?
thanks
[ 13 October 2001: Message edited by: tecknophreak ]
lazy_cod3R
10-13-2001, 09:08 PM
If they are 2 totally differet programs, 2 separate processes the only thing i can think of is to use something like CORBA middleware to help you let the different process communicate to each other.
A solution would be to use a FIFO (first in, first out) aka a named pipe.
I haven't used fifo's in quite some time so excuse the rustiness of this reply, which can be remedied by looking at some man pages.
iirc, create a fifo with,
mknod("FIFONAME", S_IFIFO | 0644, 0);
where the last two arguments are the permissions, and the device number which is ignored for fifos.
with your first program, open the fifo the same way you would open any other file, and write your data into the file descriptor. with your second program, open it and read the data out, which hopefully will come out in the same order.
hopefully open will block until the other end of the fifo is opened . o 0
edit: clarification: the second argument is the only one ignored, sorry for the vagueness of that.
[ 13 October 2001: Message edited by: jkm ]
tecknophreak
10-14-2001, 02:45 PM
can both programs access the fifo at the same time? since you usually can't access fd's with more than one prog.
pinoy
10-14-2001, 04:35 PM
What you're basically trying to do is InterProcess Communication (IPC). There are several ways to achieve this, pipes, sockets, fifo's, shared memory, etc.
Semaphore is not an IPC mechanism instead it's a synchronization mechanism. It allows multiple threads/processes to use a shared resource.
Stuka
10-15-2001, 11:11 AM
I think that for writing to and reading from a single spot in memory, how you do it depends on what your code needs. If you need a spot in memory that all programs can access to read/write data as necessary, then shared memory and a semaphore might be what you want. If all you need is for one program to send data to another, a fifo would work. It really depends on what your needs are from a code perspective.
if you want to share memory explicitly between two programs, you could declare the variable as type volatile, which should allow it to be modified by external programs.
then you could just write out the memory address to a fifo, or whatever, and read the address in dynamically.
pinoy
10-15-2001, 04:35 PM
Originally posted by jkm:
<STRONG>if you want to share memory explicitly between two programs, you could declare the variable as type volatile, which should allow it to be modified by external programs.
then you could just write out the memory address to a fifo, or whatever, and read the address in dynamically.</STRONG>
This is not how volatile works. volatile just tells the compiler not to optimize this variable. e.g. when you need to know what the value of a volatile variable, the compiler will have to fetch it from memory and not return what's cached in the register for example. volatile is useful in cases where some other source might change the value, e.g. threads.
tecknophreak
10-16-2001, 01:49 AM
Thanks all.
What I've done, for now, is set up two progs to use semaphores to share some structs. If this is horribly wrong, let me know. In these structs I need to know if a buff is empty(the one has read it) or full(it has not been read yet). This is sort of a sychronization process. I'm up for suggestions since this is the first time I've used all of this.
pinoy
10-16-2001, 02:25 AM
how do you wish to synchronize the data? Can only one thread read/write the data at any one time? If so you're better of using a mutex. Do you have a reader thread and a writer thread? Use a condition and a mutex? Can multiple threads access the data at the same time, use semaphores. It is not very common to use semaphores, mutex is probably what you are looking for.
pinoy
10-16-2001, 02:27 AM
BTW, most of these are thread synchronization functions. I am not aware of process synchronization functions except for file locks. I guess it is possible to use these with shared memory.
My bad. Thanks for clearing that up.
<STRONG>This is not how volatile works. volatile just tells the compiler not to optimize this variable. e.g. when you need to know what the value of a volatile variable, the compiler will have to fetch it from memory and not return what's cached in the register for example. volatile is useful in cases where some other source might change the value, e.g. threads.</STRONG>
tecknophreak
10-17-2001, 12:36 AM
New update/change of code:
I'll be using threads instead of different programs, since they're supposed to work faster in theory, and prolly semaphores or mutex for synchronization.
I'll prolly have another Q tomorrow, thanks for all the help, I'm pretty new to RT programming.