-1

I have a process that dived itself with fork. I need to create a region of memory (a matrix) for the result of the computation of each process. How can I do this? Everything I tried or I can use but it's not shared between processes or I can't use (not sure if shared or not). Someone knows what I can use? It can be something simple and without any security. The simpler the better. I tried shmget but it's not sharing and I couldn't get how to use mmap to allocate or use it correctly. I tried other estranges things, but nothing. Any tips?

Some tries:

segment_id = shmget(IPC_PRIVATE, (sizeof(int) * linhas_mat1 * colunas_mat2) ,  S_IRUSR|S_IWUSR);
matriz_result = (int **) shmat(segment_id, NULL, 0);

Forks after that. Each process can use the matriz_result normally as a matrix, but the memory is not shared. Each one has one like a local variable.

segment_id = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
matriz_result = mmap(NULL, (sizeof(int) * linhas_mat1 * colunas_mat2), PROT_READ | PROT_WRITE, MAP_SHARED, segment_id, 0);

Tried this with mmap, but I don't know if it's right. I'm not good with such low level programming and I couldn't find any good example on how to use it correctly.

declarations:

int segment_id is;
int **matriz_result;
Erik Scheffer
  • 101
  • 1
  • 2
  • 7

1 Answers1

2
int createMemShare(){
    //File descriptor declaration: 
    int fd;
    //We want to open the file with readwrite,create it, and empty it if it exists
    //We want the user to have permission to read and write from it
    fd = open(MEMSHARENAME, O_RDWR| O_CREAT | O_TRUNC, S_IRUSR| S_IWUSR );
    if(fd <= 0){
         puts("Failed in creating memory share .");
         return -1;
    }
    //Move the file pointer and write an empty byte, this forces the file to
    //be of the size we want it to be.
    if (lseek(fd, MEMSHARESIZE - 1, SEEK_SET) == -1) {
         puts("Failed to expand the memory share to the correct size.");
    return -1;
    }
    //Write out 1 byte as said in previous comment
    write(fd, "", 1);

    //Memory share is now set to use, send it back.
    return fd;
}

//Later on...
int memShareFD = mmap(NULL, MEMSHARESIZE, PROT_READ, MAP_SHARED, fd, 0);

//And to sync up data between the processes using it:
//The 0 will invalidate all memory so everything will be checked
msync(memshareFD,0,MS_SYNC|MS_INVALIDATE);

you can try the above function to create a shared memory space. Essentially all you need to do is treat it like any other file once you've made it. The code example on the man page is pretty complete and worth a look into: check it out here

Edit: You'd probably be better off using shm_open as Jens Gustedt suggested in the comments. It's simple to use and simpler than making the file yourself with the function I've written above.

Community
  • 1
  • 1
EdgeCaseBerg
  • 2,761
  • 1
  • 23
  • 39
  • Tried with `shm_open` and with your function. Still nothing. It gives segmentation fault the first moment it tries to write in the space. I need to call some some function to write on it? – Erik Scheffer May 07 '13 at 16:08
  • Just to clarify, You want to create a matrix, then have each process use this matrix during some calculations? – EdgeCaseBerg May 09 '13 at 04:36
  • Also, why are you using fork instead of using threads? Have a program that does computation and stores the result somewhere in parallel sounds like a good place to use threads to me. – EdgeCaseBerg May 09 '13 at 05:49
  • [this example here](http://www.jimscode.ca/index.php/component/content/article/13-c/45-c-simple-mmap-example) shows a way to mmap a file and write to it from a forked process. The first bit of code is basically my createMemshare function, from there on he essentially treats the memory as a large contiguous block of space to write to using pointer arithmetic. You can access this like a matrix in the same way you could for indexing a multi-dimensional array from its base. (x*rowwidth + y) I can edit my answer and provide a code snippet for that if you'd like? – EdgeCaseBerg May 09 '13 at 06:07