title: how to lock a file so that other process cannot cat it?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      struct flock fl;
      fl.l_start = 0;
      fl.l_len = 517; //found the size of the file outside (not best practise, I know)
      fl.l_whence = SEEK_SET;
      int fd = open(&quot;test&quot;, O_RDWR);
      if(fd&lt;0)
          perror(&quot;open&quot;);
      
      fl.l_type = F_RDLCK;
      fl.l_pid = getpid();
      if(fcntl(fd, F_SETLK, &amp;fl) &lt; 0)
      {
          perror(&quot;fcntl&quot;); //geting fcntl: Invalid argument
          exit(1);
      }
      
      if(fl.l_type != F_UNLCK)
      {
          printf(&quot;file has been exclusively locked by process:%u\n&quot;,fl.l_pid);
          printf(&quot;press enter to release the file\n&quot;);
          getchar();
          fl.l_type = F_UNLCK; //file released
      }
  }
  </code></pre>
  <p>I would like to lock a file (<code>test</code>), which contain lorem ipsum (some random text), so that other process could not <code>cat</code> it until the currect process release the lock. But which argument passed to <code>fcntl</code> is wrong?</p>
  <p>EDIT:
  After comments I have initialized some members of <code>fl</code> variable (see edits), no, despite working. I can still <code>cat</code> the locked file <code>test</code> in another process... why, when it is locked?</p>