2

In c you can do

shmid = shmget(SHMEM_KEY, sizeof(int*) * n , SHEMEM_MODE | IPC_CREAT);
int* shmem = shmat(shmid, NULL, 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?

jschmier
  • 15,458
  • 6
  • 54
  • 72
Anatoli
  • 922
  • 2
  • 11
  • 25
  • I wouldn't think so. I think (but don't quote me) that the difference between private and shared memory is where it is located. So I don't think that you can "assign" A memory location as shared. But, I might be wrong. – Alexander Rafferty Sep 08 '10 at 08:14

2 Answers2

1

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.

Will
  • 73,905
  • 40
  • 169
  • 246
0

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

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177