title: Which lock function to use to prevent a file from being deleted while locked?
body: <p>a process running in root privileges on Mac locks a file, does some work on it and then unlocks it.</p>
  
  <p>The problem I am facing is that none of the locking functions that I have tried prevents a file from being deleted even when it is locked. So, it is possible for someone to delete the current file just after locking and create a new file with the same name.</p>
  
  <p>Is there some way to solve this? I have tried <code>flock</code>, <code>lockf</code>, <code>fopen</code>, <code>fcntl</code>.</p>
  
  <p>One way is to use <code>chflags uchg &lt;filename&gt;</code>, but I am not sure if it's advisable or not to use in a C++ program?</p>