mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Wragg <dpw@doc.ic.ac.uk>
To: "David Schwartz" <davids@webmaster.com>
Cc: <tytso@mit.edu>, <linux-kernel@vger.kernel.org>
Subject: Re: What protects f_pos?
Date: 13 Nov 2000 15:22:56 +0000	[thread overview]
Message-ID: <y7rg0kv99rj.fsf@sytry.doc.ic.ac.uk> (raw)
In-Reply-To: <NCBBLIEPOCNJOAEKBEAKKENGLNAA.davids@webmaster.com>
In-Reply-To: "David Schwartz"'s message of "Sun, 12 Nov 2000 14:27:54 -0800"

"David Schwartz" <davids@webmaster.com> writes:
> 	Suppose you had a multithreaded program that used a
> configuration file with a group of fixed-length blocks indicating what
> work it had to do. Each thread read a block from the file and then did
> the work. One might think that there's no need to protect the file
> descriptor with a mutex.

I don't think that this will work, due to a separate non-atomicity
issue with f_pos.  The generic file read and write implementations do
not atomically update f_pos.  They read f_pos to determine the file
offset to use, then manipulate the page cache (possibly sleeping on
I/O), and only then set f_pos to the appropriate updated value.  So
the example you suggest, with two threads, could do something like:

          Thread 1                           Thread 2

   sys_read(fd, buf1, len)

      off = file->f_pos                sys_read(fd, buf2, len)

         read to buf1                     off = file->f_pos

    file->f_pos = off + len                 read to buf2

                                        file->f_pos = off + len


So both threads read the same block, and f_pos only gets incremented
once.

(Pipes and sockets are a different matter, of course.)

2.2 has the same issue, since although the BKL is held, it will get
dropped if one of the threads sleeps on I/O.  (Earlier Linux versions
might well have the same issue, but I don't have the source around to
check.)

POSIX doesn't seem to bar this behaviour.  From 6.4.1.2:

    On a regular file or other file capable of seeking, read() shall
    start at a position in the file given by the file offset
    associated with fildes.  Before successful return from read(), the
    file offset shall be incremented by the number of bytes actually
    read.

Which is exactly what Linux does.  I can't find text anywhere else in
POSIX.1 that strengthens that condition for the case of multiple
processes/threads reading from the same file.  I'll try to find out
what the Austin Group has to say about this.


David
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

  reply	other threads:[~2000-11-13 15:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-04 22:16 David Wragg
2000-11-11 23:54 ` tytso
2000-11-12  1:56   ` David Wragg
2000-11-12 22:27     ` David Schwartz
2000-11-13 15:22       ` David Wragg [this message]
2000-11-13 15:44         ` Richard B. Johnson

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=y7rg0kv99rj.fsf@sytry.doc.ic.ac.uk \
    --to=dpw@doc.ic.ac.uk \
    --cc=davids@webmaster.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tytso@mit.edu \
    /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®