1

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.

bxshi
  • 2,252
  • 5
  • 31
  • 50

4 Answers4

7

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
cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

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.

Dan Albert
  • 10,079
  • 2
  • 36
  • 79
0

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!

kube
  • 13,176
  • 9
  • 34
  • 38
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
-2

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.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • The OP is clearly aware that processes do not share memory by default, that's why he's trying to use shmem. – jupp0r Feb 23 '12 at 08:20