title: getrlimit -1 return value
body: <p>While creating different benchmarks I often aim for the best latency. As you all know, memory in the heap is much more expensive to access than on the stack. We are now in 2021 and when I try <code>ulimit -s</code> I see that I have 8192kbs available. Couldn't it set higher?</p>
  <p>I was wondering about how to set it using c code and found <a href="https://linux.die.net/man/2/setrlimit" rel="nofollow noreferrer">setrlimit</a> and <a href="https://linux.die.net/man/2/getrlimit" rel="nofollow noreferrer">getrlimit</a>:</p>
  <blockquote>
  <p><em>The getrlimit() and setrlimit() system calls get and set resource limits respectively. Each resource has an associated soft and hard limit, as defined by the rlimit structure [...]</em></p>
  </blockquote>
  <p>I found <a href="https://stackoverflow.com/a/5864773/3161139">this answer</a> and <a href="https://stackoverflow.com/a/48426774/3161139">this one</a>, and, while they are both very interesting, there are still little things I don't get:</p>
  <pre class="lang-c prettyprint-override"><code>#define _GNU_SOURCE
  #define _FILE_OFFSET_BITS 64
  
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/resource.h&gt;
  #include &lt;errno.h&gt;
  
  #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
  
  int main(int argc, char *argv[])
  {
      struct rlimit old, new;
      struct rlimit *newp;
      pid_t pid;
  
      if (!(argc == 2 || argc == 4)) {
          fprintf(stderr, &quot;Usage: %s &lt;pid&gt; [&lt;new-soft-limit&gt; &quot;
          &quot;&lt;new-hard-limit&gt;]\n&quot;, argv[0]);
          exit(EXIT_FAILURE);
      }
  
      pid = atoi(argv[1]); /* PID of target process */
  
      newp = NULL;
      if (argc == 4) {
          new.rlim_cur = atoi(argv[2]);
          new.rlim_max = atoi(argv[3]);
       //   newp = &amp;new; // I comment this line to avoid messing up with the processes too much;)
      }
  
  /* Set CPU time limit of target process; retrieve and display previous limit */
  
      if (prlimit(pid, RLIMIT_CPU, newp, &amp;old) == -1)
          errExit(&quot;prlimit-1&quot;);
  
        ;
      printf(&quot;Previous limits: soft=%lld; hard=%lld\n&quot;, (long long) old.rlim_cur, (long long) old.rlim_max);
  
  /* Retrieve and display new CPU time limit */
  
      if (prlimit(pid, RLIMIT_CPU, NULL, &amp;old) == -1)
          errExit(&quot;prlimit-2&quot;);
      printf(&quot;New limits: soft=%lld; hard=%lld\n&quot;, (long long) old.rlim_cur, (long long) old.rlim_max);
  
      //perror(&quot;error:&quot;);
      exit(EXIT_FAILURE);
  }
  </code></pre>
  <p>If I try on any process, I will see, at least on Ubuntu 18.04 and compiling with gcc, that when it tries to retrieve the current and max limits it will return -1. <strong>What is this -1 return value exactly? Does it mean &quot;set to default system value&quot; or &quot;failed to retrieve&quot; and if it is &quot;failed to retrieve&quot;, what was the reason for that?</strong> Why is <code>if (prlimit(pid, RLIMIT_CPU, newp, &amp;old) == -1)</code> not triggered?</p>
  <pre><code>Previous limits: soft=-1; hard=-1
  </code></pre>
  <p>However if I uncomment <code>newp = &amp;new;</code> (commented on purpose), then I can set a new value for the process, both current and max, there is no problem with that and it is quite amazing. <strong>What would be the max value that I could theoretically set?</strong> I tried with <code>sudo gcc raise_stack.c &amp;&amp; ./a.out 9194 8192 16384</code> but I am too afraid to burn my cpu to try it myself ;)</p>
  <pre><code>New limits: soft=8192; hard=16384
  </code></pre>
  <p>Many thanks in advance for helping me to better understand getrlimit.</p>
  <p>NB: avoid &quot;if you have to raise your stack memory then you are doing something wrong&quot;. This is not an opinion question.</p>
  <p>edit: cant figure out where to set errno to get the right result</p>
  <pre><code>error:: Success
  error:: Success
  error:: Success
  error:: Success
  error:: Success
  error:: Success
  Previous limits: soft=-1; hard=-1
  error:: Success
  New limits: soft=-1; hard=-1
  error:: Success
  
  </code></pre>
  <p>I set it after</p>
  <pre class="lang-c prettyprint-override"><code>long long a = prlimit(pid, RLIMIT_CPU, newp, &amp;old);
  perror(&quot;error:&quot;);
  </code></pre>
  <p>but still not the ERRNO that I am looking for.</p>