Mutexes between Processes

Hello,

is it possible to share mutexes between different processes? I have three
processes which have to access a file, and I want to use a mutex to lock
the file while one of the processes is reading/writing from it. Is this
possible with mutexes? Thanks.

Matt.

Mathew Asselin <m2asselin@yahoo.com> wrote:

Hello,

is it possible to share mutexes between different processes? I have three
processes which have to access a file, and I want to use a mutex to lock
the file while one of the processes is reading/writing from it. Is this
possible with mutexes? Thanks.

Mutexes can be used between processes, they need to be in a shared memory
area so they are visible to all of the processes, and they need to be
created with the process shared flag set – take a look at
pthread_mutexattr_setpshared() for setting that flag. And shm_open()
for setting up shared memory.

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

David Gibbs wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
is it possible to share mutexes between different processes? I have three
processes which have to access a file, and I want to use a mutex to lock
the file while one of the processes is reading/writing from it. Is this
possible with mutexes? Thanks.

Mutexes can be used between processes, they need to be in a shared memory
area so they are visible to all of the processes, and they need to be
created with the process shared flag set – take a look at
pthread_mutexattr_setpshared() for setting that flag. And shm_open()
for setting up shared memory.

Wouldn’t it be simpler to use lockf(), flock(), or fcntl() to lock the file?

Wojtek Lerch <Wojtek_L@yahoo.ca> wrote:

David Gibbs wrote:
Mathew Asselin <> m2asselin@yahoo.com> > wrote:
is it possible to share mutexes between different processes? I have three
processes which have to access a file, and I want to use a mutex to lock
the file while one of the processes is reading/writing from it. Is this
possible with mutexes? Thanks.

Mutexes can be used between processes, they need to be in a shared memory
area so they are visible to all of the processes, and they need to be
created with the process shared flag set – take a look at
pthread_mutexattr_setpshared() for setting that flag. And shm_open()
for setting up shared memory.

Wouldn’t it be simpler to use lockf(), flock(), or fcntl() to lock the file?

Yeah, I was thinking that last night – file locks might make more sense
than mutexes for something like this.

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

David Gibbs wrote:

Yeah, I was thinking that last night – file locks might make more sense
than mutexes for something like this.

On that note, it would be useful to fix the QNX version of Samba
to use file locks rather than lock files. If you don’t shut
down QNX properly, Samba won’t start until you clean up its
lock files.

John Nagle
Team Overbot