filelock


File locking in Linux using c programme


I want to create one file from c programme and i want use bit long time in my c binary.
but i want create file in such way that until my c programme 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.
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):
mount -oremount,mand /
2 Now, let's create our secret file:
echo "big secret" > locked_file
3 We need to set-group-id, and disable group execution privileges on the file:
chmod g+s,g-x locked_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)
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main() {
struct flock fl;
int fd;
fl.l_type = F_WRLCK; /* read/write lock */
fl.l_whence = SEEK_SET; /* beginning of file */
fl.l_start = 0; /* offset from l_whence */
fl.l_len = 0; /* length, 0 = to EOF */
fl.l_pid = getpid(); /* PID */
fd = open("locked_file", O_RDWR | O_EXCL); /* not 100% sure if O_EXCL needed */
fcntl(fd, F_SETLKW, &fl); /* set lock */
usleep(10000000);
printf("\n release lock \n");
fl.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &fl); /* unset lock */
}
More info at
http://kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
Files can be locked by using flock(). Its syntax is
#include <sys/file.h>
#define LOCK_SH 1 /* shared lock */
#define LOCK_EX 2 /* exclusive lock */
#define LOCK_NB 4 /* don't block when locking */
#define LOCK_UN 8 /* unlock */
int flock(int fd, int operation);
First file is opened using fopen() or open(). Then this opened file is locked using flock() as given below
int fd = open("test.txt","r");
int lock = flock(fd, LOCK_SH); // Lock the file . . .
// . . . .
// Locked file in use
// . . . .
int release = flock(fd, LOCK_UN); // Unlock the file . . .

Related Links

File locking in Linux using c programme

Categories

HOME
elasticsearch
apache-nifi
class
yaml
activex
command-prompt
ews
ios-simulator
consul
hyperledger
ui-automation
polymer-2.x
solution
fbloginview
tiff
smooks
pm2
sslhandshakeexception
mahout-recommender
bnf
google-tasks-api
nest-api
user-defined-types
rpmbuild
maven-plugin
significance
psql
sharedpreferences
gollum-wiki
websphere-portal
pubxml
android-cardview
sfml
unmarshalling
intune
premake
steam-web-api
memory-fences
epplus
gsp
serversocket
nppexec
game-center
access-denied
vungle-ads
moinmoin
xcode8.2
music21
image-registration
dblink
business-rules
bpms
elastic4s
deviare
livecycle
svnserve
fiber
unsigned
node-glob
jemdoc
textblock
onepage-checkout
certificate-authority
ui4j
openshift-cartridge
highest
netdatacontractserializer
vdm-sl
relational-model
azure-xplat-cli
mgwt
debug-symbols
qt5.4
prism.js
neo4jphp
tabletools
accountmanager
profiles
lsa
facebook-game-groups
pbkdf2
visual-c++-2005
vlab
disjoint-union
office-app
user-forums
clearinterval
sfinae
uploading
background-repeat
differentiation
radscheduler
play2-mini
nsrangeexception
winsxs
simile
activestate
remote-working

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App