[SciPy-user] shared memory machines

Sturla Molden sturla at molden.no
Mon Feb 9 16:41:59 EST 2009


> On Feb 9, 2009, at 1:05 PM, Sturla Molden wrote:

> Yes, sort of. Sys V IPC objects are owned by users, not processes, so
> if user foo creates a semaphore in one process and destroys it in
> another, that's OK. I just verified this on OS X. Unless portions of
> your SciPy application will be running under different users, I think
> the matter of which process destroys an IPC object is irrelevant.

My programs will certainly not do that (but I use Windows anyway). I have
one process that spawns workers, and they run with the same user. I think
that covers 99% of all use for this extension.

But others may have a more complex design, so the safest method is to let
the creator kill the segment. But it comes at the expense of a thread.
Otherwise I could just assume the user is the same and do the check after
shmdt. As I use Windows I have no personal preference here. Not using a
thread avoids some of the ctrl-c issue, and it will be a bit faster. But
then all processes sharing memory must run with the same user, otherwise
there will be leaks and havoc.


>> In that case I could rewrite the object to pickle the shmid instead
>> of a
>> random name (uuid string) on System V.
>
> But you need the key, not the id, to pass to shmget() to get a handle
> to an existing IPC object.

Yes, but if we use the shmid, we have the handle. So we can pass that
integer from one process to another. At least that is what my old book on
Linux programming describes. Since you say I need to use shmget, can I
assume this method is not valid on all Unix incarnations?


> If I were you, rather than trying to figure out how broken ftok() is
> (and it might be broken in different ways on different platforms), I'd
> just abandon it altogether. It's not as if generating a random number
> instead is difficult. In fact, it's easier. Instead of generating a
> random uuid and passing that to ftok(), eliminate the middleman and
> generate a random key yourself.

It has to be a unique key for the system, not just a random number. So I
could try to call shmget multiple times with IPC_EXCL until it succeeds.
Then I'll have to check why it failed as well.

This is the first time I have found Windows to be the less annoying system.


Thanks for your help. :-)


Sturla Molden










More information about the SciPy-User mailing list