473,626 Members | 3,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

flock usage

Is this a stupid use of flock:

FILE *fp=fopen(SOME_ FILE_CONSTANT," r+");
flock(fileno(fp ),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp ),LOCK_UN);
fclose(fp);

Does this accomplish real locking, or just narrow down the
race quite a bit? (Race existing between the unlock and the
fclose(), i guess.)

Normally I would think you'd want a separate file to do
nothing but the locking, and guard the use of the real data
file with locks on the lockfile.

Also, assuming the above is stupid, is the following a wee
bit smarter:

FILE *fp=fopen(SOME_ FILE_CONSTANT," r+");
flock(fileno(fp ),LOCK_EX);
something important here, including reads and a write to fp
fflush(fp); <<---- at least try to make
fdatasync(fp); <<---- sure contents out before unlock
flock(fileno(fp ),LOCK_UN);
fclose(fp);

(Or does this add very little?)
TIA!
Aug 30 '07 #1
4 19157
bf*@fenway.UUCP (Time Waster) writes:
Is this a stupid use of flock:

FILE *fp=fopen(SOME_ FILE_CONSTANT," r+");
flock(fileno(fp ),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp ),LOCK_UN);
fclose(fp);
[snip]

This is not a good place to ask. flock() is not a standard C
function; in fact, standard C provides no facility for locking files.

comp.unix.progr ammer is a better place to ask about this -- but since
flock() isn't defined by the POSIX standard either, they might advise
you to use lockf() instead.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 30 '07 #2
In article <y9IBi.11154$Eh 5.6838@trndny06 >,
Time Waster <no***@nowhere. comwrote:
>Is this a stupid use of flock:
>FILE *fp=fopen(SOME_ FILE_CONSTANT," r+");
flock(fileno(f p),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(f p),LOCK_UN);
fclose(fp);
flock() is not defined by the C language; it's properties are
OS specific; for example, there are important differences
between flock() for BSD or System V Unices.

flock() isn't even defined by POSIX.1. If you were intending to
use extensions, you could at least use extensions defined by
the POSIX.1 standard, such as using the POSIX fcntl() with
F_SETLK.

The most obvious stupidity in the code is that it doesn't
check the return values from flock(), so it will go ahead
and scribble on the file if a lock is denied. Not checking
that the open worked is stupid too.
You would probably have better success discussing this in
a newsgroup more specific to the variety of operating system
you are targetting.
--
All is vanity. -- Ecclesiastes
Aug 30 '07 #3
Time Waster wrote:
>
Is this a stupid use of flock:

FILE *fp=fopen(SOME_ FILE_CONSTANT," r+");
flock(fileno(fp ),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp ),LOCK_UN);
fclose(fp);
Maybe you should be asking this somewhere control of herds of birds
is topical. Standard C contains no such routine as 'flock', which
is thus off-topic on c.l.c.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 31 '07 #4
CBFalconer wrote:
>
Time Waster wrote:

Is this a stupid use of flock:

FILE *fp=fopen(SOME_ FILE_CONSTANT," r+");
flock(fileno(fp ),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp ),LOCK_UN);
fclose(fp);

Maybe you should be asking this somewhere control of herds of birds
is topical.
Flocks can contain either feathers or wool.

--
pete
Aug 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2269
by: Marc | last post by:
Hello, when using flock() I get a permission denied error: Warning: fopen("<filename>", "r+") - Permission denied in <pathtofile> on line 7 I do this: $fileToOpen=substr($PHP_SELF, strrpos($PHP_SELF,"/")+1); $fileHandle=fopen($fileToOpen, 'r+') or die($php_erormsg);
0
2066
by: Daniel Brunthaler | last post by:
hi, i've a problem with file locking. seems that flock() and gzopen() do not work together. i get the error (or warning) message ... Warning: flock(): cannot represent a stream of type ZLIB as a File Descriptor in ... when using flock() after gzopen().
14
3157
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required when opening a file with fopen() when there is contention for the file: $fp=fopen($ctr, 'w'); //only write if we can get lock on file if (flock($fp, LOCK_EX)) { fwrite($fp, "0");
3
4770
by: Rex Karz | last post by:
Newbie here. I interpret the fine print at http://us2.php.net/manual/en/function.flock.php to mean that flock() does not work where the file being locked is on an NFS filesystem. The commentary following the "official" description of flock() offers numerous circumventions broken flock() where the file needing atomic access is on an NFS filesystem.
4
7884
by: writeson | last post by:
Hi all, I've got a PHP program that I've added flock() to in order to protect multiple scripts trying to write to the same file. After I added the flock() to the code the performance of the code went way down. Can anyone tell me if calling flock() on a file handle is particularly slow? Thanks, Doug
3
2807
by: siliconmike | last post by:
I've scoured over manual page for flock on http://in2.php.net/flock but I can't understand a few things: 1. will flock wait until a different process releases the lock ? 2. please clarify this line: " If you don't want flock() to block while locking, add LOCK_NB " what does "block" mean above?
7
4480
by: sven.holcombe | last post by:
Hi there, I'm trying to just prevent multiple instances of a script running. I would have thought that the following code would do this for me. <php $fp = fopen("foo.txt", "w"); flock($fp, LOCK_EX + LOCK_NB) or die ("Could not get lock!\n"); echo "Got lock!\n";
2
3642
by: xucs007 | last post by:
I ran following 2 programs (lock1, lock2) at almost same time, to write either "123456", or "222" to file "aaa" at the same time. But I often just got "222456" in "aaa" . Is this a bug of python fcntl module ? See 2 programs I ran: #!/usr/bin/env python import fcntl, time
1
1743
by: Seb | last post by:
I'm trying to implement a file server using the code below. However the locking doesn't work. I can delete while put'ing a file. Anyone got an idea about why? best regards, seb
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.