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.