Skip to contentSkip to conversation

rt_printf with daemonized task

Xenomai-help [email protected] 3 participants 7 messages Oct 8, 2009

lore
Share & tools
lore
#0 lore raw

-------- Original Message --------

Subject: Re: Re-8:  rt_printf with daemonized task (07-Okt-2009 17:38)
From:    Jan Kiszka <[email protected]>
To:      [email protected]
Quoted 53 lines
Quoted 52 lines · [email protected] wrote:

As you expected, changing the syslog call from

    syslog( LOG_ERR | LOG_LOCAL4, head->text ); 
to
    syslog( LOG_ERR | LOG_LOCAL4, "%s", head->text ); 

did not change anything.

To track down the problem further, I have made a small test application :

#include <stdlib.h>
#include <stdio.h>

#include <native/task.h>
#include <native/timer.h>
#include <rtdk.h>

int main( int arc, char *argv[] )
{
int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

	sleep(1);

daemon(0,0);

	rt_print_auto_init(1);

	rt_printf("--------------- TEST RT-PRINTF  2 ------------\n");

	sleep(10);

return(0);
}

This application gives the following results :
- If the daemon() function is not called, all rt_printf() do work well and output is shown in syslog
- If daemon() function is called, and first call to sleep(1) is omitted, there is no output in the syslog whatsoever.
- If daemon() function is called, and first call to sleep(1) is executed, there is output of the first rt_printf in the syslog but not of the
second
one.

Quoted 7 lines
Quoted 2 lines

I do not have any idea why daemonizing of the process is not giving any output to the syslog.

As you are already looking at rt_printf.c: __rt_print_init spawns a pthread that polls the internal queues and issues the output to stdout or whatever. On fork (or daemonize), this thread is not replicated. So the child process simply has no printing thread.

Ok, understood, at least for the scenario where the rt_print feature is initialised
befor the fork/daemon call.
What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

Quoted 3 lines

I think this should be fixable with a pthread_atfork hook, patches always welcome.

I would like to follow up this track. I have to have a look at pthread_atfork first, because I'm not so familiar with the pthread functions.
Therefor any hint what should be done with pthread_atfork is very welcome.

Quoted 6 lines

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux

Oliver

To: [email protected]
Cc: [email protected]
[email protected]

#1 lore raw
Quoted 10 lines · [email protected] wrote:
Quoted 10 lines
Quoted 10 lines

