cancel
Showing results for 
Search instead for 
Did you mean: 

POSIX IO atomicity

SOLVED
Go to solution
Scotty HD
Frequent Advisor

POSIX IO atomicity

experts,

I read this -
Posix standard defines atomiciy such that all bytes from single file I/O request
that starts our together ends up together, without interleaving from other I/O requests.

What does this mean.

Example -
consider file has 100 blocks.
Reader reads blocks 1 to 100
and at the same time
Writer writes blocks 1 to 100

Question 1
In this case, Reader should get all 100 blocks of old data (before write)
or all 100 blocks of new data(after write), not mix data.
Is this correct ?

Question 2
What is the maximum size for which the atomicity is garunteed.
in the above example if the reader/writer were to read/write 10000
blocks, even then the atomicty rule will apply ?
is there a maximum limit for the atomicity ?

please advise

Scotty
5 REPLIES
Dennis Handly
Acclaimed Contributor

Re: POSIX IO atomicity

>Q1: not mix data. Is this correct?

Seems reasonable.

>Q2: What is the maximum size for which the atomicity is guaranteed. ... is there a maximum limit for the atomicity?

For pipes it is about 8 Kb, PIPE_MAX(5).
Scotty HD
Frequent Advisor

Re: POSIX IO atomicity

Dennis Handly>>
for question 2, i mean system calls write/read...

Scotty
Matti_Kurkela
Honored Contributor
Solution

Re: POSIX IO atomicity

On my Debian Linux laptop, I have a set of POSIX man pages (the manpages-posix-dev package).

Both read(3posix) and write(3posix) have this sentence:
---
If the value of _nbyte_ is greater than {SSIZE_MAX}, the result is implementation-defined.
---
Note: nbyte is the parameter in the read()/write() system call that determines the number of bytes to read/write.

I'm not a programming guru, but I would understand it like this:

A single read()/write() system call, with a data size of SSIZE_MAX bytes or less, is guaranteed to be atomic by POSIX.

If you try to read/write a larger piece than that in one system call, what happens will depend on the platform you're on. It may be rejected outright, or the system might accept it; if the system accepts it, it may or may not be an atomic operation.

If you run two separate read()/write() calls, that is clearly two separate I/O requests, and interleaving may happen between them.

A bit of Googling seems to indicate that you might generally expect SSIZE_MAX to be at least 32767 bytes, but it could be a lot more on some platforms.

http://www.pasc.org/interps/unofficial/db/p1003.1/pasc-1003.1-29.html

Since SSIZE_MAX seems to be a compile-time constant, if your program is supposed to be portable, you might put in some pre-processor directives like this if you need larger than 32kB atomic I/O requests:

#if (SSIZE_MAX < [required_size])
#then
#error "Oh noes! This system cannot do atomic reads/writes of [required_size]. Implement some file locking or something..."
#endif

MK
MK
Dennis Handly
Acclaimed Contributor

Re: POSIX IO atomicity

>for question 2, I mean system calls write/read.

Yes, and if you read/write to a pipe, you are limited. Don't know about regular disk files.
Scotty HD
Frequent Advisor

Re: POSIX IO atomicity

if i understand correct,
if io size is less than SSIZE_MAX then OS garuntees atomicity
otherwise its implementation defined (OS has the right to deal it
in its own way)

Scotty