From: john on
Hello,

I am trying to create a very simple program that will create a shared
memory segment using shmget and shmat etc....

I have the starts of a cross platform mutex class that I would like to
stick into the shared memory - just now only pthread_mutex_t is in it
but eventually the plan is to get it using CMutex/CriticalSections from
windows too.

Anyway, I've followed the guidelines on using mutexes in shared memory:

pthread_mutexattr_t hAttr;
pthread_mutexattr_init(&hAttr);
pthread_mutexattr_settype(&hAttr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutexattr_setrobust_np(&hAttr, PTHREAD_MUTEX_ROBUST_NP);
pthread_mutexattr_setpshared(&hAttr, PTHREAD_PROCESS_SHARED)
pthread_mutex_init((pthread_mutex_t*)m_hMutex, NULL);
pthread_mutexattr_destroy(&hAttr);

so now I should be able to call lock and unlock on the mutex. Outwith
the shared memory, the mutex class acts as one would expect. When in the
shared memory, the creating process can use it but as soon as another
process tries to access the mutex I receive a segmentation fault.

I've not included all of the code as its quite a bit but hoping that
this conveys the general idea of the program.

Now, I'm almost certain the shared memory is correct as I can put things
into it and get them out on another process without any problems. In my
opinion it seems to be the mutex in the shared memory that has the
problem. Also, I didn't directly create the mutex class - instead I got
a pointer to the shared memory and cast it to a struct that had some
ints and an instance of my mutex class. The ints work as expected and I
call an initialise method in the mutex class that calls the above code
so in theory the mutex in the shared memory is the one that is intialised.

Does this make sense? Anyway, can anyone provide any code where they are
correctly using mutexes in shared memory? Should I be using mmap instead
of the shmget/shmat combo? Any pitfalls I should be aware of?

Thanks in advance for any help you may provide?

John.
From: David Schwartz on
On Jul 20, 5:44 am, john <j...(a)replyatnewsgroup.com> wrote:

> so now I should be able to call lock and unlock on the mutex. Outwith
> the shared memory, the mutex class acts as one would expect. When in the
> shared memory, the creating process can use it but as soon as another
> process tries to access the mutex I receive a segmentation fault.

Perhaps you assumed the pointer would be the same in both processes?
Perhaps you called pthread_mutex_init in both processes? It's hard to
debug code we can't see.

DS
From: Daniel Molina Wegener on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On Mar 20 Jul 2010 08:44,
john wrote:

> Hello,
>
> I am trying to create a very simple program that will create a shared
> memory segment using shmget and shmat etc....
>
> I have the starts of a cross platform mutex class that I would like to
> stick into the shared memory - just now only pthread_mutex_t is in it
> but eventually the plan is to get it using CMutex/CriticalSections from
> windows too.
>
> Anyway, I've followed the guidelines on using mutexes in shared memory:
>
> pthread_mutexattr_t hAttr;
> pthread_mutexattr_init(&hAttr);
> pthread_mutexattr_settype(&hAttr, PTHREAD_MUTEX_RECURSIVE_NP);
> pthread_mutexattr_setrobust_np(&hAttr, PTHREAD_MUTEX_ROBUST_NP);
> pthread_mutexattr_setpshared(&hAttr, PTHREAD_PROCESS_SHARED)
> pthread_mutex_init((pthread_mutex_t*)m_hMutex, NULL);
> pthread_mutexattr_destroy(&hAttr);
>
> so now I should be able to call lock and unlock on the mutex. Outwith
> the shared memory, the mutex class acts as one would expect. When in the
> shared memory, the creating process can use it but as soon as another
> process tries to access the mutex I receive a segmentation fault.
>
> I've not included all of the code as its quite a bit but hoping that
> this conveys the general idea of the program.
>
> Now, I'm almost certain the shared memory is correct as I can put things
> into it and get them out on another process without any problems. In my
> opinion it seems to be the mutex in the shared memory that has the
> problem. Also, I didn't directly create the mutex class - instead I got
> a pointer to the shared memory and cast it to a struct that had some
> ints and an instance of my mutex class. The ints work as expected and I
> call an initialise method in the mutex class that calls the above code
> so in theory the mutex in the shared memory is the one that is intialised.
>
> Does this make sense? Anyway, can anyone provide any code where they are
> correctly using mutexes in shared memory? Should I be using mmap instead
> of the shmget/shmat combo? Any pitfalls I should be aware of?
>
> Thanks in advance for any help you may provide?
>
> John.

Seems that you are mixing topics. If you are using threads and mutexes,
possibly you are missing the calls bellow:

pthread_mutex_lock()
pthread_mutex_trylock()
pthread_mutex_unlock()

Also, for shared memory segments and IPC, assuming that you are sharing
the memory segment between 2 or more proccess, POSIX thread mutexes on
that case are useless, since you can not see a mutex between two or more
process. On that case, you must try using semaphores, and use the calls
bellow:

semget()
semctl()
semop()

Also there are more facilities to work with, depending on the platform,
on Linux is present the futex(2) facility.

Best regards,
- --
Daniel Molina Wegener <dmw [at] coder [dot] cl>
System Programmer & Web Developer
Phone: +56 (2) 979-0277 | Blog: http://coder.cl/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQIcBAEBCgAGBQJMRt1xAAoJEHxqfq6Y4O5NuLQQAKM4uJSz42mJ1MJMoks71hWV
3zqfrHZl41rqsnbMIrfUkZJsiYUWaLdYqavnLo3k6DN+XR8R2dxLPRXwlD8vX1GY
U/VvnCsVoK4CprIkWcHd106BrnLrSmXnjnq8pNoTd+YeWoil3xHVytqzeAkcj9En
9+SdP8ZczZr1+fWIoXcXqJ35miEF68TrOm1wibuZ/5Ucl84jj1XQL+t0BX4WA6s+
Ecc30Ek2k3ANwBtnZT05XX4aNt5PlrAEWFHdCjos2YC+WWxUXrkJc5cGUj1eBVpT
x6RRb5My+chltjXH2AkGs1DTDR/uLdzlRZF8vLCEI4iSJBUOtJMWoFc/clTY3txx
ikgcgp56tfMKhfejPSyh7M1e6iQdRLaPLWARSwsylzYz33jI04H0SQEO10ETdxOj
9W+byyEwQ7ir4lfg6jLdvDnVKMDavNGCWQ4H1BuIXUIHmVOgD76+kUbA/V7jm+Wg
Gl58WUY9qHbJRxrXW+TMYGZy33a4/e7cmYPg2YNhNXERPHpbtQ5k2IP+cvvvG90z
/n60IXA2Ump6+9U182lMq27OjMesIPebXbHhTy1GLBTBGCMgn8um3IswylgyTw4Q
jmtl2yA9hjZwdUxZYTiOj0oCc6h4RNIyKYrZ5GgVeMZV7ZgRPRv0mEUo3CFuG9nL
AB5UjzyCsPf2IS8597OI
=PQY3
-----END PGP SIGNATURE-----

From: Ersek, Laszlo on
On Wed, 21 Jul 2010, Daniel Molina Wegener wrote:

> Also, for shared memory segments and IPC, assuming that you are sharing
> the memory segment between 2 or more proccess, POSIX thread mutexes on
> that case are useless, since you can not see a mutex between two or more
> process.

This seems to be wrong.

http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_setpshared.html

lacos
From: Geoff Clare on
john wrote:

> pthread_mutexattr_t hAttr;
> pthread_mutexattr_init(&hAttr);
> pthread_mutexattr_settype(&hAttr, PTHREAD_MUTEX_RECURSIVE_NP);
> pthread_mutexattr_setrobust_np(&hAttr, PTHREAD_MUTEX_ROBUST_NP);
> pthread_mutexattr_setpshared(&hAttr, PTHREAD_PROCESS_SHARED)
> pthread_mutex_init((pthread_mutex_t*)m_hMutex, NULL);

The second argument should be &hAttr, not NULL.

--
Geoff Clare <netnews(a)gclare.org.uk>