int main( int arc, char *argv[] ) { int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

sleep(1);

daemon(0,0);

Quoted 7 lines
Quoted 1 line
Quoted 1 line

rt_print_auto_init(1);

Ok, understood, at least for the scenario where the rt_print feature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

From the way I understand your code, you never tried to initialize the

rt_print feature only after the fork, your code initializes it both before and after.

Signature · 1 line
                                          Gilles
#2 lore raw
Quoted 12 lines · Gilles Chanteperdrix
Quoted 11 lines · [email protected] wrote:
Quoted 11 lines
Quoted 11 lines

int main( int arc, char *argv[] ) { int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

sleep(1);

daemon(0,0);

rt_print_auto_init(1);

Quoted 9 lines
Quoted 4 lines

Ok, understood, at least for the scenario where the rt_print feature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

From the way I understand your code, you never tried to initialize the rt_print feature only after the fork, your code initializes it both before and after.

There are two initializations: The base init done via __rt_print_init on library loading and the one to be done per-thread via rt_print_init (or on first rt_printf). That printer thread is initialized via the former one. On fork, we do not need to re-run the full __rt_print_init (variables and resources are cloned on fork), we just need to spawn another printer thread.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#3 lore raw
Quoted 22 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 10 lines · [email protected] wrote:
Quoted 10 lines
Quoted 10 lines

int main( int arc, char *argv[] ) { int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

sleep(1);

daemon(0,0);

Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

rt_print_auto_init(1);

Ok, understood, at least for the scenario where the rt_print feature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

From the way I understand your code, you never tried to initialize the rt_print feature only after the fork, your code initializes it both before and after.

Quoted 6 lines

There are two initializations: The base init done via __rt_print_init on library loading and the one to be done per-thread via rt_print_init (or on first rt_printf). That printer thread is initialized via the former one. On fork, we do not need to re-run the full __rt_print_init (variables and resources are cloned on fork), we just need to spawn another printer thread.

Unless I am wrong, rtdk also maintains a list of the thread buffers which need to be polled. After the fork, this list will be intact, but the threads to which belong the buffers will no longer exist. So, IMO, the fork handler should also free all these buffers and reset the list to the empty state.

Signature · 1 line
                                          Gilles
#4 lore raw
Quoted 29 lines · Gilles Chanteperdrix
Quoted 21 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 10 lines · [email protected] wrote:
Quoted 10 lines
Quoted 10 lines

int main( int arc, char *argv[] ) { int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

sleep(1);

daemon(0,0);

Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

rt_print_auto_init(1);

Ok, understood, at least for the scenario where the rt_print feature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

From the way I understand your code, you never tried to initialize the rt_print feature only after the fork, your code initializes it both before and after.

Quoted 6 lines

There are two initializations: The base init done via __rt_print_init on library loading and the one to be done per-thread via rt_print_init (or on first rt_printf). That printer thread is initialized via the former one. On fork, we do not need to re-run the full __rt_print_init (variables and resources are cloned on fork), we just need to spawn another printer thread.

Quoted 5 lines

Unless I am wrong, rtdk also maintains a list of the thread buffers which need to be polled. After the fork, this list will be intact, but the threads to which belong the buffers will no longer exist. So, IMO, the fork handler should also free all these buffers and reset the list to the empty state.

Famous last words: That should work without tweaking. A print_buffer only contains data references, nothing that points to some uncloned thread.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#5 lore raw
Quoted 29 lines · Jan Kiszka
Quoted 28 lines · Gilles Chanteperdrix
Quoted 27 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 10 lines · [email protected] wrote:
Quoted 10 lines
Quoted 10 lines

int main( int arc, char *argv[] ) { int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

sleep(1);

daemon(0,0);

Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

rt_print_auto_init(1);

Ok, understood, at least for the scenario where the rt_print feature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

From the way I understand your code, you never tried to initialize the rt_print feature only after the fork, your code initializes it both before and after.

There are two initializations: The base init done via __rt_print_init on library loading and the one to be done per-thread via rt_print_init (or on first rt_printf). That printer thread is initialized via the former one. On fork, we do not need to re-run the full __rt_print_init (variables and resources are cloned on fork), we just need to spawn another printer thread.

Quoted 8 lines
Quoted 5 lines

Unless I am wrong, rtdk also maintains a list of the thread buffers which need to be polled. After the fork, this list will be intact, but the threads to which belong the buffers will no longer exist. So, IMO, the fork handler should also free all these buffers and reset the list to the empty state.

Famous last words: That should work without tweaking. A print_buffer only contains data references, nothing that points to some uncloned thread.

Ok. But you walk the list of print_buffers every time, and these print_buffers remain allocated in the child process. So, that is a kind of memory leak.

Quoted 3 lines

Jan

Signature · 1 line
                                          Gilles
#6 lore raw
Quoted 35 lines · Gilles Chanteperdrix
Quoted 34 lines · Jan Kiszka
Quoted 33 lines · Gilles Chanteperdrix
Quoted 21 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 10 lines · [email protected] wrote:
Quoted 10 lines
Quoted 10 lines

int main( int arc, char *argv[] ) { int i;

rt_print_auto_init(1);

rt_printf("--------------- TEST RT-PRINTF 1 ------------\n");

sleep(1);

daemon(0,0);

Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

rt_print_auto_init(1);

Ok, understood, at least for the scenario where the rt_print feature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if the rt_print feature is initialised after the fork / daemon.

From the way I understand your code, you never tried to initialize the rt_print feature only after the fork, your code initializes it both before and after.

Quoted 6 lines

There are two initializations: The base init done via __rt_print_init on library loading and the one to be done per-thread via rt_print_init (or on first rt_printf). That printer thread is initialized via the former one. On fork, we do not need to re-run the full __rt_print_init (variables and resources are cloned on fork), we just need to spawn another printer thread.

Unless I am wrong, rtdk also maintains a list of the thread buffers which need to be polled. After the fork, this list will be intact, but the threads to which belong the buffers will no longer exist. So, IMO, the fork handler should also free all these buffers and reset the list to the empty state.

Quoted 6 lines
Quoted 2 lines

Famous last words: That should work without tweaking. A print_buffer only contains data references, nothing that points to some uncloned thread.

Ok. But you walk the list of print_buffers every time, and these print_buffers remain allocated in the child process. So, that is a kind of memory leak.

Hmm, right. They should be freed in the child right after forking.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux