This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SK-TDA4VM: TDA4 QNX SDK TIMER

Part Number: SK-TDA4VM


Tool/software:

I encountered a problem. I called the system interface to create a timer. I found that every time I entered the timer, the memory would increase by 4K. The following is my test record:
Prerequisite: the same code.
1. There is no memory leak in either thread mode or signal mode under Linux.
2. Under tda4 qnx, there is no memory leak when using signal, but there is a memory leak when using thread.
3. I also tried other hardware qnx, and there is no memory leak.
Therefore, I suspect that there may be a problem with TDA4 qnx. The following is my source code, which can be compiled into bin and run to see the effect.

./a.out thread or  ./a.out signal

as_stats.rss=0x7b (492.000kB)

-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>


void show_mem()
{
pid_t pid = getpid();
char buff[50] = {0};
snprintf(buff, sizeof(buff),"cat /proc/%d/vmstat | grep as_stats.rss", pid );
system(buff);
}


void *timer_thread_handler(union sigval sv) {
pthread_detach(pthread_self());
printf("Thread Timer expired! Thread ID: %lu\n", (unsigned long)pthread_self());
show_mem();
pthread_exit(NULL);
return NULL;
}


int create_timer_thread(timer_t *timerid, int interval_ms) {
struct sigevent sev;
struct itimerspec its;


sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = timer_thread_handler;
sev.sigev_notify_attributes = NULL; 
sev.sigev_value.sival_ptr = timerid;

if (timer_create(CLOCK_REALTIME, &sev, timerid) == -1) {
perror("timer_create");
return -1;
}


its.it_value.tv_sec = interval_ms / 1000; 
its.it_value.tv_nsec = (interval_ms % 1000) * 1000000;
its.it_interval.tv_sec = interval_ms / 1000; 
its.it_interval.tv_nsec = (interval_ms % 1000) * 1000000;


if (timer_settime(*timerid, 0, &its, NULL) == -1) {
perror("timer_settime");
return -1;
}

return 0;
}

int thread_main()
{

timer_t timerid;
if (create_timer_thread(&timerid,1000) == -1) {
fprintf(stderr, "Failed to create timer\n");
exit(EXIT_FAILURE);
}
printf("Timer started, waiting for timer events...\n");
while(1);
}



timer_t timer_id;


void timer_handler_signal(int signo, siginfo_t *info, void *context) {
if (signo == SIGALRM) {
printf("Signal Timer expired! Thread ID: %lu\n", (unsigned long)pthread_self());
show_mem();
}
}


int create_timer_signal(int initial_sec, int interval_sec, int user_data) {
struct sigevent sev;
struct itimerspec its;
struct sigaction sa;


sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timer_handler_signal;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGALRM, &sa, NULL) == -1) {
perror("sigaction");
return -1;
}


sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGALRM;
sev.sigev_value.sival_int = user_data;


if (timer_create(CLOCK_REALTIME, &sev, &timer_id) == -1) {
perror("timer_create");
return -1;
}


its.it_value.tv_sec = initial_sec;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = interval_sec;
its.it_interval.tv_nsec = 0;


if (timer_settime(timer_id, 0, &its, NULL) == -1) {
perror("timer_settime");
return -1;
}

return 0;
}

int signal_main() {
int user_data = 42; 
if (create_timer_signal(1, 1, user_data) == -1) {
fprintf(stderr, "Failed to create timer\n");
return EXIT_FAILURE;
}
while (1) {
pause(); 
}
timer_delete(timer_id);

return EXIT_SUCCESS;
}

int main(int argc, char *argv[])
{
if(strcmp("thread", argv[1]) == 0){
thread_main();
}else{
signal_main();
}
return 0;
}