ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, despite looking on google (possibly im searching without vital keywords?) and a forum search I can't seem to dig up anything usefull on flock and touch in linux.
Im looking to lock a file with flock(), and I think touch() is used to prevent a file from accidentally being permenantly locked?
Any relevant url's or even better code snippet examples would be VERY appreciated, as I have no idea how to use either of these, and my guessing attempts, while compiling don't seem to lock the file, nor return any perror.
EDIT: I should add, this is in C. I can find a few examples in other situations, that I don't understand how to use in C.
That man page for touch is for the shell command touch. I'm not seeing any man pages for a function form of touch on my Slackware system, but I'm assuming it would do the same thing.
P.S. The forum rules indicate that you should wait 24 hours before bumping a topic. Not everyone reading these forums are in the same timezone as you.
flock(2) places advisory locks only; given suitable permissions on a file, a process is free to ignore the use of flock(2) and perform I/O on the file.
In other words, you have to check for a lock explicitly, it's not handled automatically for you...
Originally posted by chrism01
In other words, you have to check for a lock explicitly, it's not handled automatically for you...
It is possible though using fcntl().
Code:
Quote from "man fcntl"
Mandatory locking
(Non-POSIX.) The above record locks may be either advisory or mandatory, and are advi-
sory by default. To make use of mandatory locks, mandatory locking must be enabled
(using the "-o mand" option to mount(8)) for the file system containing the file to be
locked and enabled on the file itself (by disabling group execute permission on the file
and enabling the set-GID permission bit).
Advisory locks are not enforced and are useful only between cooperating processes.
Mandatory locks are enforced for all processes.
If those requirements are satisfied, you should be able to lock an open file descriptor with a function like:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.