I want to create one file from c program and i want use bit long time in my c binary. But i want create file in such way that until my c program finish processing file created and unlock it nobody(may using vim or any other editor) can able to open and read file contents.
Please help me on this thanks in advance.
Best Answer
You can define a mandatory file lock on Unix, for this purpose. However, it's necessary to (re-)mount file system, so that it honors mandatory locks.
1 For example to remount the root fs, use (as root):
2 Now, let's create our secret file:
3 We need to set-group-id, and disable group execution privileges on the file:
And our C code to lock that file: (The code will lock the file, and keep it locked for a while, you can try another terminal to read it, the read will be delayed until lock is released)
More info at http://kernel.org/doc/Documentation/filesystems/mandatory-locking.txt