title: unable to write /proc/sys/kernel/ns_last_pid file
body: <p>I want to edit the <code>ns_last_pid</code> file present in <code>/proc/sys/kernel</code>, but i'm getting the error of <code>Read-only file system</code>. How to resolve this?
  This is what i've written to open the file.</p>
  <pre><code>int fd = open(&quot;/proc/sys/kernel/ns_last_pid&quot;, O_RDWR | O_CREAT, 0644);
              if (fd &lt; 0) {
                  cout&lt;&lt;strerror(errno)&lt;&lt;&quot;\n&quot;;
                  return 1;
              }
  </code></pre>
  <p>I've to write this file, change it's value. This file contains a single number represnting the last pid allocated to any process. I've to edit this so that i can get desired pid number for a process. like these guys are doing for their project <strong><code>CRIU</code></strong>(see first link).</p>
  <p><a href="https://criu.org/Pid_restore" rel="nofollow noreferrer">Pid_restore</a>(criu.org),</p>
  <p><a href="https://stackoverflow.com/questions/18122592/how-to-set-process-id-in-linux-for-a-specific-program">How to set process ID in Linux for a specific program</a>(stackoverflow answer)</p>
  <p><strong>EDIT 1:</strong> <strong>Smallest reproducible example</strong></p>
  <pre><code>#include &lt;fstream&gt;
  #include &lt;bits/stdc++.h&gt;
  #include &lt;sys/types.h&gt;
  #define _GNU_SOURCE             /* See feature_test_macros(7) */
  #include &lt;sched.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/file.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt; 
  #include &lt;errno.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/syscall.h&gt;
  
  using namespace std;
      int main(){
              printf(&quot;Opening ns_last_pid...\n&quot;);   
              int fd = open(&quot;/proc/sys/kernel/ns_last_pid&quot;, O_RDWR | O_CREAT, 0644);
              if (fd &lt; 0) {
                  cout&lt;&lt;strerror(errno)&lt;&lt;&quot;\n&quot;;
                  return 1;
              }
              printf(&quot;Locking ns_last_pid...\n&quot;);
              if (flock(fd, LOCK_EX)) {
                  close(fd);
                  printf(&quot;Can't lock ns_last_pid\n&quot;);
                  return 1;
              }
              printf(&quot;Done\n&quot;);
              char buf[100];
              int pid_max = 30000;
              snprintf(buf, sizeof(buf), &quot;%d&quot;, pid_max-1);
  
              printf(&quot;Writing pid-1 to ns_last_pid...\n&quot;);
              cout&lt;&lt;fd&lt;&lt;&quot;\n&quot;;
              if (write(fd, buf, strlen(buf)) != strlen(buf)) {
                 cout&lt;&lt;strerror(errno)&lt;&lt;&quot;\n&quot;;
                 printf(&quot;Can't write to buf\n&quot;);
                 return 1;
              }
          
              printf(&quot;Done\n&quot;);
          
              printf(&quot;Cleaning up...&quot;);
              if (flock(fd, LOCK_UN)) {
                  printf(&quot;Can't unlock&quot;);
                  }
          
              close(fd);
          
              printf(&quot;Done\n&quot;);            
                        
              return 0;
          }
  </code></pre>