bxshi
bxshi

Reputation: 2292

linux dynamic shared memory in different programs

I create a shared memory in program A with the following codes:

shm = shm_open("/mfs_hash_pool_container", O_CREAT|O_RDWR, 0666);

size = sizeof(struct mfs_hash_pool_container);

ftruncate(shm, size);

mfs_hash_pool_stat_p = (struct mfs_hash_pool_container *)mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm, 0);

I use that to store a hash table.

The other program B, will receives the addr (mfs_hash_pool_stat_p+offset) send from program A, but I can not write it in B. Does this means I must also open this shared memory in B? Is there any other way to solve that? Because I create this memory automatically.

Thanks you guys.

Upvotes: 1

Views: 9380

Answers (4)

Pavan Manjunath
Pavan Manjunath

Reputation: 28525

I am not sure about how your program A and program B are related, but if you manage to spawn 'B' from 'A', using fork() + execve() combination, then you need not worry about passing the memory pointer as both processes will have the same copy.

For your reference I am pasting a nice code example present at IBM developerworks here-

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/wait.h>

void error_and_die(const char *msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
    int r;

    const char *memname = "sample";
    const size_t region_size = sysconf(_SC_PAGE_SIZE);

    int fd = shm_open(memname, O_CREAT | O_TRUNC | O_RDWR, 0666);
    if (fd == -1)
        error_and_die("shm_open");

    r = ftruncate(fd, region_size);
    if (r != 0)
        error_and_die("ftruncate");

    void *ptr = mmap(0, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED)
        error_and_die("mmap");
    close(fd);

    pid_t pid = fork();

    if (pid == 0)
    {
        u_long *d = (u_long *)ptr;
        *d = 0xdbeebee;
        exit(0);
    }
    else
    {
        int status;
        waitpid(pid, &status, 0);
        printf("child wrote %#lx\n", *(u_long *)ptr);
    }

    r = munmap(ptr, region_size);
    if (r != 0)
        error_and_die("munmap");

    r = shm_unlink(memname);
    if (r != 0)
        error_and_die("shm_unlink");

    return 0;
}

Read the full article in the above link to gain a better understanding of shared memory!

Upvotes: 0

Dan Albert
Dan Albert

Reputation: 10499

Separate processes do not share memory, so the address being passed to B from A will not point to the shared memory segment. Each process must call shm_open() and mmap() the segment individually.

If you have information about the segment that every process needs to be aware of, pack it at the beginning of the segment in an order that each process is aware of.

Upvotes: 1

m0skit0
m0skit0

Reputation: 25873

Process do not share memory by default. If you want this 2 processes to communicate or share memory, you'll have to make that happen. Check this question.

Another solution is to use threads, which share code and memory alike.

Upvotes: -2

cnicutar
cnicutar

Reputation: 182639

You can't just use that address in the other program. B has to:

  • Obtain the file descriptor: shm_open("/mfs_hash_pool_container", O_RDWR, 0)
  • Map memory for the file descriptor: mmap just like A does

Notes:

  • You need to check the return value of mmap (it could return MAP_FAILED)
  • You need not cast the return value of mmap

Upvotes: 7

Related Questions