RicardasSim

learning c

Dec 23rd, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/resource.h>
  3.  
  4. typedef struct {
  5.     unsigned long size, resident, share, text, lib, data, dt;
  6. }St;
  7.  
  8. void readMemoryStatus()
  9. {
  10.    
  11.     St r;
  12.  
  13.     FILE* file = fopen("/proc/self/statm", "r");
  14.    
  15.     if(file){
  16.        
  17.         int rv = fscanf(file,"%lu %lu %lu %lu %lu %lu %lu", &r.size, &r.resident, &r.share, &r.text, &r.lib, &r.data, &r.dt);
  18.  
  19.         if(rv == 7){
  20.             printf("%lu %lu %lu %lu %lu %lu %lu\n", r.size, r.resident, r.share, r.text, r.lib, r.data, r.dt);
  21.         }
  22.        
  23.         fclose(file);
  24.     }
  25.    
  26. }
  27.  
  28.  
  29. long int g_Variable = 0;
  30.  
  31. long int testas(long int i) {
  32.  
  33.     if(i <= 0) return 0;
  34.    
  35.     g_Variable++;
  36.    
  37.     readMemoryStatus();
  38.    
  39.     return testas(i - 1);
  40.  
  41. }
  42.  
  43. //long int -2147483648 2147483647
  44.  
  45. int  main() {
  46.    long int i = 5;
  47.    printf("\n%ld %ld\n", testas(i), g_Variable);
  48.    printf("%ld\n", g_Variable);
  49.    return 0;
  50. }
  51.  
  52. /*
  53. with -O0 and i = 500000;
  54.  
  55. in pages
  56. ...
  57. 3140 2403 353 1 0 2090 0
  58. ...
  59. 3141 2403 353 1 0 2091 0
  60. ...
  61. 3142 2403 353 1 0 2092 0
  62. SEGFAULT (core dumped)
  63.  
  64. valgrind --tool=massif ./testas
  65. Stack overflow in thread #1: can't grow stack to 0x1ffe801000
  66. Process terminating with default action of signal 11 (SIGSEGV)
  67.  
  68. ulimit
  69. stack size (kbytes, -s) 8192
  70.  
  71.  
  72. ok with -03 and i = 500000:
  73. 1127 193 177 1 0 77 0
  74.    
  75. */
Add Comment
Please, Sign In to add comment