Problem with mutexes in daemon
Hello,
I'm experiencing weird behavior with mutexes in a daemon application. After daemon() is called, unnamed mutexes fail on call rt_mutex_release() with -EPERM. Everything is ok for named mutexes.
Is there something special with daemon and unnamed mutexes - should all have name? Or is it a bug? I'm using kernel 2.6.28 and Xenomai 2.4.6.1.
Example showing this problem is attached.
Thank you for help,
Roman
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/cond.h>
#include <native/mutex.h>
#include <native/timer.h>
RT_TASK demo_task;RT_TASK main_task;RT_MUTEX mutex;RT_MUTEX shut_mutex;RT_COND shut_cond;
#define TRUE 1
#define FALSE 0
bool terminated = FALSE;
bool terminated2 = FALSE;
void demo(void *arg)
{
int ret;
RTIME now, previous;
rt_task_set_periodic(NULL, TM_NOW, 1000000000);
previous = rt_timer_read();
while (!terminated2) {
rt_task_wait_period(NULL);
now = rt_timer_read();
printf("DT: Time since last turn: %ld.%06ld ms\n",
(long)(now - previous) / 1000000,
(long)(now - previous) % 1000000);
previous = now;
if (ret = rt_mutex_acquire(&mutex, TM_INFINITE))
printf("DT: rt_mutex_acquire() failed %i\n", ret);
rt_task_sleep(1000000);
if (ret = rt_mutex_release(&mutex))
printf("DT: rt_mutex_release() failed %i\n", ret);
}
if (ret = rt_mutex_acquire(&shut_mutex, TM_INFINITE))
printf("DT: rt_mutex_acquire() before cond failed %i\n", ret);
if (ret = rt_cond_signal(&shut_cond))
printf("DT: rt_cond_signal() failed %i\n", ret);
if (ret = rt_mutex_release(&shut_mutex))
printf("DT: rt_mutex_release() after cond failed %i\n", ret);
}void catch_signal(int sig)
{
terminated = TRUE;
}int main(int argc, char* argv[])
{
int ret;
//when daemon - problems with unnamed mutexes
//without daemon everything ok
daemon(1, 1);
if (ret = rt_mutex_create(&mutex, NULL))
{
printf("Error creating mutex %i\n", ret);
return 0;
}
if (ret = rt_mutex_create(&shut_mutex, NULL))
{
printf("Error creating shut_mutex %i\n", ret);
return 0;
}
if (ret = rt_cond_create(&shut_cond, NULL))
{
printf("Error creating cond%i\n", ret);
return 0;
}
signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
mlockall(MCL_CURRENT|MCL_FUTURE);
if (ret = rt_task_shadow(&main_task, "main", 90, 0))
printf("rt_task_shadow() failed %i\n", ret);
if (ret = rt_task_create(&demo_task, "trivial", 0, 99, 0))
printf("rt_task_create() failed %i\n", ret);
rt_task_start(&demo_task, &demo, NULL);
while (!terminated)
{
if (ret = rt_mutex_acquire(&mutex, TM_INFINITE))
printf("MT: rt_mutex_acquire() failed, %i\n", ret);
rt_task_sleep(1000000);
printf("sleep\n");
if (ret = rt_mutex_release(&mutex))
printf("MT: rt_mutex_release() failed %i\n", ret);
rt_task_sleep(1000000000);
}
printf("Finished...\n");
if (ret = rt_mutex_acquire(&shut_mutex, TM_INFINITE))
printf("MT: rt_mutex_acquire() before cond failed %i\n", ret);
terminated2 = TRUE;
if (ret = rt_cond_wait(&shut_cond, &shut_mutex, TM_INFINITE))
printf("MT: rt_cond_signal() failed %i\n", ret);
if (ret = rt_mutex_release(&shut_mutex))
printf("MT: rt_mutex_release() after cond failed %i\n", ret);
rt_task_delete(&demo_task);
rt_mutex_delete(&mutex);
rt_mutex_delete(&shut_mutex);
rt_cond_delete(&shut_cond);
}Quoted 9 lines · Roman Pisl
Hello,
I'm experiencing weird behavior with mutexes in a daemon application. Afterdaemon()is called, unnamed mutexes fail on callrt_mutex_release()with-EPERM. Everything is ok for named mutexes.Is there something special with daemon and unnamed mutexes - should all have name? Or is it a bug? I'm using kernel 2.6.28 and Xenomai 2.4.6.1.
Example showing this problem is attached.
Unnamed mutexes are local to a process. Since forking results in a different process, mutexes are not preserved.
Yes, Xenomai behaviour is not intuitive, but fixing this requires internal changes.
Signature · 1 line
Gilles.
Gilles Chanteperdrix napsal(a):
Quoted 6 lines
Quoted 5 lines · Roman Pisl
Hello,
I'm experiencing weird behavior with mutexes in a daemon application. Afterdaemon()is called, unnamed mutexes fail on callrt_mutex_release()with-EPERM. Everything is ok for named mutexes.Is there something special with daemon and unnamed mutexes - should all have name? Or is it a bug? I'm using kernel 2.6.28 and Xenomai 2.4.6.1.
Quoted 8 lines
Quoted 1 line
Example showing this problem is attached.
Unnamed mutexes are local to a process. Since forking results in a different process, mutexes are not preserved.
Yes, Xenomai behaviour is not intuitive, but fixing this requires internal changes.
I understand, but daemon() is the first call and everything is done in the new forked process.
What is the best solution for now? Is everything except unnamed mutexes supposed to work well in daemon or should I cancel the concept of daemon at all?
Roman
Quoted 18 lines · Roman Pisl
Gilles Chanteperdrix napsal(a):
Quoted 9 lines
Quoted 8 lines · Roman Pisl
Hello,
I'm experiencing weird behavior with mutexes in a daemon application. Afterdaemon()is called, unnamed mutexes fail on callrt_mutex_release()with-EPERM. Everything is ok for named mutexes.Is there something special with daemon and unnamed mutexes - should all have name? Or is it a bug? I'm using kernel 2.6.28 and Xenomai 2.4.6.1.
Quoted 7 lines
Quoted 1 line
Example showing this problem is attached.
Unnamed mutexes are local to a process. Since forking results in a different process, mutexes are not preserved.
Yes, Xenomai behaviour is not intuitive, but fixing this requires internal changes.
Quoted 6 lines
I understand, but
daemon()is the first call and everything is done in the new forked process.What is the best solution for now? Is everything except unnamed mutexes supposed to work well in daemon or should I cancel the concept of daemon at all?
Yes, what you are doing is supposed to work, I did not look at your code, I thought you were expecting to be able to create the mutex in a process and use it in its child.
On what platform do you get these issues ?
Signature · 1 line
Gilles.