473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Atomic line-reading without buffering

Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?

Mar 2 '07 #1
11 2041
japhy wrote:
Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?
Within Standard C all you can do is use the setvbuf function and
specify no buffering with _IONBF as one of it's arguments. Despite
this, the operating system may still use disk buffers when doing I/O.
To turn off all buffering is system dependant and you'll have to
examine the facilities your OS provides to do so.

The word atomic is inappropriate in the context in which you've used
it. Even when buffering is disabled, I/O is not guaranteed to be an
atomic operation.

Mar 2 '07 #2
In article <11********************@p10g2000cwp.googlegroups.c om>,
santosh <sa*********@gmail.comwrote:
>japhy wrote:
>Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?
>The word atomic is inappropriate in the context in which you've used
it. Even when buffering is disabled, I/O is not guaranteed to be an
atomic operation.
Though if you make the step over to POSIX, writes below a
system-dependant size are promised to be atomic. POSIX needs
this property because it deals with multiple processes (and
multiple threads in later editions) and it would be a real
nuisance to have the output of one write intermixed with the
output of another write.

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Mar 2 '07 #3
Walter Roberson wrote:
In article <11********************@p10g2000cwp.googlegroups.c om>,
santosh <sa*********@gmail.comwrote:
japhy wrote:
Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?
The word atomic is inappropriate in the context in which you've used
it. Even when buffering is disabled, I/O is not guaranteed to be an
atomic operation.

Though if you make the step over to POSIX, writes below a
system-dependant size are promised to be atomic. POSIX needs
this property because it deals with multiple processes (and
multiple threads in later editions) and it would be a real
nuisance to have the output of one write intermixed with the
output of another write.
Yes, thanks, I should probably have mentioned that, but I was thinking
more along the lines of the C standard's assurances with regard to
atomic operations.

Mar 2 '07 #4
santosh wrote:
>
.... snip ...
>
Yes, thanks, I should probably have mentioned that, but I was
thinking more along the lines of the C standard's assurances
with regard to atomic operations.
The C standard runs on a dedicated machine with no processes,
threads, interrupts, etc. and thus has no need of atomic
operations, semaphores, etc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #5
CBFalconer said:
santosh wrote:
>>
... snip ...
>>
Yes, thanks, I should probably have mentioned that, but I was
thinking more along the lines of the C standard's assurances
with regard to atomic operations.

The C standard runs on a dedicated machine with no processes,
threads, interrupts, etc. and thus has no need of atomic
operations, semaphores, etc.
Almost true. Even if we ignore the C99 floating point stuff as being
irrelevant here, we still must not forget sig_atomic_t, which is
defined to be "the (possibly volatile-qualified) integer type of an
object that can be accessed as an atomic entity, even in the presence
of asynchronous interrupts".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 2 '07 #6
"santosh" <sa*********@gmail.comwrote:
# Walter Roberson wrote:
# In article <11********************@p10g2000cwp.googlegroups.c om>,
# santosh <sa*********@gmail.comwrote:
# japhy wrote:
# Is there a way to read a line (a series of characters ending in a
# newline) from a file (either by descriptor or stream) atomically,
# without buffering additional contents of the file?
# >
# The word atomic is inappropriate in the context in which you've used
# it. Even when buffering is disabled, I/O is not guaranteed to be an
# atomic operation.
# >
# Though if you make the step over to POSIX, writes below a
# system-dependant size are promised to be atomic. POSIX needs
# this property because it deals with multiple processes (and
# multiple threads in later editions) and it would be a real
# nuisance to have the output of one write intermixed with the
# output of another write.
#
# Yes, thanks, I should probably have mentioned that, but I was thinking
# more along the lines of the C standard's assurances with regard to
# atomic operations.
#
#
#

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Mar 3 '07 #7
On Mar 2, 2:31 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Almost true. Even if we ignore the C99 floating point stuff as being
irrelevant here, we still must not forget sig_atomic_t, which is
defined to be "the (possibly volatile-qualified) integer type of an
object that can be accessed as an atomic entity, even in the presence
of asynchronous interrupts".
Just for my own information, does "asynchronous interrupts" include
thread pre-emption? Is this how thread mutexes are implemented? I was
perusing the boost library recently and saw a lot of inline assembly
for mutexes, etc. I presume if sig_atomic_t were thread-safe as well
as signal safe the boost people could have saved a lot of trouble?

Or is that because in something like:

