c - What does this "alarm" error mean? -


i trying memory consumed algorithm, have created group of functions stop execution in periods of 10 milliseconds let me read memory using getrusage() function. idea set timer raise alarm signal process received handler medir_memoria().

however, program stops in middle message:

[1]    3267 alarm    ./memory_test 

the code reading memory is:

#include "../include/rastreador_memoria.h"  #if defined(__linux__) || defined(__apple__) || (defined(__unix__) && !defined(_win32))  #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <signal.h> #include <sys/resource.h>  static long max_data_size; static long max_stack_size;  void medir_memoria (int sig) {     struct rusage info_memoria;      if (getrusage(rusage_self, &info_memoria) < 0)     {         perror("not reading memory");     }      max_data_size = (info_memoria.ru_idrss > max_data_size) ? info_memoria.ru_idrss : max_data_size;     max_stack_size = (info_memoria.ru_isrss > max_stack_size) ? info_memoria.ru_isrss : max_stack_size;      signal(sigalrm, medir_memoria); }  void rastrear_memoria () {     struct itimerval t;      t.it_interval.tv_sec = 0;     t.it_interval.tv_usec = 10;     t.it_value.tv_sec = 0;     t.it_value.tv_usec = 10;      max_data_size = 0;     max_stack_size = 0;      setitimer(itimer_real, &t,0);     signal(sigalrm, medir_memoria); }  void detener_rastreo () {     signal(sigalrm, sig_dfl);      printf("data: %ld\nstack: %ld\n", max_data_size, max_stack_size); }  #else  #endif 

the main() function works calling of them in order:

  1. rastrear_memoria()
  2. function of algorithm testing
  3. detener_rastreo()

how can solve this? alarm message mean?

first, setting itimer ring every 10 µs optimistic, since ten microseconds small interval of time. try 500 µs (or perhaps 20 milliseconds, i.e. 20000 µs) instead of 10 µs first.

stop execution in periods of 10 milliseconds

you have coded period of 10 microseconds, not milliseconds!

then, should exchange 2 lines , code:

signal(sigalrm, medir_memoria); setitimer(itimer_real, &t,0); 

so signal handler set before first itimer rings.

i guess first itimer rings before signal handler installed. read signal(7) , time(7). default handling of sigalrm termination.

btw, better way measure time used function clock_gettime(2) or clock(3). vdso(7) tricks, clock_gettime able clock in less 50 nanoseconds on i5-4690s desktop computer.

trying memory consumed

you consider using proc(5) e.g. opening, reading, , closing quickly /proc/self/status or /proc/self/statm etc....

(i guess on linux)

btw, measurements disappoint you: notice quite free(3) don't release memory kernel (thru munmap(2)...) mark & manage zone reusable future malloc(3). might consider mallinfo(3) or malloc_info(3) notice not async-signal-safe cannot called inside signal handler.

(i tend believe approach flawed)


Comments