rt_printf with daemonized task
-------- Original Message --------
Subject: Re: Re : rt_printf with daemonized task (08-Okt-2009 14:19) From: Gilles Chanteperdrix <[email protected]> To: [email protected]
Quoted 40 lines
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Gilles Chanteperdrix
Quoted 15 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);Ok, understood, at least for the scenario where the
rt_printfeature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if thert_printfeature is initialised after the fork / daemon.Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
From the way I understand your code, you never tried to initialize the
rt_printfeature only after the fork, your code initializes it both before and after.There are two initializations: The base init done via
__rt_print_initon
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
library loading and the one to be done per-thread via
rt_print_init(or on firstrt_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_bufferonly contains data references, nothing that points to some uncloned thread.
Quoted 12 lines
Ok. But you walk the list of
print_buffersevery time, and theseprint_buffersremain allocated in the child process. So, that is a kind of memory leak.Quoted 3 lines
Jan
Signature · 1 line
Gilles
Did change my test code in the meantime to :
#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;
daemon(0,1);
__rt_print_init();
rt_print_auto_init(1);
rt_printf("--------------- TEST RT-PRINTF ------------\n");
sleep(10);
return(0);
}
and everything works fine at first glance.
There is at least an output to syslog of the rt_printf message. I have no idea if this creates an internal memory leak. For my application it might be not a real problem, because the fork is done only once. For application with several fork calls or a recursional fork calls it might be not ok.
Oliver
To: [email protected]
[email protected]
Cc: [email protected]
Quoted 87 lines · [email protected] wrote:
-------- Original Message --------
Subject: Re: Re : rt_printf with daemonized task (08-Okt-2009 14:19) From: Gilles Chanteperdrix <[email protected]> To: [email protected]Quoted 36 lines
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Gilles Chanteperdrix
Quoted 15 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);Ok, understood, at least for the scenario where the
rt_printfeature is initialised befor the fork/daemon call. What I don't understand is, why it does not work if thert_printfeature is initialised after the fork / daemon.Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
From the way I understand your code, you never tried to initialize the
rt_printfeature only after the fork, your code initializes it both before and after.There are two initializations: The base init done via
__rt_print_initon
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
library loading and the one to be done per-thread via
rt_print_init(or on firstrt_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 12 lines
Quoted 2 lines
Famous last words: That should work without tweaking. A
print_bufferonly contains data references, nothing that points to some uncloned thread.Ok. But you walk the list of
print_buffersevery time, and theseprint_buffersremain allocated in the child process. So, that is a kind of memory leak.Quoted 2 lines
Jan
Signature · 1 line
GillesDid change my test code in the meantime to :
#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;
daemon(0,1);
__rt_print_init();
rt_print_auto_init(1);
rt_printf("--------------- TEST RT-PRINTF ------------\n");sleep(10);
return(0);
}
Quoted 7 lines
and everything works fine at first glance.
There is at least an output to syslog of the
rt_printfmessage. I have no idea if this creates an internal memory leak. For my application it might be not a real problem, because the fork is done only once. For application with several fork calls or a recursional fork calls it might be not ok.
This case will not leak as there were no buffers registered before the fork. But it's not a generic solution, using a non-public service, __rt_print_init, which I don't want to make public.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2 Corporate Competence Center Embedded Linux
Quoted 29 lines · [email protected] wrote:
Did change my test code in the meantime to :
#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;
daemon(0,1);
__rt_print_init();
rt_print_auto_init(1);
rt_printf("--------------- TEST RT-PRINTF ------------\n");sleep(10);
return(0);
}
Quoted 7 lines
and everything works fine at first glance.
There is at least an output to syslog of the
rt_printfmessage. I have no idea if this creates an internal memory leak. For my application it might be not a real problem, because the fork is done only once. For application with several fork calls or a recursional fork calls it might be not ok.
Calling fork inside an application which has several running threads is already insane. Think about it: if the running threads had a mutex locked, the mutex will still appear as locked in the child process and can not be unlocked. The threads will not exist in the child process, but their stacks and allocated tsd will still exists. posix proposes pthread_atfork to solve these issues, but not forking in a process with threads is more sane (the exception being to call fork+exec or vfork+exec, since in that case the virtual memory space will be wiped out by exec, so we do not care about the leaks anyway).
Signature · 1 line
Gilles