mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Philippe Troin <phil@fifi.org>
To: Dave Chinner <david@fromorbit.com>
Cc: Juliusz Chroboczek <jch@pps.jussieu.fr>, linux-kernel@vger.kernel.org
Subject: Re: Write is not atomic?
Date: Mon, 15 Oct 2012 16:24:38 -0700	[thread overview]
Message-ID: <1350343478.4523.7.camel@air.home.fifi.org> (raw)
In-Reply-To: <20121015231302.GF2739@dastard>

On Tue, 2012-10-16 at 10:13 +1100, Dave Chinner wrote:
> On Mon, Oct 15, 2012 at 11:36:15PM +0200, Juliusz Chroboczek wrote:
> > Hi,
> > 
> > The Linux manual page for write(2) says:
> > 
> >     The adjustment of the file offset and the write operation are
> >     performed as an atomic step.
> 
> That's wrong. The file offset update is not synchronised at all with
> the write, and for a shared fd the update will race.

That's what O_APPEND or pread/pwrite are for.

> > This is apparently an extension to POSIX, which says
> > 
> >     This volume of IEEE Std 1003.1-2001 does not specify behavior of
> >     concurrent writes to a file from multiple processes. Applications
> >     should use some form of concurrency control.
> 
> This is how Linux behaves.
> 
> > The following fragment of code
> > 
> >     int fd;
> >     fd = open("exemple", O_CREAT | O_WRONLY | O_TRUNC, 0666);
> >     fork();
> >     write(fd, "Ouille", 6);
> >     close(fd);

can be replaced with:

        int fd;
        fd = open("exemple", O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0666);
        fork();
        write(fd, "Ouille", 6);
        close(fd);

or:

        int fd;
    fd = open("exemple", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    pid_t pid = fork();
    pwrite(fd, "Ouille", 6, strlen("Ouille")*(pid == 0));
    close(fd);

(both code fragments untested)

Phil.


  reply	other threads:[~2012-10-15 23:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-15 21:36 Juliusz Chroboczek
2012-10-15 22:21 ` Max Filippov
2012-10-15 23:36   ` Juliusz Chroboczek
2012-10-15 23:13 ` Dave Chinner
2012-10-15 23:24   ` Philippe Troin [this message]
2012-10-15 23:42     ` Max Filippov
2012-10-15 23:40 ` Jochen Striepe
2012-10-16  6:21   ` Juliusz Chroboczek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350343478.4523.7.camel@air.home.fifi.org \
    --to=phil@fifi.org \
    --cc=david@fromorbit.com \
    --cc=jch@pps.jussieu.fr \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®