Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Is the write(2) system call atomic



| There has been some discussion on another list regarding the write(2) system
| call on Linux.
| Here is a short code fragment to illustrate where the parent process creates
| and opens a file for writing, then forks a child. it was my impression that
| the kernel maintains a single structure for each open file. If that is true
| then we would see all 6 lines. In most of the failure cases we see that the
| first line and part of the second line of the parent are overwritten by the
| child.

I've  always  understood  that  there  was  a  single  per-open  data
structure that contains things like the kernel file buffer, but there
was also a per-process structure that contains the offset. Otherwise,
you'd  have an lseek() call in one process affect the other's offset,
which would be truly bizarre to try to debug.

I've seen occasional warnings that unless you use the O_APPEND  flag,
you can't rely on two writers' writes not overlapping.

I've written a few cases of code that updates a  file  simultaneously
from multiple processes, but I've always made sure that they used one
of the many possibe schemes to coordinate their writes. I don't think
the  code  could have worked if an lseek() affected all the processes
with the file open for writing.


--
   _,
   O   John Chambers
 <:#/> <jc at trillian.mit.edu>
   +   <jc1742 at gmail.com>
  /#\  in Waltham, Massachusetts, USA, Earth
  | |




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org