I am using flock
to prevent concurrent access to a shared resource. It works fine the first time I check the lock, but the second time I check the lock, I am able to successfully obtain it despite the fact that lock is stil being held by another process.
I am using the following code:
#include <stdio.h>
#include <sys/file.h>
#include <errno.h>
int main(void) {
printf("App started:\n");
int fd = open("test.lock", O_RDWR | O_CREAT, 0666); // open or create lockfile
//check open success...
int rc = flock(fd, LOCK_EX | LOCK_NB); // grab exclusive lock, fail if can't obtain.
if (rc)
{
printf("Failed .. lock is already held\n");
if(EWOULDBLOCK == errno)
{
printf("errno said, that we would block\n");
}
return 0;
}
else
{
printf("Lock obtained\n");
}
printf("Press ENTER...\n");
getc(stdin);
return 0;
}
Platform: OSX 10.10.
If I run the program in two separate terminal windows, I get the following output:
First window - the app acquires the lock and waits for key to be pressed
App started: Lock obtained Press ENTER...
Second terminal window first run - application sucesfully finds out, that the lock is already held by someone else
App started: Failed .. lock is already held errno said, that we would block
Second terminal window second run - application obtains the lock despite the fact that first instance is already holding it
App started: Lock obtained Press ENTER...
It as if testing for lock would also release it...
Any ideas?
UPDATE: This looks like OS X related problem. The code works without problem on 14.04 32 bit. Matra