Shared Memory Between C Programs


 
Thread Tools Search this Thread
Top Forums Programming Shared Memory Between C Programs
# 1  
Old 04-13-2011
Shared Memory Between C Programs

I'm involved in a project with multiple teams which are developing C code for a Linux machine and we would like to have our program pass data to one of the other group's programs. The immediate idea is to have shared memory between the programs which would simply consist of variables whose size and offset from the beginning of the file are specified and have one program read from it while the other writes to it.

Will we run into any problems if we simply treat this file like any other file in our respective programs (i.e. fd = open(....) and just write to/read from fd at our leisure) or are there special steps that need to be taken in order to avoid simultaneously accessing the same data or something of that nature?
# 2  
Old 04-13-2011
Quote:
Originally Posted by cmcmanus3
I'm involved in a project with multiple teams which are developing C code for a Linux machine and we would like to have our program pass data to one of the other group's programs. The immediate idea is to have shared memory between the programs which would simply consist of variables whose size and offset from the beginning of the file are specified and have one program read from it while the other writes to it.

Will we run into any problems if we simply treat this file like any other file in our respective programs (i.e. fd = open(....) and just write to/read from fd at our leisure)
I don't see the point of using read()/write() if you're also mapping in shared memory. It still ought to work, I think, but it really defeats the point.

You don't need to hardcode offsets, either, you can use C's own complex data structures inside shared memory:

Code:
typedef struct mytype
{
      int type;
      char buf[512];
      float whatever;
} mytype;

...

mytype *mem=mmap(...);
mem[0].type=1;
strcpy(mem[0].buf, "this is a string");
mem[0].whatever=3.14159f;
mem[1].type=2;
strcpy(mem[1].buf, "this is another string");
mem[1].whatever=1.23456f;

The only caveat is that the structure can't hold pointers. Pointers from one process won't make sense in another. Even pointers to inside the shared area might not work since the shared area might not always end up in the same address in every process. If you need something like pointers, use long integers that mean the index of an array inside the shared memory (or just an offset from the beginning of shared memory).

Quote:
are there special steps that need to be taken in order to avoid simultaneously accessing the same data or something of that nature
yes, always, even if you were just read/writing the files all the time instead of mapping them.

Since you have the file open anyway, you can use POSIX advisory locking to control who gets to access the memory when. These functions will block when the section you ask for is busy with something else.

Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

// More than one thing can read simultaneously.
int readlock(int fd, int pos, int len)
{
        struct flock l;

        memset(&l, 0, sizeof(l));
        l.l_type=F_RDLCK;
        l.l_whence=SEEK_SET;
        l.l_start=pos;
        l.l_len=len;

        if(fcntl(fd, F_SETLKW, &l) != 0)
        {
                perror("Couldn't set rdlock");
                return(-1);
        }

        return(0);
}

// ...but everything has to wait while something's writing.
int wrlock(int fd, int pos, int len)
{
        struct flock l;

        memset(&l, 0, sizeof(l));
        l.l_type=F_WRLCK;
        l.l_whence=SEEK_SET;
        l.l_start=pos;
        l.l_len=len;

        if(fcntl(locked, F_SETLKW, &l) != 0)
        {
                perror("Couldn't set wrlock");
                return(-1);
        }

        return(0);
}

int unlock(int fd, int pos, int len)
{
        struct flock l;
        memset(&l, 0, sizeof(l));
        l.l_type=F_UNLCK;
        l.l_whence=SEEK_SET;
        l.l_start=pos;
        l.l_len=len;

        if(fcntl(locked, F_SETLK, &l) != 0)
        {
                perror("Couldn't unlock");
                return(-1);
        }

        return(0);
}

# 3  
Old 04-13-2011
Dude, Corona, your posts are always awesome!

Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Use programs in shared folder except ones in my home dir

I am using a cluster where all the programs are located in a shared folder (I can only read but not modify anything in this folder). The path of the share folder is in my .bashrc file (and thus also in my $PATH - first position): source /home/shared/bashrc But some of the programs are... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Use programs in shared folder except ones in my home dir

(0 Replies)
Discussion started by: beca123456
0 Replies

3. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

4. UNIX for Advanced & Expert Users

Shared hosting, how to install programs and libraries in your home folder

Hi all I hope I am posting in the right section. If not please excuse me and redirect me to the right section. Here is my problem: I am using a shared hosting plan at Godady. I have shell access and of course my own folder. I would like to be able to install programs in my own folder... (4 Replies)
Discussion started by: PiniFarini
4 Replies

5. UNIX for Dummies Questions & Answers

What programs access shared library file

I was curious how to tell which programs are accessing a file (libobjc.A.dylib) in /usr/lib This file seems to be the culprit in a bunch of Safari crashes, and I just wanted to know if and what other programs use it. Also, I was curious what a good way to find out what files are being written... (4 Replies)
Discussion started by: glev2005
4 Replies

6. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

7. Programming

Shared memory in shared library

I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

8. Programming

memory sharing - not shared memory -

hi, this is the problem: i want to swap a linked list between 4 processes (unrelated), is there any way i can do that just by sending a pointer to a structure? //example typedef struct node { int x; char c; struct node *next; } node; or i should send the items ( x,c ) by... (9 Replies)
Discussion started by: elzalem
9 Replies

9. UNIX for Advanced & Expert Users

Memory resident programs

How can you make a program as Memory resident in AIX. If I make a program as a memory resident program whether all the parts of the program like code and data (stack) segements of the program will be loaded in to the Memory. For Ex: I have a C code which is creating array of 10000 long ints... (4 Replies)
Discussion started by: Pola Balaji
4 Replies

10. UNIX for Advanced & Expert Users

Shared memory shortage but lots of unused memory

I am running HP-UX B.11.11. I'm increasing a parameter for a database engine so that it uses more memory to buffer the disk drive (to speed up performance). I have over 5GB of memory not being used. But when I try to start the DB with the increased buffer parameter I get told. "Not... (1 Reply)
Discussion started by: cjcamaro
1 Replies
Login or Register to Ask a Question