Is there any possible way to change a memory location to a shared memory in C?
In c you can do
shmid = shmget(SHMEM_KEY, sizeof(int*) * n , SHEMEM_MODE | IPC_CREAT);
int* shmem = shmat(shmid, N开发者_StackOverflow中文版ULL, 0);
to assign first given free memory space as a shared memory.
Is there any way to assigne current memory space as a shared memory?
You use shmat()
to alias the shared memory you created to any arbitrary page-aligned range in your address-space
So this isn't taking some memory you already have and publishing it; its taking some new shared memory, you then copy what you want to publish across, then use shmat
to alias it to where you had what you wanted to publish - this has the same effect.
First I would advise not to use the oldish shmget
interfaces but the
more modern POSIX interfaces shm_open
and mmap
(if you have them,
you didn't specify your system)
Then the mmap
allows you to propose an address in your address space
where you'd like to have the segment
this is not exactly what you are asking for, but the closest you can get, I think
精彩评论