Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > syncronizing between two file descriptors open on the same file

Reply
Thread Tools

syncronizing between two file descriptors open on the same file

 
 
JG
Guest
Posts: n/a
 
      02-04-2005
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

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      02-04-2005
JG <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-04-2005
In article <cu0i11$7mb$(E-Mail Removed)>,
Joona I Palaste <(E-Mail Removed)> wrote:
:JG <(E-Mail Removed)> 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)
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      02-04-2005
In article <(E-Mail Removed). com>,
JG <(E-Mail Removed)> 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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      02-04-2005
In article <cu0leb$n25$(E-Mail Removed)>,
Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote:
|In article <(E-Mail Removed). com>,
|JG <(E-Mail Removed)> 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.
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      02-08-2005
On Fri, 04 Feb 2005 22:05:38 +0000, Walter Roberson wrote:

> In article <cu0leb$n25$(E-Mail Removed)>,
> Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote:
> |In article <(E-Mail Removed). com>,
> |JG <(E-Mail Removed)> 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.programmer. 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.programmer.

Lawrence

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syncronizing a list adisps C Programming 0 12-21-2010 05:52 PM
syncronizing pocketPc using bluetooth =?Utf-8?B?TWlrZQ==?= Wireless Networking 0 11-11-2004 10:31 PM
Syncronizing Data - disconnected scenario REB ASP .Net 0 04-02-2004 04:06 PM
Re: Syncronizing two instances of the same control Hermit Dave ASP .Net 5 01-13-2004 12:32 AM
Syncronizing two instances of same control Aaron Prohaska ASP .Net 0 11-19-2003 09:29 PM



Advertisments