Hi,
I want to use the apr_proc_mutex_* API to synchronize processes. But if
I want to use a APR_LOCK_FLOCK mutex the second process will fail to
open the mutex with a File exists error. This is due to the fact the
file is opened with the flags:
APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL
If I remove the APR_FOPEN_EXCL it works. So it seems to me that
apr_proc_mutex_* API only works on Linux for parent/child processes
which uses the apr_proc_mutex_child_init() function within the forked
child process!? Current documentation says nothing about that.
static apr_status_t proc_mutex_flock_create(apr_proc_mutex_t *new_mutex,
const char *fname)
{
int rv;
if (fname) {
new_mutex->fname = apr_pstrdup(new_mutex->pool, fname);
rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
APR_FOPEN_CREATE | APR_FOPEN_WRITE |
APR_FOPEN_EXCL,
APR_UREAD | APR_UWRITE,
new_mutex->pool);
}
else {
...
}
...
Attached you will find a simple test program if started twice with the
same lock file one of the two processes will terminate with a "file
exists" error:
./procmutex mylock&
./procmutex mylock&
Any ideas to solve this problem? Does anyone rely on the fact the passed
file need to be non-existing???
Regards,
Stefan
#include "apr-1/apr_proc_mutex.h"
#include "apr-1/apr_time.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
apr_status_t rc;
apr_proc_mutex_t* lock;
apr_pool_t *pool;
int i=0;
apr_initialize();
if(apr_pool_create(&pool, NULL) != APR_SUCCESS)
exit(1);
if((rc = apr_proc_mutex_create(&lock, argv[1],
APR_LOCK_FLOCK, pool)) != APR_SUCCESS)
{
fprintf(stderr, "error: %d\n", rc);
exit(2);
}
do
{
++i;
apr_proc_mutex_lock(lock);
fprintf(stderr, "after lock: %d\n", (int)getpid());
apr_sleep(1000*1000);
fprintf(stderr, "before unlock: %d\n", (int)getpid());
fprintf(stderr, "\n");
apr_proc_mutex_unlock(lock);
apr_sleep(500*1000);
} while(i < 100);
return 0;
}