Grupos de Google ya no admite publicaciones ni suscripciones nuevas de Usenet. El contenido anterior sigue visible.

size argument to shmget()

658 vistas
Ir al primer mensaje no leído

Numeromancer

no leída,
13 nov 2008, 3:25:13 p.m.13/11/08
para
I'm developing a system using shared memory segments between processes. On Linux.

I was looking in the POSIX standard to find how to connect to an existing shared
memory segment whose size is unknown to the client, and realized that it gave no
clue is to what shmget() does with the size argument when it connects you to an
existing segment. There are hints that it ignores it, but it says nothing
certain from which I can deduce that it ignores the size.

It is possible, as far as I can tell, that if the size argument is less than the
size of the segment, the pointer that shmat() returns could be valid only for
the size argument to shmget(), and that if this is less than shmem_id.shm_size,
trying to access memory between (p + size) and (p + shmem_id.shm_segsz) could be
undefined (p as returned by shmat()). Nothing in the standard, or the linux man
page for shmget(), or anything I could find on the net, affirms or refutes this.
In fact, I could find no example of this kind of use. Empirical evidence
suggests that accessing the memory in this range is fine (ie it works for me),
and there is a hint in the posix standard that size can be zero if the segment
exists, which suggests the same. But nothing for sure.

The only well-defined way to get and use a shared memory of unknown size, then, is

int shmid = shmget(key, 0, ...);
shmctl(shmid, IPC_STAT, &shm_id);
size_t realSize = shm_id.shm_segsz;
shmid = shmget(key, realSize, ...);
void * p = shmat(shmid);
...

Does anyone know any better? Can I portably skip the second shmget() and still
assume I can access realSize bytes from p?


sean_in...@yahoo.com

no leída,
14 nov 2008, 10:19:09 a.m.14/11/08
para
> Does anyone know any better? Can I portably skip the second shmget() and still
> assume I can access realSize bytes from p?

For what it's worth, in both Linux and OpenSolaris'
shmget() implementations, the size argument for a
pre-existing segment is used only to check that it's not
bigger than that segment's actual size. There is no hint of
restricting access to the memory beyond what the caller
specified.

I read the POSIX page on shmget and didn't see any hint
in that direction either.

I think you can skip the second shmget with no problem.

Sean

Numeromancer

no leída,
21 nov 2008, 1:41:21 p.m.21/11/08
para

Thanks. These things seems so easy until you start trying to do the right thing
(in this case, get the size from the segment rather than fixing an arbitrary
size for both all apps using it).

TMS

sean_in...@yahoo.com

no leída,
25 nov 2008, 10:59:30 a.m.25/11/08
para
On Nov 21, 1:41 pm, Numeromancer <tsch...@sbcglobal.net> wrote:
> Thanks. These things seems so easy until you start trying to do the right thing
> (in this case, get the size from the segment rather than fixing an arbitrary
> size for both all apps using it).

Google Code Search can really help in a case like this.
If a high-quality cross-platform app like Apache's HTTP
server does it a certain way, there's a pretty good chance
it's correct. A search like:

package:httpd shmget\([^,]+, 0,

will show their usage in this case, and you can see they don't
do the followup call.

Sean

http://www.google.com/codesearch?hl=en&q=package:httpd+shmget%5C(%5B%5E,%5D%2B,+0,+show:n0HUAy2ijxo:8DmeQcZQbnU:1vy4RHQ7iHc&sa=N&cd=2&ct=rc&cs_p=ftp://www.ibiblio.org/pub/mirrors/apache/httpd/httpd-2.0.59-win32-src.zip&cs_f=httpd-2.0.59/srclib/apr/shmem/unix/shm.c

0 mensajes nuevos