473,624 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

syncronizing between two file descriptors open on the same file

JG
Hi all,

Does anyone know how the implementations on Linux and Windows handle
synchronization between a read and write FD open to the same file.

For example, if I have 2 FD open to file X.txt. 1 I use for reading,
the other for writing. If I write to position 125 on the write_FD,
call flush, and then turn around and read from the read_FD position
125, am I guaranteed to get the result I just wrote?

I could call sync (unix) or commit (win32) after each write, but that
would be a performance killer.

Thanks for the help.

Jacob

Nov 14 '05 #1
5 3548
JG <ja*********@gm ail.com> scribbled the following:
Hi all, Does anyone know how the implementations on Linux and Windows handle
synchronization between a read and write FD open to the same file.


I think people on Linux and Windows newsgroups would. As far as C
knows or cares, only one operation happens to a file at a time.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
Nov 14 '05 #2
In article <cu**********@o ravannahka.hels inki.fi>,
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
:JG <ja*********@gm ail.com> scribbled the following:
:> Does anyone know how the implementations on Linux and Windows handle
:> synchronization between a read and write FD open to the same file.

:I think people on Linux and Windows newsgroups would. As far as C
:knows or cares, only one operation happens to a file at a time.

I haven't looked at the C9x specs, but in C89, FD's are not part
of the language or library specifications. C89 talks only of
FILE* operations.
--
I don't know if there's destiny,
but there's a decision! -- Wim Wenders (WoD)
Nov 14 '05 #3
In article <11************ *********@g14g2 000cwa.googlegr oups.com>,
JG <ja*********@gm ail.com> wrote:
:For example, if I have 2 FD open to file X.txt. 1 I use for reading,
:the other for writing. If I write to position 125 on the write_FD,
:call flush, and then turn around and read from the read_FD position
:125, am I guaranteed to get the result I just wrote?

$ man fopen
However, output may not be directly
followed by input without an intervening fseek, fsetpos, or rewind.
Similarly, input may not be directly followed by output without an
intervening call to one of these functions, unless the input operation
left the file positioned at end-of-file.

The fflush() man page on the same system talks about fd's being
synchronized in some cases, but I would want to check out my X3.159
and POSIX.1 copies to see whether it would be usable for your purpose,
--
Entropy is the logarithm of probability -- Boltzmann
Nov 14 '05 #4
In article <cu**********@c anopus.cc.umani toba.ca>,
Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
|In article <11************ *********@g14g2 000cwa.googlegr oups.com>,
|JG <ja*********@gm ail.com> wrote:
|:For example, if I have 2 FD open to file X.txt. 1 I use for reading,
|:the other for writing. If I write to position 125 on the write_FD,
|:call flush, and then turn around and read from the read_FD position
|:125, am I guaranteed to get the result I just wrote?

|$ man fopen
| However, output may not be directly
| followed by input without an intervening fseek, fsetpos, or rewind.

The C89 spec includes "fflush()" in that list of functions.

Note though that fflush() in the C89 spec is only defined to affect
output or update streams, so you cannot portably use fflush() to
synchronize between multiple fread()'s.

POSIX.1 does not define flush(). In POSIX.1 if you write() to a
fd that is not associated with a stream (e.g., you didn't use
fileno(FILE*) to get the fd and you didn't use fdopen() to promote
the fd into a FILE*), then you do not need to do anything in the
process that did the write(), but the process that will do the
read() needs to lseek() [or fseek()] before the reading takes place.

--
Oh, yeah, an African swallow maybe, but not a European swallow.
That's my point.
Nov 14 '05 #5
On Fri, 04 Feb 2005 22:05:38 +0000, Walter Roberson wrote:
In article <cu**********@c anopus.cc.umani toba.ca>,
Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
|In article <11************ *********@g14g2 000cwa.googlegr oups.com>,
|JG <ja*********@gm ail.com> wrote:
|:For example, if I have 2 FD open to file X.txt. 1 I use for reading,
|:the other for writing. If I write to position 125 on the write_FD,
|:call flush, and then turn around and read from the read_FD position
|:125, am I guaranteed to get the result I just wrote?
If this is referring to POSIX file descriptors a good place top discuss it
is comp.unix.progr ammer. The concept of a file descriptor doesn't exist in
the C language itself.
|$ man fopen
| However, output may not be directly
| followed by input without an intervening fseek, fsetpos, or rewind.
fopen() is part of the C lannguage but the interface it provides does not
involve file descriptors.
The C89 spec includes "fflush()" in that list of functions.

Note though that fflush() in the C89 spec is only defined to affect
output or update streams, so you cannot portably use fflush() to
synchronize between multiple fread()'s.
In what sense do multiple freads() need synchronising?
POSIX.1 does not define flush(). In POSIX.1 if you write() to a fd that
is not associated with a stream (e.g., you didn't use fileno(FILE*) to
get the fd and you didn't use fdopen() to promote the fd into a FILE*),
then you do not need to do anything in the process that did the write(),
Ah ha
but the process that will do the read() needs to lseek() [or fseek()]
before the reading takes place.


It is dangerous to take advice that is off-topic in the newsgroup because
there may not be anybody else reading the newsgroup who knows enough or is
prepared to post corrections to errors. Again, this is best discussed in
comp.unix.progr ammer.

Lawrence

Nov 14 '05 #6

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

Similar topics

5
2920
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
4
5810
by: Joe Lester | last post by:
I'm trying to figure out what the optimal Postgres configuration would be for my server (with 200 connecting clients, even though I'd really like to get it up to 500). I've got a 700 MHz eMac running Mac OS 10.3.2 (Panther) with 512 MB of RAM. I've messed around with some settings but I'm still getting an occasional "out of file descriptor" error, especially when performing a VACUUM. Like so... 2004-04-13 23:30:05 LOG: out of file...
36
2589
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): options { directory "/var/named"; listen-on { 192.168.11.22; 127.0.0.1; }; forwarders { 168.126.63.1; };
0
1375
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
1
64087
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
5
5253
by: yinglcs | last post by:
I have a c/c++ program in linux. I would like to know if I kill my program (e.g. exit(1)), will it release all the file descriptors my program held (regardless if I call close(fd) of each file descriptor)? Thank you.
8
12005
ashitpro
by: ashitpro | last post by:
Understanding Ext-2 file system:chapter 1 The first block in each Ext2 partition is never managed by the Ext2 filesystem, because it is reserved for the partition boot sector. The rest of the Ext2 partition is split into block groups, each of which has the layout shown in Image 2. As you will notice from the figure, some data structures must fit in exactly one block, while others may require more than one block. All the block groups...
2
5428
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads are reading the content written into the file. Can anybody tell me is there a performance or any other gain (except for the multex locking) by using different file descriptors in each thread for the same file rather than using a single FD with a...
2
4673
by: TP | last post by:
Hi everybody, The following code does not redirect the output of os.system("ls") in a file: import sys, os saveout = sys.stdout fd = open( 'toto', 'w' ) sys.stdout = fd os.system( "ls" )
0
8680
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
8625
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
8336
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
8482
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
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
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
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.