Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #147930

Re: [Linux] Global named interprocess mutex

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c
Subject Re: [Linux] Global named interprocess mutex
Date 2019-11-10 15:03 -0800
Organization Aioe.org NNTP Server
Message-ID <qqa507$105n$2@gioia.aioe.org> (permalink)
References <18f48b11-ecf6-4340-af89-c98cec9eb6ab@googlegroups.com>

Show all headers | View raw


On 11/10/2019 6:12 AM, Frederick Gotham wrote:
> I have a piece of C code in Linux that uses a piece of hardware (/dev/caterpillar0). The hardware device does not support simultaneous accesses -- you can't have two threads (or two processes) using it at the same time.
> 
> About half a dozen processes need to use this piece of hardware near-simultaneously, and some of these processes are multi-threaded, so I am creating a minimalistic library to achieve this and it will be made available both as a static library (.a) and dynamic library (.so).
> 
> If I was doing this in MS-Windows, I'd just create a globally named mutex:
> 
>      HANDLE hMtx = CreateMutex( NULL, TRUE, "hardware_caterpillar0_lock"));
> 
> At the moment in Linux, I am creating a share memory object and then putting a pthread mutex inside it, and then using the pthread API to lock and unlock it. Here is the code I'm using for this interprocess mutex:
> 
> https://gist.github.com/yamnikov-oleg/abf61cf96b4867cbf72d
> 
> This interprocess mutex is actually quite clean and works quite well, but I'm able to break everything by doing this:
> If I get one of the processes to perform a time-consuming operation (e.g. 6 seconds), and if I hit Ctrl + C on this process when it's doing this, then it kills the process. . . and my global mutex remains locked! So now nobody can use the hardware!
> 
> Before I started using this shared mutex, I was just creating a file and using the Linux function "flock" to lock it, but the problem with this is that 'flock' gets a lock for the current process (not the current thread) which means that it won't stop simultaneous accesses for multiple threads of the same process.
> 
> How can I get a global interprocess mutex in Linux that will release the lock if the process dies? (Even better if it will release the lock when the thread dies).
> 

Make sure its robust!

https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_getrobust.html

If multiple processes are waiting on this, well, the robust attribute 
works well. EOWNERDEAD is akin to WAIT_ABANDONED over on win...

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

[Linux] Global named interprocess mutex Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-11-10 06:12 -0800
  Re: [Linux] Global named interprocess mutex Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-10 17:51 +0000
    Re: [Linux] Global named interprocess mutex gazelle@shell.xmission.com (Kenny McCormack) - 2019-11-10 21:16 +0000
      Re: [Linux] Global named interprocess mutex Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2019-11-10 23:00 +0100
  Re: [Linux] Global named interprocess mutex nobody@example.org (Scott) - 2019-11-10 17:54 +0000
  Re: [Linux] Global named interprocess mutex "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-11-10 15:03 -0800

csiph-web