void do_something(void)
{
static sig_atomic_t mx = 0;
start:
if (mx++ == 0) {
/* Wait for mx to be zero */
goto start;
} else {
/* Do some work */
}
}

mx may be atomic, but the test (mx++ == 0) is not guaranteed to be an
atomic operation? (And I presume there's no way to make such a
guarantee in C alone.)

-Bluejack

Mar 3 '07 #8
SM Ryan wrote:
"santosh" <sa*********@gmail.comwrote:
# Walter Roberson wrote:
# In article <11********************@p10g2000cwp.googlegroups.c om>,
# santosh <sa*********@gmail.comwrote:
# japhy wrote:
# Is there a way to read a line (a series of characters ending in a
# newline) from a file (either by descriptor or stream) atomically,
# without buffering additional contents of the file?
# >
# The word atomic is inappropriate in the context in which you've used
# it. Even when buffering is disabled, I/O is not guaranteed to be an
# atomic operation.
# >
# Though if you make the step over to POSIX, writes below a
# system-dependant size are promised to be atomic. POSIX needs
# this property because it deals with multiple processes (and
# multiple threads in later editions) and it would be a real
# nuisance to have the output of one write intermixed with the
# output of another write.
#
# Yes, thanks, I should probably have mentioned that, but I was thinking
# more along the lines of the C standard's assurances with regard to
# atomic operations.
#
#
#
Err, did you have something to say? Your post appears empty of a
reply, (except for the quoted text), but maybe that's a Google Groups
issue?

Mar 3 '07 #9
# Is there a way to read a line (a series of characters ending in a
# newline) from a file (either by descriptor or stream) atomically,
# without buffering additional contents of the file?

Generally once data is transferred from system to program
unless you're explicitly using locks, you lose atomicity.
On systems that transfer chunks of chars and expect the program
to find line structure in that, you aren't going to get
atmoic line reads. Some systems transfer lines; you're more
likely to have atomicity there.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Mar 3 '07 #10
"japhy" <ja*******@gmail.comwrote in message
Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?
Not really. The problem is that operating systems are now so complex that it
is difficult to define exactly what we mean by "unbuffered input". For
instance a frequently accessed small file may be memory mapped, completely
transparently to the user.

There are low-level routines provided on most systems that may do what you
want, called "open" insgtead of "fopen" and so forth.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Mar 3 '07 #11
bluejack said:
On Mar 2, 2:31 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Almost true. Even if we ignore the C99 floating point stuff as being
irrelevant here, we still must not forget sig_atomic_t, which is
defined to be "the (possibly volatile-qualified) integer type of an
object that can be accessed as an atomic entity, even in the presence
of asynchronous interrupts".

Just for my own information, does "asynchronous interrupts" include
thread pre-emption?
Since the Standard doesn't mention threads, it is (alas) impossible to
say within the context of ISO C.

<snip>
mx may be atomic, but the test (mx++ == 0) is not guaranteed to be an
atomic operation?
Correct.
(And I presume there's no way to make such a guarantee in C alone.)
Again, correct.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 3 '07 #12

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

Similar topics

5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
4
by: Seenu | last post by:
I'm experiencing some puzzling behaviour with some of my UDFs when declaring them as ATOMIC.. Basically I'm invoking another UDF (which uses some Java code) in one branch of a CASE statment, and...
5
by: hikums | last post by:
Begin Atomic Declare a integer; Set a=(select count(1) from claims.table1); if a<>0 then import into c:\tmptyfile.txt of del replace into claims.table1; end if; End!
5
by: Pegboy | last post by:
What does it mean to make a function atomic? Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not,...
7
by: dm | last post by:
Hi, I'd like to know if the C standard can guarantee that an assignment to/from a variable will be atomic, that is will occur in a single indivisible instruction. For example, if I have the...
0
by: yogeeswar | last post by:
Hi All I wrote SP with number of delete commands in an atomic block.And there is a possibility of deleting records from parent table before child table so I wrote a handler to handle the...
31
by: Michael | last post by:
Why are functions atomic? (I.e. they are not copied.) For example, I would like to make a copy of a function so I can change the default values: (2, 2) I would like the following...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
0
by: Vishwahere | last post by:
Hi I am using a query as below: BEGIN NOT ATOMIC FOR temp_row AS SELECT pr.REC_TRK_ID,cm.FINC_MTCH_KEY_ID,pr.MED_MTCH_KEY_ID FROM CARDTC.PGM_REQ pr LEFT OUTER JOIN CARDTC.CDB_MTCH cm ON...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.