0

I want to create some buffer in virtual memory and I want to use its related physical memory in another application.

#include <stdio.h> 
#include <sys/shm.h> 
#include <sys/stat.h> 

int main () 
{
  int segment_id; 
  char* shared_memory; 
  struct shmid_ds shmbuffer; 
  int segment_size; 
  const int shared_segment_size = 0x6400; 
  int key = 1234;
  segment_id = shmget (key, shared_segment_size, 
                 IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 
  shared_memory = (char*) shmat (segment_id, 0, 0); 
  printf ("shared memory attached at address %p\n", shared_memory); 
  segment_size  =               shmbuffer.shm_segsz; 
  printf ("segment size: %d\n", segment_size); 
  /* Write a string to the shared memory segment.  */ 
  sprintf (shared_memory,"%p" shared_memory); 
 while(1)){}
return 0;
}

In above example I have created buffer and the start address of that buffer I am passing to another application and I want to use that address as a starting address to other function in another application. Ex.

#include <iostream> 
#include <sys/ipc.h> 
#include <sys/shm.h> 
#include <stdio.h> 
using namespace std; 

int main() 
{ 
    int shmid = shmget(1234,0x6400,PC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 
    char *str = (char*) shmat(shmid,(void*)0,0); 

    printf("Data read from memory: %s\n",str); 
      /// USE THIS str (address passed from previous application)
    shmdt(str); 
    shmctl(shmid,IPC_RMID,NULL); 

    return 0; 
} 

Can I use the address which I am passing from shared memory in another application(above)?

I want to convert virtual address which I am getting in application from shared memory application translate it to physical memory and use.

Is this approach right? Any leads will be much helpful.

1 Answer 1

1

Shared memory can be accessed by key across different processes. When one creates with a key and IPC_CREAT, just using same key without IPC_CREAT can give access to the memory. Every process will have its separate virtual memory on same physical memory of shared memory. Passing address from one process to another is not required, just key is enough. If there is need to create block of virtual memory, just a guess that mmap() function can help but referring its man/info page will help.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You for reply Anand. Actually I want to allocate memory block for FPGA which is allocated by shared memory which will be shared memory application. Can I know which physical addresses both will share?
it should be same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.