How to share memory in C?
How do we share variable between two processes in C? Thanks in advance.
In what OS?
In Linux/UNIX, there are all kinds of possible ways to do this. Consider the entire subject of InterProcess Communication (IPC), which includes pipes, shared memory, and UNIX domain sockets.
In DOS/Windows, I'm not sure what there is. I think that there might be a form of shared memory in Win32 in the form of memory-mapped files (I think, so don't quote me on it).
In either case, you could always use a disk file. In Linux/UNIX, you have file-locking that prevents the one process from reading the file at the same time the other process is writing to it. I don't know whether Win32 has it.
Hi, I'm working on the client/server program in C in Solaris system. One of the the programs is calling fork so that it enables the multithreaded servers. The problem is when I call fork, the variables that are needed to be updated between processes cannot get the latest value. Don't worry about synchronization point/mutual exclusion part first. The problem right now is I don't know how to make global variable between two processes. If I am not wrong, sharing variables between two variables between two files just need to initialize variable by adding "extern." Does anyone has a solution of how to share variable between two processes? Can we save variable in socket? But, writing and reading from socket in the same program? How to achieve that? Is there any function in C or library that could enable sharing variable between processes? I hope it's clear for you guys now. I really need help right now. Thanks.
I believe that POSIX IPC (interprocess comm) is available under Solaris. It is a subject that can very easily fill a book.
Basically the same thing that you do with the | on the command line. Just before you fork, call the pipe() function with an array of two file descriptors (fd's). It will return in that array the read and write fd's for the pipe. Then after the fork the child has the same fd's, so the parent and child can pass data to each other via the pipe.
If the two processes are unrelated, they can use a named pipe, which is created with the mkfifo() function. That pipe exists within the file system. We didn't discuss named pipes much, so I don't know the pros and cons.
You can also use shared memory with the functions shmat, shmctl, shmdt, and shmget. In this case a block of memory is created with shmget and made available to a process by attaching to it with shmat.
UNIX Domain sockets:
If you are familiar with sockets programming, then create the socket within the UNIX domain instead of the internet domain; i.e., AF_UNIX instead of AF_INET.
These should give you the keywords you need to find your solution. Like I said, it's a large subject area that requires a book or a large portion of a book.
You can directly apply low-level I/O to pipes and sockets; ie, the functions open, read, write, close. We didn't have time to play with shared memory, but I'm sure there are plenty of examples out there.
Last edited by dwise1_aol; May 26th, 2003 at 12:59 PM.